Today I’m going to share with you how I stay organized and visualize my progress using the Notion API.
I use Notion a LOT. First, as a knowledge management system, where I keep all my documents and new ideas I’m working on. Second, and even more importantly, I started using several months ago as a to-do list where I keep track of all the tasks and deadlines.
Needless to say, it is probably the only tool I did not ditch after a few months of using it.
“Why not use the interface?” Right, the interface is great, but let’s say the sheer amount of documents I have makes managing my workspaces (yes with an S) a project of its own. I have this saying:
I never like doing things manually when they can be automated in a few scripts. Yes, I’m aware of Notion’s automations; I would like to try it at some point, but my method works and I have full control, which in my experience most automation features in apps lack.
Notion’s API Documentation
For more information about the Notion API, please refer to the official documentation
Importing a database using notion API
Databases are the single best thing about notion. you can create a database, add some pages and link it to existing databases you already have.
A simple example would be linking a to-do database with a projects database.
Step 1: Configure an integration
the first step i to configure an integration. It serves as a bridge between our notion database and the python scripts. Using the password (secret_key
), we can allow the script to access any part of our workspace we give it access to.
First, go to your notion’s settings > My connections. Click on Develop or manage integrations
We will create a new integration like some of the ones you probably use to import Google Docs to notion, for example.
click on New integration
and start filling the required fields:
- Name the integration whatever you want. I choose
🐍 Python APP
- We will keep our application as
internal
, since it is for personal usage. - Select a workspace, I have only one default workspace called
Default Workspace
Next, go back to your notion workspace and add the integration. You should be able to find it using the search bar.
Now you are all set! Let’s open a new folder and our VS Code (or whatever you happen to use), and start writing some code!
Create a new python notebook in the folder, and start importing the requests
library
import requests
Now we will add some variables:
- The integration secret: you can find it by opening the integrations page like we did earlier.
- The database ID: you can find your ID by opening a database and copying the part after between
\
and?v=
- The headers: contains authentication information to access the notion database
import requests
NOTION_TOKEN=<'Replace with the integration secret'
DATABASE_ID='Replace with the Database ID'
#Add the headers
headers={
"Authorization": "Bearer " + NOTION_TOKEN,
"Content-Type": "application/json",
"Notion-Version": "2022-06-28", #repalce with a newer version if it exists.
}
Step 2: reading a database using notion API
Now, once we have the access sorted out, let’s get our database.
import json
url = f"https://api.notion.com/v1/databases/{database_id}/query"
# Payload information
payload = {
"page_size": 5,
}
#Make request to get 5 pages from our notion database
response=requests.post(url,headers=headers,json=payload)
#print output as a Json
print(response.json())
Let’s break down our code:
- We imported the JSON library to make the data readable, and we can extract fields easily later on.
- The payload dictionary contains information about the request, like how many pages we want.
- We used the
post
methodt
o get the data from notion.
When you print the data, it will still be hard to read. Let’s create a JSON file to store it.
json_db=response.json()
with open('db.json','w') as db:
json.dump(json_db,db,indent=4)
You will have a new file called db.json
where you can better see your import.
Final words
Importing your notion’s data is easy as long as you follow their documentation (which is great!). In the upcoming articles, I will share with you how I use the import to manage and update my data automatically.