Using Python to Query a Notion Database

June 15, 2025

A few weeks ago, I wrote a post titled “What I’m Building Next.”

I talked about how I’m going all-in with my website and plan to create pages that display and visualize data I track in my personal life. One type of data I collect is the content I consume daily. This includes blogs, articles, videos, and podcasts. Since I consume so many pieces, I want to show them on my website to share what’s useful and what interests me at a given moment.

My data is currently stored across multiple Notion pages, and fortunately, Notion provides an easy-to-use API for extracting data from your workspace.

I’m building a pipeline to move data from Notion into my personal website (via the Webflow API). I still have a lot to do and am still in the data extraction phase.

From research and reading the Notion API documentation, I've found that the simplest way for me to extract my data is by building a Notion database. My data is scattered across many pages, so I have to go through the tedious and manual exercise of entering it into the Notion DB. That’s going to take time and is a story for another time.

For now, I want to talk about how simple it is to query a Notion database using Python.

There’s an awesome Python SDK for Notion’s API that does the heavy lifting, so you can get what you want with a few lines of code.

It’s literally as simple as this:

import os
from notion_client import Client
from dotenv import load_dotenv

load_dotenv()

NotionApiKey = os.getenv("YOUR_NOTION_API_KEY")
Notion = Client(auth=NotionApiKey)

NotionDbId = "abcdef1234567890abcdef1234567890" # Some 32 character string
# Get this from the end of your database's url -> https://www.notion.so/your-workspace/your-database-name-abcdef1234567890abcdef1234567890

Response = Notion.databases.query(database_id=NotionDbId)

print(Response)

That’s all.

Note that Response returns a JSON object. Using the above script prints all the data concerning your Notion databases. You can customize the script further to specify fields and values you want; this way, you don’t have to process unnecessary data downstream.

But this is the basic starting point for you to begin using Python to extract data from a Notion database.