Notion API with Python

Lucas B.
3 min readOct 12, 2021

--

Notion is a widely use app where you can manage from your simple notes or reminders to complex Kanban boards or interconnected databases. Now they have released there API so here is an example written in Python with a small shell automation.

Requirements:

  • Notion account with an existing page I'm going to manipulate in the example.
  • What Notion called Integration, this integration needs to have access to the page itself.
https://www.notion.so/my-integrations
Page share settings
  • Python environment setup (which I assume you already has, everyone has a python env up and running right? :). Here is a good articule with the entire walkthrough).

Hands on, for this example I'm going to update an existing Notion page so checking the documentation here is the great place to start.

Make the curl requests can be done in many different ways but I've chosen to make it using python with the following modules:

import requests
import json
import sys

The payload block:

payload = json.dumps({
"children": [
{
"object": "block",
"type": "paragraph",
"paragraph": {
"text": [
{
"type": "text",
"text": {
"content": data, //data is the new block to be added in the page, in my case a new line.
}
}
]
}
}
]
})

This is a PATCH request so the call is done in this way:

response = requests.request("PATCH", url, headers=headers, data=payload)

Here is the link for the python request method.

An example of a successful call is like this one, in my case I've written a small bash script that can execute the python call with a string as an argument.

Notion response
Screenshot from Notion page

As shown in this example is quite easy to automate or include Notion in your daily activities. Notion capabilities are huge with tons of features and now with the API in place many tasks can be automated.

--

--

No responses yet