--- name: notion-api description: Use Notion API Python SDK to create pages, append blocks, and manage content in Notion. Triggers on "Notion", "同步到Notion", "notion API". --- # Notion API Python SDK Create and populate Notion pages programmatically. ## Setup ```bash pip install notion-client ``` Set environment variables: `NOTION_API_KEY` (integration token), `NOTION_PAGE_ID` (parent page ID). ## Create Page ```python from notion_client import NotionClient import os client = NotionClient(auth=os.environ["NOTION_API_KEY"]) page = client.pages.create( parent={"page_id": os.environ["NOTION_PAGE_ID"], "type": "page_id"}, properties={ "title": [{"type": "text", "text": {"content": "Title"}}] } ) page_id = page["id"] ``` ## Append Blocks ```python client.blocks.children.append(block_id=page_id, children=[ {"type": "heading_2", "heading_2": {"rich_text": [{"type": "text", "text": {"content": "Section"}}]}}, {"type": "paragraph", "paragraph": {"rich_text": [{"type": "text", "text": {"content": "Text."}}]}}, {"type": "code", "code": {"language": "python", "rich_text": [{"type": "text", "text": {"content": "code"}}]}}, ]) ``` ## Column Layout Content goes in nested `children` of `column`/`column_2` inside `column_list`: ```python {"type": "column_list", "column_list": {"children": [ {"type": "column", "column": {"children": [left_block]}}, {"type": "column", "column": {"children": [right_block]}}, ]}} ``` ## Block Types - `heading_1/2/3`, `paragraph`, `code` (with `language`), `bulleted_list_item`, `numbered_list_item` - `column_list` / `column`, `bookmark`, `divider`, `to_do`, `callout` - Rich text: `{"type": "text", "text": {"content": "..."}, "annotations": {"bold": true}}` ## Database Pages Properties must match the database schema exactly — unknown keys cause `ValidationError`.