DEV Community

Alex Spinov
Alex Spinov

Posted on

Notion Has a Free API — Create Pages, Query Databases, and Automate Your Workspace

Notion is where I manage everything — notes, projects, clients, content calendar. But I was doing everything manually until I discovered their API.

Now I automate 80% of my Notion workflow with simple scripts.


Getting Started (3 minutes)

  1. Go to notion.so/my-integrations
  2. Create a new integration
  3. Copy the API token
  4. Share your database with the integration (click Share in Notion, add your integration)

Query a Database

import requests

NOTION_TOKEN = 'your_token'
DATABASE_ID = 'your_database_id'

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

def query_database(database_id, filter_obj=None):
    url = f'https://api.notion.com/v1/databases/{database_id}/query'
    payload = {}
    if filter_obj:
        payload['filter'] = filter_obj

    response = requests.post(url, headers=headers, json=payload)
    return response.json()['results']

# Get all items
items = query_database(DATABASE_ID)
for item in items:
    props = item['properties']
    title = props.get('Name', {}).get('title', [{}])[0].get('plain_text', 'Untitled')
    print(title)
Enter fullscreen mode Exit fullscreen mode

Create a New Page

def create_page(database_id, title, status='Not Started', tags=None):
    url = 'https://api.notion.com/v1/pages'

    properties = {
        'Name': {
            'title': [{'text': {'content': title}}]
        },
        'Status': {
            'select': {'name': status}
        }
    }

    if tags:
        properties['Tags'] = {
            'multi_select': [{'name': tag} for tag in tags]
        }

    payload = {
        'parent': {'database_id': database_id},
        'properties': properties
    }

    response = requests.post(url, headers=headers, json=payload)
    return response.json()

# Create a task
create_page(DATABASE_ID, 'Write blog post about Notion API', 'In Progress', ['writing', 'api'])
Enter fullscreen mode Exit fullscreen mode

Add Content to a Page

def add_content(page_id, blocks):
    url = f'https://api.notion.com/v1/blocks/{page_id}/children'
    response = requests.patch(url, headers=headers, json={'children': blocks})
    return response.json()

# Add a heading and paragraph
blocks = [
    {
        'type': 'heading_2',
        'heading_2': {
            'rich_text': [{'text': {'content': 'Meeting Notes'}}]
        }
    },
    {
        'type': 'paragraph',
        'paragraph': {
            'rich_text': [{'text': {'content': 'Discussed Q2 roadmap and priorities.'}}]
        }
    },
    {
        'type': 'to_do',
        'to_do': {
            'rich_text': [{'text': {'content': 'Follow up with design team'}}],
            'checked': False
        }
    }
]

add_content('your_page_id', blocks)
Enter fullscreen mode Exit fullscreen mode

Real Automations I Use

1. Auto-create tasks from email:
Script scans inbox, creates Notion page for each task-like email.

2. Content calendar automation:
When I publish on Dev.to, a script auto-updates the Notion database with title, URL, and publish date.

3. Daily standup generator:
Script queries "In Progress" items and generates a summary.

4. Client project tracker:
New client email triggers a Notion page with template properties.


Filter and Sort

# Get only 'In Progress' items, sorted by date
filter_obj = {
    'property': 'Status',
    'select': {'equals': 'In Progress'}
}

in_progress = query_database(DATABASE_ID, filter_obj)
print(f'{len(in_progress)} tasks in progress')
Enter fullscreen mode Exit fullscreen mode

Tips

  1. Notion Version header is required — always include Notion-Version: 2022-06-28
  2. Share databases with your integration — most common mistake is forgetting this
  3. Property names are case-sensitive — "Name" is not "name"
  4. Rate limit: 3 requests/second — add a small delay in loops
  5. Pagination — results over 100 items require start_cursor

What would you automate in Notion?

I have automated task creation, content tracking, and standup reports. What repetitive Notion task do you wish you could automate? I might build it and write about it.


I write about APIs, automation, and developer productivity. Follow for practical tutorials.


More from me: 10 Dev Tools I Use Daily | 77 Scrapers on a Schedule | 150+ Free APIs

Top comments (0)