Fetching data from Notion API with Python

Unofficial Python 3 client for Notion.so API v3

12 Jul 2022

Library resources
PyPI ---
Github https://github.com/jamalex/notion-py
Documentation ---
Blog https://medium.com/@jamiealexandre/introducing-notion-py-an-unofficial-python-api-wrapper-for-notion-so-603700f92369

Working code:

from dotenv import load_dotenv
load_dotenv()

import json
import requests

token = os.getenv("NOTION_TOKEN") # generate at https://www.notion.so/my-integrations + ensure all access is granted
database_id = os.getenv("NOTION_DATABASE_ID") # first variable in database URL, ie before ?

headers = {
    "Authorization": "Bearer " + token,
    "Content-Type": "application/json",
    "Notion-Version": "2021-05-13"
}

url = f'https://api.notion.com/v1/databases/{database_id}/query'

r = requests.post(url, headers={
    "Authorization": f"Bearer {token}",
    "Notion-Version": "2021-08-16"
    })

result_dict = r.json()
results = result_dict['results']

links

social