Install
openclaw skills install openclaw-notion-apiNotion API 用于创建和管理页面、数据库和块。包含正确的图片、文件上传功能
openclaw skills install openclaw-notion-api使用 Notion API 创建/读取/更新页面、数据源(数据库)和块。
ntn_ 或 secret_ 开头)mkdir -p ~/.config/notion
echo "ntn_your_key_here" > ~/.config/notion/api_key
所有请求都需要:
NOTION_KEY=$(cat ~/.config/notion/api_key)
curl -X GET "https://api.notion.com/v1/..." \
-H "Authorization: Bearer $NOTION_KEY" \
-H "Notion-Version: 2025-09-03" \
-H "Content-Type: application/json"
注意:
Notion-Versionheader 是必需的。本技能使用2025-09-03(最新版本)。在此版本中,数据库在 API 中称为"数据源"。
搜索页面和数据源:
curl -X POST "https://api.notion.com/v1/search" \
-H "Authorization: Bearer $NOTION_KEY" \
-H "Notion-Version: 2025-09-03" \
-H "Content-Type: application/json" \
-d '{"query": "page title"}'
获取页面:
curl "https://api.notion.com/v1/pages/{page_id}" \
-H "Authorization: Bearer $NOTION_KEY" \
-H "Notion-Version: 2025-09-03"
获取页面内容(块):
curl "https://api.notion.com/v1/blocks/{page_id}/children" \
-H "Authorization: Bearer $NOTION_KEY" \
-H "Notion-Version: 2025-09-03"
在数据源中创建页面:
curl -X POST "https://api.notion.com/v1/pages" \
-H "Authorization: Bearer $NOTION_KEY" \
-H "Notion-Version: 2025-09-03" \
-H "Content-Type: application/json" \
-d '{
"parent": {"database_id": "xxxxxx"},
"properties": {
"Name": {"title": [{"text": {"content": "New Item"}}]},
"Status": {"select": {"name": "Todo"}}
}
}'
查询数据源(数据库):
curl -X POST "https://api.notion.com/v1/data_sources/{data_source_id}/query" \
-H "Authorization: Bearer $NOTION_KEY" \
-H "Notion-Version: 2025-09-03" \
-H "Content-Type: application/json" \
-d '{
"filter": {"property": "Status", "select": {"equals": "Active"}},
"sorts": [{"property": "Date", "direction": "descending"}]
}'
创建数据源(数据库):
curl -X POST "https://api.notion.com/v1/data_sources" \
-H "Authorization: Bearer $NOTION_KEY" \
-H "Notion-Version: 2025-09-03" \
-H "Content-Type: application/json" \
-d '{
"parent": {"page_id": "xxx"},
"title": [{"text": {"content": "My Database"}}],
"properties": {
"Name": {"title": {}},
"Status": {"select": {"options": [{"name": "Todo"}, {"name": "Done"}]}},
"Date": {"date": {}}
}
}'
更新页面属性:
curl -X PATCH "https://api.notion.com/v1/pages/{page_id}" \
-H "Authorization: Bearer $NOTION_KEY" \
-H "Notion-Version: 2025-09-03" \
-H "Content-Type: application/json" \
-d '{"properties": {"Status": {"select": {"name": "Done"}}}}'
向页面添加块:
curl -X PATCH "https://api.notion.com/v1/blocks/{page_id}/children" \
-H "Authorization: Bearer $NOTION_KEY" \
-H "Notion-Version: 2025-09-03" \
-H "Content-Type: application/json" \
-d '{
"children": [
{"type": "paragraph", "paragraph": {"rich_text": [{"text": {"content": "Hello"}}]}}
]
}'
数据库项的常用属性格式:
{"title": [{"text": {"content": "..."}}]}{"rich_text": [{"text": {"content": "..."}}]}{"select": {"name": "Option"}}{"multi_select": [{"name": "A"}, {"name": "B"}]}{"date": {"start": "2024-01-15", "end": "2024-01-16"}}{"checkbox": true}{"number": 42}{"url": "https://..."}{"email": "a@b.com"}{"relation": [{"id": "page_id"}]}/data_sources/ 端点进行查询和检索database_id 和 data_source_id
database_id(parent: {"database_id": "..."})data_source_id(POST /v1/data_sources/{id}/query)"object": "data_source" 形式返回,并带有其 data_source_idparent.data_source_id 以及 parent.database_idGET /v1/data_sources/{data_source_id}使用 Direct Upload 方法上传图片或文件到 Notion(文件大小不超过 20MB)。两者的步骤完全相同,仅在 Step 3 的块类型上有区别。
创建上传对象以获取上传 URL:
curl --request POST \
--url 'https://api.notion.com/v1/file_uploads' \
-H "Authorization: Bearer $NOTION_KEY" \
-H 'Content-Type: application/json' \
-H 'Notion-Version: 2025-09-03' \
--data '{}'
响应包含:
id:文件上传 ID(在步骤 3 中使用)upload_url:上传文件内容的 URL使用 multipart/form-data 上传实际的文件:
curl --request POST \
--url 'https://api.notion.com/v1/file_uploads/{file_upload_id}/send' \
-H "Authorization: Bearer $NOTION_KEY" \
-H 'Notion-Version: 2025-09-03' \
-H 'Content-Type: multipart/form-data' \
-F "file=@/path/to/file.png"
重要:
POST 方法(不是 PUT)Authorization 和 Notion-Version headers-F 进行 multipart/form-data将上传的文件作为块添加。根据文件类型选择不同的块类型:
上传图片:
curl --request PATCH \
--url "https://api.notion.com/v1/blocks/{page_id}/children" \
-H "Authorization: Bearer $NOTION_KEY" \
-H 'Content-Type: application/json' \
-H 'Notion-Version: 2025-09-03' \
--data '{
"children": [
{
"type": "image",
"image": {
"type": "file_upload",
"file_upload": {
"id": "{file_upload_id}"
}
}
}
]
}'
上传文件:
curl --request PATCH \
--url "https://api.notion.com/v1/blocks/{page_id}/children" \
-H "Authorization: Bearer $NOTION_KEY" \
-H 'Content-Type: application/json' \
-H 'Notion-Version: 2025-09-03' \
--data '{
"children": [
{
"type": "file",
"file": {
"type": "file_upload",
"file_upload": {
"id": "{file_upload_id}"
}
}
}
]
}'
注意: 不要在块定义中包含 "object": "block"。
POST 方法和正确的 URL 格式Authorization 和 Notion-Version headersis_inline: true 以将其嵌入页面中