How to make a request to Notion API using Python

·

2 min read

Python script using the Notion API to read a specific page

Below is a Python script that uses the requests library to interact with the Notion API and read a specific page. This script automates the process described earlier.

Prerequisites:

  1. Notion Account: Ensure you have a Notion account and a page you want to read.
  2. API Token: Obtain an integration token from Notion by creating an integration at Notion Integrations.
    1. Copy yourInternal Integration Secret and use that as your token
  3. Install the requests library if you haven't already:

     pip install requests
    

Python Script:

import requests

def get_notion_page(page_id, token):
    url = f"<https://api.notion.com/v1/pages/{page_id}>"

    headers = {
        "Authorization": f"Bearer {token}",
        "Notion-Version": "2022-06-28",
        "Content-Type": "application/json"
    }

    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        print("Page details:")
        print(response.json())
    else:
        print(f"Failed to retrieve page. Status code: {response.status_code}")
        print(response.text)

if __name__ == "__main__":
    # Replace these variables with your actual values
    page_id = "your_page_id"
    token = "your_integration_token"

    get_notion_page(page_id, token)

How to Use:

  1. Replace "your_page_id" with the actual ID of the Notion page you want to read.
  2. Replace "your_integration_token" with your actual Notion integration token.
  3. Run the script.

Running the Script:

Save the script to a file, for example notion_read_page.py, and run it using Python:

python notion_read_page.py

The script will send a GET request to the Notion API and print the details of the specified page.

Written with the help of Gen AI!