Automation

Using Notion API in Python to Automate Page Operations

Learn how to use the Notion API in Python to automate operations such as creating, reading, updating, and deleting page entries.

4 min read

Learn how to use the Notion API in Python to automate tasks like retrieving, creating, updating, and deleting pages in your Notion workspace. We will guide you step-by-step to set up and execute a seamless integration.

Prerequisites: Getting Started with Notion API

Before beginning, ensure the following setup steps are complete:

prerequisites

  • Create an Integration in the Notion admin dashboard.
  • Obtain your API token from the Integration setup.
  • Share the target database with your Integration using the "Add Connections" option in Notion.
  • Copy the Database ID by accessing the database's sharing URL.
  • Install Python and necessary libraries:
    • requests for sending API calls.
    • Optionally datetime for formatting dates.

Steps to Automate Database Manipulations

Learn how to perform CRUD operations on a Notion database using Python.

steps

  1. Set up the API token and database ID: Define the API token and database ID as string variables inside a Python script.

    python
    notion_token = "YOUR_API_TOKEN"
    database_id = "YOUR_DATABASE_ID"
  2. Configure headers for API requests: Create a dictionary for the headers. Make sure this code is placed after defining notion_token.

    python
    headers = {
        "Authorization": f"Bearer {notion_token}",
        "Content-Type": "application/json",
        "Notion-Version": "2022-06-28"
    }
  3. Query the database to retrieve pages: Write a function to get all pages from the database.

    python
    import requests
    
    def get_pages(database_id, headers):
        url = f"https://api.notion.com/v1/databases/{database_id}/query"
        response = requests.post(url, headers=headers)
        pages = response.json()
        return pages.get("results", [])
  4. Add new pages to the database: Create a helper function to add pages.

    python
    def create_page(database_id, properties, headers):
        url = "https://api.notion.com/v1/pages"
        payload = {
            "parent": {"database_id": database_id},
            "properties": properties
        }
        response = requests.post(url, headers=headers, json=payload)
        return response.status_code == 200
  5. Update existing pages: Write a function to modify page properties.

    python
    def update_page(page_id, properties, headers):
        url = f"https://api.notion.com/v1/pages/{page_id}"
        payload = {"properties": properties}
        response = requests.patch(url, headers=headers, json=payload)
        return response.status_code == 200
  6. Delete pages from the database: Archive a page using the patch method to update archived status.

    python
    def delete_page(page_id, headers):
        url = f"https://api.notion.com/v1/pages/{page_id}"
        payload = {"archived": True}
        response = requests.patch(url, headers=headers, json=payload)
        return response.status_code == 200

Verify each step with test cases, input-output formats, and debug messages for success.

Handling the Shared Database Limitation

A common challenge: Your integration cannot see the database unless explicitly shared. Resolve this issue as follows:

Python Example Code

Example Python script demonstrating CRUD operations with the Notion API.

Python Script

# Install the requests library
pip install requests

# Run the sample script to verify operations
python automate_notion.py
# Verify output locally: check printed page data and updates

Key Takeaways


FAQ

FAQ

How do I find my Notion database ID?

You can find it by sharing the database (use "Copy link"). The Database ID is the part of the URL before the ? query parameter.

Can I automate across multiple Notion databases?

Yes, once the Integration has access to each database via explicit sharing, you can manage multiple databases by referencing their unique IDs.

Why does my query return empty results?

This happens when the database hasn’t been shared with your Integration. Verify the permissions under the "Add Connections" menu.

What is the maximum page size for database queries?

The Notion API supports up to 100 pages per query. Use cursors for paginated results to access more pages.


Official reference: Notion API reference.