Skip to main content

A Python client for Notion API

Project description

notion-py

A type-safe Python client library for the Notion API, built with Pydantic v2.

PyPI version Python Version License: MIT

Features

  • Type-Safe: Complete type definitions using Pydantic v2
  • Async-First: Built on httpx for async/await support
  • API 2025-09-03: Latest Notion API with DataSources support
  • Comprehensive: All blocks, properties, filters, and request types
  • Domain Mapping: Built-in mapper for converting to domain models

Installation

# TestPyPI からインストール(依存関係は本番PyPIから取得)
pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ notion-py

Quick Start

import asyncio
from notion_py import NotionAsyncClient

async def main():
    client = NotionAsyncClient(auth="your_notion_api_key")

    # Query a database (API 2025-09-03)
    response = await client.dataSources.query(
        data_source_id="your_database_id"
    )

    for page in response.results:
        print(page.id, page.properties)

asyncio.run(main())

Notion API 2025-09-03

This library supports the latest Notion API version 2025-09-03, which introduces:

  • DataSources: New paradigm replacing the legacy databases endpoint
  • Backward Compatibility: Legacy databases endpoint still supported
  • Migration Path: Seamless transition from databases to dataSources

DataSources vs Databases

# New DataSources API (recommended)
await client.dataSources.query(data_source_id="...")

# Legacy Databases API (still supported)
await client.databases.query(database_id="...")

Both endpoints work identically, but dataSources is the future-proof choice.

Documentation

Full documentation is available at: https://higashi-masafumi.github.io/notion-py/

Core Capabilities

Pages

# Create a page
from notion_py.requests import CreatePageParameters, TitlePropertyRequest

await client.pages.create(
    parameters=CreatePageParameters(
        parent={"database_id": "your_database_id"},
        properties={
            "Name": TitlePropertyRequest(
                title=[{"type": "text", "text": {"content": "New Page"}}]
            )
        }
    )
)

Filters

from notion_py.filters import TextPropertyFilter, CompoundFilter

# Type-safe query filters
filter = CompoundFilter.and_(
    TextPropertyFilter(property="Name", rich_text={"contains": "urgent"}),
    TextPropertyFilter(property="Status", rich_text={"equals": "In Progress"})
)

await client.dataSources.query(
    data_source_id="your_database_id",
    filter=filter
)

Domain Mapping

from notion_py.helpder import NotionMapper, Field
from notion_py.requests.property_requests import (
    TitlePropertyRequest,
    StatusPropertyRequest,
)
from pydantic import BaseModel

class Task(BaseModel):
    id: str
    name: str
    status: str

class TaskMapper(NotionMapper[Task]):
    # Define field descriptors
    name_field = Field(
        notion_name="タスク名",
        parser=lambda p: p.title[0].plain_text if p.title else "",
        request_builder=lambda v: TitlePropertyRequest(
            title=[{"type": "text", "text": {"content": v}}]
        )
    )

    status_field = Field(
        notion_name="ステータス",
        parser=lambda p: p.status.name if p.status else "",
        request_builder=lambda v: StatusPropertyRequest(
            status={"name": v}
        )
    )

    def to_domain(self, notion_page):
        """Convert Notion page to domain model."""
        return Task(
            id=notion_page.id,
            name=self.name_field.parse(notion_page.properties["タスク名"]),
            status=self.status_field.parse(notion_page.properties["ステータス"]),
        )

# Use the mapper
mapper = TaskMapper()
task = mapper.to_domain(notion_page)

Requirements

  • Python >= 3.10
  • Pydantic >= 2.11.10

License

MIT License - see LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

notion_py_client-0.1.2.tar.gz (75.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

notion_py_client-0.1.2-py3-none-any.whl (95.8 kB view details)

Uploaded Python 3

File details

Details for the file notion_py_client-0.1.2.tar.gz.

File metadata

  • Download URL: notion_py_client-0.1.2.tar.gz
  • Upload date:
  • Size: 75.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for notion_py_client-0.1.2.tar.gz
Algorithm Hash digest
SHA256 0a02ed32e8cdaebfb906c94795281cd325042856f2e95b9f3d39d8ab3e70cdc8
MD5 34eebeedc3b430cb8f4dea9412a05600
BLAKE2b-256 fbf8a21e95c0295e08b28b3c3ce6eb23c31e706769a7dfa39f9e61a96c9e6d74

See more details on using hashes here.

File details

Details for the file notion_py_client-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for notion_py_client-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 34d2e10565db9d8e73112975f1ac6bb038026cb690de167367df5dd46f395bc3
MD5 0d435f33026a0a799c07df954c74ad59
BLAKE2b-256 bacc2efb230243d71b5b0c70f9ca056c6a91afa13ea830ab2bdf2161d807a7c8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page