name: notion description: 使用Notion API创建、搜索和更新Notion页面/数据库。用于记录工作、生成运行手册和自动化知识库更新。 triggers:
- notion
Notion
<重要> 在执行任何Notion操作之前,首先检查所需的环境变量是否设置:
[ -n "$NOTION_INTEGRATION_KEY" ] && echo "NOTION_INTEGRATION_KEY已设置" || echo "NOTION_INTEGRATION_KEY未设置"
如果缺少,请要求用户提供(或连接Notion集成)再继续:
- NOTION_INTEGRATION_KEY: Notion集成密钥(以
ntn_...开头)
同时确认集成已在Notion中共享给目标页面/数据库。 </重要>
基础头信息
-H "Authorization: Bearer ${NOTION_INTEGRATION_KEY}" \
-H "Notion-Version: 2022-06-28" \
-H "Content-Type: application/json"
查找页面(搜索)
使用Notion的搜索端点通过标题查找页面。
curl -s https://api.notion.com/v1/search \
-H "Authorization: Bearer ${NOTION_INTEGRATION_KEY}" \
-H "Notion-Version: 2022-06-28" \
-H "Content-Type: application/json" \
-d '{
"query": "OpenHands Wiki",
"page_size": 10
}' | jq .
在父页面下创建页面
PARENT_PAGE_ID="<parent_page_id>"
curl -s https://api.notion.com/v1/pages \
-H "Authorization: Bearer ${NOTION_INTEGRATION_KEY}" \
-H "Notion-Version: 2022-06-28" \
-H "Content-Type: application/json" \
-d '{
"parent": {"type": "page_id", "page_id": "'"${PARENT_PAGE_ID}"'"},
"properties": {
"title": {
"title": [{"type": "text", "text": {"content": "My new page"}}]
}
},
"children": [
{
"object": "block",
"type": "paragraph",
"paragraph": {
"rich_text": [{"type": "text", "text": {"content": "Hello from OpenHands."}}]
}
}
]
}' | jq .
向现有页面追加块
使用页面的块ID(与页面ID相同)来追加子块。
PAGE_ID="<page_id>"
curl -s -X PATCH "https://api.notion.com/v1/blocks/${PAGE_ID}/children" \
-H "Authorization: Bearer ${NOTION_INTEGRATION_KEY}" \
-H "Notion-Version: 2022-06-28" \
-H "Content-Type: application/json" \
-d '{
"children": [
{
"object": "block",
"type": "heading_2",
"heading_2": {"rich_text": [{"type": "text", "text": {"content": "Appended section"}}]}
}
]
}' | jq .
提示/注意事项
- 共享是必需的:即使有有效的密钥,集成也无法看到页面/数据库,直到在Notion UI中与集成共享。
- 速率限制:保持请求小;对于大页面,先创建页面,然后分批追加块。
- ID格式:Notion ID可能返回带连字符的形式;在API调用中,带连字符和不带连字符的形式通常都有效。