mcpField

notion-cli

github.com/4ier生产力1.9k 次安装
代码仓库

关于 notion-cli

notion-cli 是用于操作 Notion API 的命令行工具,适合管理 Notion 工作区的用户与 Agent。它覆盖多类 API 操作,并可输出终端表格或供脚本处理的 JSON。

主要功能

  1. 01搜索页面和数据库
  2. 02创建与管理页面
  3. 03查询和编辑数据库
  4. 04管理区块、评论、用户和文件
  5. 05切换身份验证配置
  6. 06输出表格、JSON 或 Markdown

适用场景

  1. 01查找工作区中的页面或数据库
  2. 02筛选并排序数据库行
  3. 03上传文件并附加到页面
  4. 04使用 JSON 输出自动执行 Notion 操作

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/4ier/notion-cli --skill notion-cli

安装后在 Claude Code 中输入/notion-cli确认已加载。

详细介绍

Notion CLI

notion 是一个用于 Notion API 的 CLI。单个 Go 二进制文件,完整覆盖 API,支持双重输出(面向用户的美观表格,面向 Agent 的 JSON)。当前版本:v0.7.0。

安装

# Homebrew
brew install 4ier/tap/notion-cli

# npm
npm install -g @4ier/notion-cli

# Go
go install github.com/4ier/notion-cli@latest

# Or download a binary from https://github.com/4ier/notion-cli/releases

身份验证

notion auth login --with-token <<< "ntn_xxxxxxxxxxxxx"
notion auth login --with-token --profile work <<< "ntn_xxx"  # named profile
export NOTION_TOKEN=ntn_xxxxxxxxxxxxx                        # env var alternative
notion auth status        # shows workspace + integration type (internal/public)
notion auth switch        # interactive profile picker
notion auth switch work   # direct switch
notion auth doctor        # health check — warns if internal integration

auth status / doctor 会显示集成类型,方便你判断是否需要先共享父页面,再创建工作区根目录内容。

搜索

notion search "query"                    # everything
notion search "query" --type page        # pages only
notion search "query" --type database    # databases only

页面

notion page view <id|url>                # render page content
notion page list                         # list workspace pages
notion page create <parent> --title "X" --body "content"
notion page create <db-id> --db "Name=Review" "Status=Todo"  # database row

# Archive / restore (soft-delete)
notion page archive <id>                 # canonical
notion page trash <id>                   # alias
notion page delete <id>                  # alias (legacy)
notion page restore <id>                 # reverse

# Move / open / edit
notion page move <id> --to <parent>
notion page open <id>                    # open in browser
notion page edit <id|url>                # edit in $EDITOR (markdown round-trip)

# Properties (type-aware)
notion page set <id> Key=Value ...
notion page props <id>                   # show all (summary; paginated values may be truncated)
notion page props <id> <prop-id>         # single raw JSON

# NEW in v0.7: paginated single-property fetch (fixes >25-item truncation)
notion page property <id> <prop-id>
notion page property <id> --name "References"         # resolve id by display name
notion page property <id> <prop-id> --format json

# Relations
notion page link <id> --prop "Rel" --to <target-id>
notion page unlink <id> --prop "Rel" --from <target-id>

# NEW in v0.7: server-side markdown I/O (preferred for full-page dumps)
notion page markdown <id>                # print to stdout
notion page markdown <id> --out page.md  # write to file
notion page markdown <id> --format json  # full response (truncated flag, unknown_block_ids)

notion page set-markdown <id> --file new.md           # replace whole page (default)
cat new.md | notion page set-markdown <id> --file -   # stdin
notion page set-markdown <id> --append --text "\n\n> Appended"
notion page set-markdown <id> --after "Status...pending" --text "Now: done"
notion page set-markdown <id> --range "old...stale" --text "fresh" --allow-deleting-content

page markdown 与 block list --md 的区别:获取完整页面时,优先使用 page markdown——它使用服务端渲染器,能正确处理折叠块、分栏、同步块和数据库形式的页面。只有在需要单个子块时,才使用 block list --md。

数据库

notion db list                           # list databases
notion db view <id>                      # show schema
notion db query <id>                     # all rows
notion db query <id> -F 'Status=Done' -s 'Date:desc'
notion db query <id> --filter-json '{"or":[...]}'
notion db query <id> --all
notion db create <parent> --title "X" --props "Status:select,Date:date"
notion db update <id> --title "New Name" --add-prop "Priority:select"
notion db add <id> "Name=Task" "Status=Todo" "Priority=High"
notion db add-bulk <id> --file items.json
notion db export <id>                    # CSV (default)
notion db export <id> --format json
notion db export <id> --format md -o report.md
notion db open <id>
筛选运算符
语法含义
=等于
!=不等于
> / >=大于(或等于)
< / <=小于(或等于)
~=包含

多个 -F 标志会使用 AND 组合。属性类型会根据架构自动检测。

排序:-s 'Date:desc' 或 -s 'Name:asc'
批量添加文件格式
[{"Name": "Task A", "Status": "Todo"}, {"Name": "Task B", "Status": "Done"}]

块

notion block list <parent-id>            # list child blocks
notion block list <parent-id> --all
notion block list <parent-id> --depth 3  # recursive
notion block list <parent-id> --md       # markdown; prefer 'page markdown' for pages
notion block get <id>

# Append / insert — both handle >100 children and >2000-char code blocks automatically
notion block append <parent> "text"
notion block append <parent> "text" -t bullet
notion block append <parent> "text" -t code --lang ts        # 'ts' / 'sh' / 'yml' etc. normalized
notion block append <parent> --file notes.md                 # any length, auto-batched
notion block append <parent> --file big.md --on-oversize=truncate
notion block insert <parent> "text" --after <block-id>

# Update — now with markdown support
notion block update <id> --text "plain new content"
notion block update <id> --text "See **[docs](https://x.com)**" --markdown
notion block update <id> --file patch.md                     # must parse to exactly one block

notion block delete <id1> [id2] [id3]
notion block move <id> --after <target>
notion block move <id> --before <target>
notion block move <id> --parent <new-parent>

块类型:paragraph/p、h1/h2/h3、bullet、numbered、todo、quote、code、callout、divider。

媒体块(图片 / 文件 / 视频 / 音频 / PDF)
# External URL (http/https)
notion block append <page> --image-url https://example.com/a.png --caption "fig 1"

# Local file → upload + embed in one command
notion block append <page> --image-file ./chart.png --caption "heap usage"
notion block append <page> --pdf-file ./spec.pdf
notion block append <page> --video-file ./demo.mp4

# Reference an existing file_upload id
notion block append <page> --image-upload 351d45fb-... --caption "reused"

image、file、video、audio、pdf 均支持同一组三种参数(--<kind>-url / --<kind>-file / --<kind>-upload)。--caption 适用于所有类型。

评论

notion comment list <page-id>
notion comment add <page-id> --text "comment text"
notion comment add <page-id> --text "with @mention" --mention-user <user-id>
notion comment get <comment-id>
notion comment reply <comment-id> "reply text"          # same thread

# NEW in v0.7
notion comment update <comment-id> --text "edited text"
notion comment update <comment-id> --text "with @mention" --mention-user <user-id>
notion comment delete <comment-id>                      # single or many
notion comment delete <id1> <id2> <id3>                 # variadic

用户

notion user me                           # current bot info
notion user list                         # all workspace users
notion user get <user-id>

文件

# List / retrieve / upload
notion file list
notion file get <upload-id>                                # NEW in v0.7 — status / size / URL
notion file upload ./local/file.pdf                        # local path
notion file upload https://example.com/chart.png --name chart.png   # URL source
curl -sSL https://host/file.zip | notion file upload - --name file.zip   # stdin
notion file upload ./image.png --to <page-id>              # upload + attach to page

原始 API(备用方案)

notion api GET /v1/users/me              # /v1/ is auto-prepended if missing
notion api GET /users/me                 # → prints 'note: prepending /v1...' and works
notion api POST /v1/search --body '{"query":"test"}'
notion api PATCH /v1/pages/<id> --body @body.json        # read body from file
echo '{"query":"x"}' | notion api POST /v1/search --body -  # explicit stdin

输出模式

  • 终端(TTY):彩色表格,格式清晰易读
  • 管道传输 / 脚本调用:自动输出 JSON
  • 显式指定:--format json / --format table / --format md
  • --debug:显示 HTTP 请求 / 响应详情

所有输出都包含完整的 Notion UUID。所有命令都接受 Notion URL 或 ID。

给 Agent 的提示

  • notion db add 和 notion page set 会根据架构自动检测属性类型,因此 Tags=a,b,c(multi_select)和 Done=true(checkbox)都可以直接使用。
  • 处理较长的 markdown 时,优先使用 notion page set-markdown --file,而非 notion block append --file——服务端解析没有 100 个子项的限制。
  • 对于可能包含超过 25 个项目的关系 / 汇总属性,始终使用 notion page property(而不是 page view / page props)。
  • 通过 jq 管道处理:notion db query <id> -F 'Status=Done' --format json | jq '.results[].id'
  • 如果错误信息令人困惑,请查看其中是否有 → 提示行——CLI 会为常见 API 错误补充可执行的后续步骤。
  • 使用内部集成时:不允许创建工作区根目录页面——请先共享父页面,再传入其 ID。

评论

支持 Markdown