Communitygithub.com

Notion API技能包

通过ntn CLI或HTTP+curl与Notion交互,支持页面、数据库、Markdown和Workers。

Notion API技能包 是什么?

该技能提供两种方式与Notion API交互:推荐的ntn CLI(macOS/Linux)和通用的HTTP+curl(包括Windows)。核心功能包括读写页面、查询数据库、上传文件以及通过Notion Workers部署自定义代理工具和Webhook。工作流基于同一个集成令牌,通过`NOTION_API_KEY`环境变量配置。对于AI代理场景,`/markdown`端点可将页面内容转换为Markdown格式,便于模型直接消费。高级功能Notion Workers允许使用TypeScript编写同步任务、工具和Webhook,但需要Business或Enterprise计划。

兼容平台✓Claude Code✓Codex CLI~Cursor
npx skills add https://github.com/Lord1Egypt/awesome-skill-forge/tree/HEAD/skills/notion

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

Notion

通过两种方式与Notion对话。两者使用相同的集成令牌 — 根据可用性选择。

◆ ntn CLI — Notion 官方 CLI。语法更简短,支持单行文件上传,Workers 必需。截止 2026 年 5 月仅支持 macOS + Linux(Windows 支持“即将推出”)。默认方式(已安装时)。 ◆ HTTP + curl — 跨平台,包括 Windows。默认回退方式(当 ntn 未安装时)。

设置

1. 获取集成令牌(两条路径均必需)

  1. 在 https://notion.so/my-integrations 创建一个集成
  2. 复制 API 密钥(以 ntn_ 或 secret_ 开头)
  3. 存储在 ~/.hermes/.env 中:
    NOTION_API_KEY=ntn_your_key_here
    
  4. 将目标页面/数据库与集成共享:在 Notion 中,页面菜单 ... → 连接到 → 你的集成名称。否则 API 会返回 404(即使页面存在)。

2. 安装 ntn(macOS / Linux 上的首选路径)

# 推荐
curl -fsSL https://ntn.dev | bash

# 或通过 npm(需要 Node 22+,npm 10+)
npm install --global ntn

ntn --version    # 验证

跳过 ntn login — 直接使用集成令牌。 这样无需浏览器即可无头运行:

export NOTION_API_TOKEN=$NOTION_API_KEY      # ntn 读取 NOTION_API_TOKEN
export NOTION_KEYRING=0                       # 不要尝试使用操作系统密钥环

将这些导出语句添加到你的 shell 配置文件(或 ~/.hermes/.env)中,以便每个会话继承它们。

3. 运行时选择路径

if command -v ntn >/dev/null 2>&1; then
  # 使用 ntn
else
  # 回退到 curl
fi

Windows 用户:在原生 ntn 发布之前完全跳过第二步 — 路径 B 完美运行。如果你现在就想使用 CLI 的便利性,可以在 WSL2 中安装 ntn。

API 基础

所有 HTTP 请求都需要 Notion-Version: 2025-09-03。ntn 会为你处理。在此版本中,用户所说的“数据库”在 API 中被称为数据源。

路径 A — ntn CLI(推荐,macOS / Linux)

原生 API 调用(curl 的简写)

ntn api v1/users                                  # GET
ntn api v1/pages parent[page_id]=abc123 \         # POST 内联 body
  properties[title][0][text][content]="Notes"
ntn api v1/pages/abc123 -X PATCH archived:=true   # PATCH;:= 表示非字符串(布尔/数字/null)

语法说明:

  • key=value — 字符串字段
  • key[nested]=value — 嵌套对象字段
  • key:=value — 类型化赋值(布尔、数字、null、数组)

搜索

ntn api v1/search query="page title"

读取页面元数据

ntn api v1/pages/{page_id}

将页面读作 Markdown(AI 代理友好)

ntn api v1/pages/{page_id}/markdown

读取页面内容为块

ntn api v1/blocks/{page_id}/children

从 Markdown 创建页面

ntn api v1/pages \
  parent[page_id]=xxx \
  properties[title][0][text][content]="会议笔记" \
  markdown="# 议程\n\n- Q3 路线图\n- 招聘"

使用 Markdown 修补页面

ntn api v1/pages/{page_id}/markdown -X PATCH \
  markdown="## 更新\n\n原型已发布。"

查询数据库(数据源)

ntn api v1/data_sources/{data_source_id}/query -X POST \
  filter[property]=Status filter[select][equals]=Active

对于包含 sorts、多个过滤子句或复合逻辑的复杂查询,通过管道传入 JSON:

echo '{"filter": {"property": "Status", "select": {"equals": "Active"}}, "sorts": [{"property": "Date", "direction": "descending"}]}' | \
  ntn api v1/data_sources/{data_source_id}/query -X POST --json -

文件上传(单行命令 — CLI 的最大优势)

ntn files create < photo.png
ntn files create --external-url https://example.com/photo.png
ntn files list

与三步 HTTP 流程(创建上传 → PUT 字节 → 引用)相比,这是巨大的简化。

有用的环境变量

变量效果
NOTION_API_TOKEN认证令牌(覆盖密钥环)— 将其设置为你的集成令牌
NOTION_KEYRING=0使用基于文件的凭证 ~/.config/notion/auth.json 替代 OS 密钥环
NOTION_WORKSPACE_ID跳过工作区选择器提示

路径 B — HTTP + curl(跨平台,Windows 默认)

所有请求共享此模式:

curl -s -X GET "https://api.notion.com/v1/..." \
  -H "Authorization: Bearer $NOTION_API_KEY" \
  -H "Notion-Version: 2025-09-03" \
  -H "Content-Type: application/json"

在 Windows 上,Windows 10+ 自带的 curl 可直接使用。PowerShell 用户也可以使用 Invoke-RestMethod。

搜索

curl -s -X POST "https://api.notion.com/v1/search" \
  -H "Authorization: Bearer $NOTION_API_KEY" \
  -H "Notion-Version: 2025-09-03" \
  -H "Content-Type: application/json" \
  -d '{"query": "page title"}'

读取页面元数据

curl -s "https://api.notion.com/v1/pages/{page_id}" \
  -H "Authorization: Bearer $NOTION_API_KEY" \
  -H "Notion-Version: 2025-09-03"

将页面读作 Markdown(AI 代理友好)

相比块 JSON,更易于提供给模型。

curl -s "https://api.notion.com/v1/pages/{page_id}/markdown" \
  -H "Authorization: Bearer $NOTION_API_KEY" \
  -H "Notion-Version: 2025-09-03"

读取页面内容为块(当需要结构时)

curl -s "https://api.notion.com/v1/blocks/{page_id}/children" \
  -H "Authorization: Bearer $NOTION_API_KEY" \
  -H "Notion-Version: 2025-09-03"

从 Markdown 创建页面

POST /v1/pages 接受一个 markdown 请求体参数。

curl -s -X POST "https://api.notion.com/v1/pages" \
  -H "Authorization: Bearer $NOTION_API_KEY" \
  -H "Notion-Version: 2025-09-03" \
  -H "Content-Type: application/json" \
  -d '{
    "parent": {"page_id": "xxx"},
    "properties": {"title": [{"text": {"content": "会议笔记"}}]},
    "markdown": "# 议程\n\n- Q3 路线图\n- 招聘\n\n## 决定\n- 周五发布 MVP"
  }'

使用 Markdown 修补页面

curl -s -X PATCH "https://api.notion.com/v1/pages/{page_id}/markdown" \
  -H "Authorization: Bearer $NOTION_API_KEY" \
  -H "Notion-Version: 2025-09-03" \
  -H "Content-Type: application/json" \
  -d '{"markdown": "## 更新\n\n原型已发布。"}'

在数据库中创建页面(类型化属性)

curl -s -X POST "https://api.notion.com/v1/pages" \
  -H "Authorization: Bearer $NOTION_API_KEY" \
  -H "Notion-Version: 2025-09-03" \
  -H "Content-Type: application/json" \
  -d '{
    "parent": {"database_id": "xxx"},
    "properties": {
      "Name": {"title": [{"text": {"content": "新项目"}}]},
      "Status": {"select": {"name": "Todo"}}
    }
  }'

查询数据库(数据源)

curl -s -X POST "https://api.notion.com/v1/data_sources/{data_source_id}/query" \
  -H "Authorization: Bearer $NOTION_API_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 -s -X POST "https://api.notion.com/v1/data_sources" \
  -H "Authorization: Bearer $NOTION_API_KEY" \
  -H "Notion-Version: 2025-09-03" \
  -H "Content-Type: application/json" \
  -d '{
    "parent": {"page_id": "xxx"},
    "title": [{"text": {"content": "我的数据库"}}],
    "properties": {
      "Name": {"title": {}},
      "Status": {"select": {"options": [{"name": "Todo"}, {"name": "Done"}]}},
      "Date": {"date": {}}
    }
  }'

更新页面属性

curl -s -X PATCH "https://api.notion.com/v1/pages/{page_id}" \
  -H "Authorization: Bearer $NOTION_API_KEY" \
  -H "Notion-Version: 2025-09-03" \
  -H "Content-Type: application/json" \
  -d '{"properties": {"Status": {"select": {"name": "Done"}}}}'

向页面追加块

curl -s -X PATCH "https://api.notion.com/v1/blocks/{page_id}/children" \
  -H "Authorization: Bearer $NOTION_API_KEY" \
  -H "Notion-Version: 2025-09-03" \
  -H "Content-Type: application/json" \
  -d '{
    "children": [
      {"object": "block", "type": "paragraph", "paragraph": {"rich_text": [{"text": {"content": "来自 Hermes 的问候!"}}]}}
    ]
  }'

文件上传(三步流程)

# 1. 创建上传
curl -s -X POST "https://api.notion.com/v1/file_uploads" \
  -H "Authorization: Bearer $NOTION_API_KEY" \
  -H "Notion-Version: 2025-09-03" \
  -H "Content-Type: application/json" \
  -d '{"filename": "photo.png", "content_type": "image/png"}'

# 2. 将字节 PUT 到上方返回的 upload_url
curl -s -X PUT "{upload_url}" --data-binary @photo.png

# 3. 在页面/块负载中引用 {file_upload_id}

属性类型

数据库项的常见属性格式:

  • 标题(Title): {"title": [{"text": {"content": "..."}}]}
  • 富文本(Rich text): {"rich_text": [{"text": {"content": "..."}}]}
  • 单选(Select): {"select": {"name": "Option"}}
  • 多选(Multi-select): {"multi_select": [{"name": "A"}, {"name": "B"}]}
  • 日期(Date): {"date": {"start": "2026-01-15", "end": "2026-01-16"}}
  • 复选框(Checkbox): {"checkbox": true}
  • 数字(Number): {"number": 42}
  • URL: {"url": "https://..."}
  • 邮箱(Email): {"email": "[email protected]"}
  • 关联(Relation): {"relation": [{"id": "page_id"}]}

API 版本 2025-09-03 — 数据库与数据源

  • 数据库变成了数据源。 使用 /data_sources/ 端点进行查询和检索。
  • 每个数据库有两个 ID: database_id 和 data_source_id。
    • 创建页面时使用 database_id:parent: {"database_id": "..."}
    • 查询时使用 data_source_id:POST /v1/data_sources/{id}/query
  • 搜索结果中数据库的 "object" 返回为 "data_source",并包含 data_source_id 字段。

Notion Workers(高级,需要 ntn)

Workers 是 Notion 为你托管的 TypeScript 程序。一个 Worker 可以暴露以下任意组合:

  • 同步(Syncs) — 按计划(默认 30 分钟)从外部 API 拉取数据到 Notion 数据库。
  • 工具(Tools) — 在 Notion 的自定义代理内作为可调用工具出现。
  • Webhooks — 接收外部服务(GitHub、Stripe 等)的 HTTP 事件并在 Notion 中执行操作。

计划 / 平台限制:

  • CLI 在所有计划上可用。部署 Workers 需要 Business 或 Enterprise 计划。
  • 截止 2026 年 5 月,ntn 仅支持 macOS/Linux。Windows 用户需要使用 WSL2 或等待原生支持。
  • 截至 2026 年 8 月 11 日免费;之后按 Notion 积分计费。

最小 Worker 示例

ntn workers new my-worker      # 脚手架
cd my-worker
# 编辑 src/index.ts
ntn workers deploy --name my-worker

src/index.ts:

import { Worker } from "@notionhq/workers";

const worker = new Worker();
export default worker;

worker.tool("greet", {
  title: "问候用户",
  description: "返回一个友好的问候",
  inputSchema: { type: "object", properties: { name: { type: "string" } }, required: ["name"] },
  execute: async ({ name }) => `你好,${name}!`,
});

Webhook 能力

worker.webhook("onGithubPush", {
  title: "GitHub 推送处理器",
  execute: async (events, { notion }) => {
    for (const event of events) {
      // event.body, event.rawBody(用于签名验证), event.headers
      console.log("收到交付", event.deliveryId);
    }
  },
});

部署后:ntn workers webhooks list 显示 Notion 生成的 URL。将该 URL 视为秘密 — 除非你添加签名验证,否则任何知道该 URL 的人都可以 POST 事件。

Worker 生命周期命令

ntn workers deploy
ntn workers list
ntn workers exec <capability-key> -d '{"name": "world"}'
ntn workers sync trigger <key>            # 立即运行同步
ntn workers sync pause <key>
ntn workers env set GITHUB_WEBHOOK_SECRET=...
ntn workers runs list                     # 最近调用
ntn workers runs logs <run-id>
ntn workers webhooks list

当需要构建 Worker 时,使用 ntn workers new 创建脚手架,在 src/index.ts 中编写代码,使用 ntn workers env set 设置任何秘密,然后部署。Notion 的文档 https://developers.notion.com/workers 涵盖了完整的 API 表面。

Notion 风格 Markdown(由 /markdown 端点使用)

标准 CommonMark 加上用于 Notion 特定块的类 XML 标签。使用制表符进行缩进。

超出 CommonMark 的块:

<callout icon="🎯" color="blue_bg">
	在 **周五** 之前发布 MVP。
</callout>

<details color="gray">
<summary>切换标题</summary>
	子项缩进一个制表符
</details>

<columns>
	<column>左侧</column>
	<column>右侧</column>
</columns>

<table_of_contents color="gray"/>

行内:

  • 提及:<mention-user url="..."/>, <mention-page url="...">标题</mention-page>, <mention-date start="2026-05-15"/>
  • 下划线:<span underline="true">文本</span>
  • 颜色:<span color="blue">文本</span> 或块级在第一行使用 {color="blue"}
  • 数学公式:行内 $x^2$,块级 $$ ... $$
  • 引用:[^https://example.com]

颜色: gray brown orange yellow green blue purple pink red,加上用于背景的 *_bg 变体。

标题 5/6 会折叠为 H4。多个 > 行会渲染为单独的引用块 — 对于多行引用,请在单个 > 内部使用 <br>。

选择正确的路径

任务Mac / LinuxWindows
读写页面、搜索、查询数据库ntn api ...curl
读取页面供 AI 代理总结ntn api v1/pages/{id}/markdowncurl /markdown 端点
上传文件ntn files create < file三步 HTTP 流程
一次性 API 探索ntn api ...curl
构建由 Notion 托管的同步/Webhook/代理工具ntn workers ...WSL2 + ntn workers ...

注意事项

  • 页面/数据库 ID 是 UUID(带或不带短横线均可)。
  • 速率限制:约 3 请求/秒。CLI 不会绕过此限制。
  • API 无法设置数据库视图过滤器 — 这仅限 UI 操作。
  • 创建数据源时使用 "is_inline": true 以将其嵌入页面。
  • 始终向 curl 传递 -s 以抑制进度条(更清晰的代理输出)。
  • 读取时通过管道将 JSON 传入 jq:... | jq '.results[0].properties'。
  • Notion 现在还提供 MCP 服务器(Notion MCP),但在本技能文档中不展开讨论。

以及…一切皆知

当然。所有 API 调用都使用相同的基础 URL https://api.notion.com/v1/。

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

Lord1Egypt/awesome-skill-forge

Generate a conversion-focused landing page that compares offers, pricing cues, and trust signals for a insurance agency. Use when handling offer...

Lord1Egypt/awesome-skill-forge

Generates a complete landing page with copy, layout, hero image, and responsive design from a text description. Use when launching a product quickly.

Lord1Egypt/awesome-skill-forge

Generate a conversion-focused landing page that compares offers, pricing cues, and trust signals for a family law practice. Use when handling offer...

Lord1Egypt/awesome-skill-forge

Generate a conversion-focused landing page that compares offers, pricing cues, and trust signals for a fractional CFO firm. Use when handling offer...

Lord1Egypt/awesome-skill-forge

Generate a conversion-focused landing page that compares offers, pricing cues, and trust signals for a home remodeling company. Use when handling offer...

Lord1Egypt/awesome-skill-forge

Generate a conversion-focused landing page that compares offers, pricing cues, and trust signals for a HVAC service company. Use when handling offer...

Lord1Egypt/awesome-skill-forge

Generate a conversion-focused landing page that compares offers, pricing cues, and trust signals for a immigration law firm. Use when handling offer...

Lord1Egypt/awesome-skill-forge

Generate a conversion-focused landing page that compares offers, pricing cues, and trust signals for a CPA tax advisory firm. Use when handling offer...

Lord1Egypt/awesome-skill-forge

Generate a conversion-focused landing page that compares offers, pricing cues, and trust signals for a estate planning law firm. Use when handling...

Lord1Egypt/awesome-skill-forge

Fastest way to create or update a pawr.link profile. One curl command, $19 USDC to create, $0.10 to update. Patch-style update-links endpoint lets you add, remove, or move individual links without replacing the whole profile.

Lord1Egypt/awesome-skill-forge

Use this skill when the user wants to review today, look back on yesterday, reflect on this week, or wrap things up with a gentle, low-pressure reflection.

Lord1Egypt/awesome-skill-forge

Read-only queue dashboard for workspace-aware ship. (gstack)

Lord1Egypt/awesome-skill-forge

Landing page quality assessment for paid advertising campaigns. Evaluates message match, page speed, mobile experience, trust signals, form optimization, and conversion rate potential. Use when user says landing page, post-click experience, landing page audit, LP audit, conversion rate, landing page CRO, post-click CRO, or landing page optimization.

Lord1Egypt/awesome-skill-forge

Create HTML-based video compositions, animated title cards, social overlays, captioned talking-head videos, audio-reactive visuals, and shader transitions using HyperFrames. HTML is the source of truth for video. Use when the user wants a rendered MP4/WebM from an HTML composition, wants to animate text/logos/charts over media, needs captions synced to audio, wants TTS narration, or wants to convert a website into a video.

Lord1Egypt/awesome-skill-forge

Search arXiv papers by keyword, author, category, or ID.

Lord1Egypt/awesome-skill-forge

Delegate coding to OpenAI Codex CLI (features, PRs).

Lord1Egypt/awesome-skill-forge

Track Apple devices/AirTags via FindMy.app on macOS.

Lord1Egypt/awesome-skill-forge

Jailbreak LLMs: Parseltongue, GODMODE, ULTRAPLINIAN.

Lord1Egypt/awesome-skill-forge

Geocode, POIs, routes, timezones via OpenStreetMap/OSRM.

Lord1Egypt/awesome-skill-forge

p5.js sketches: gen art, shaders, interactive, 3D.

相关技能