关于 n8n-cli
n8n-cli 是通过 REST API 管理 n8n 实例的命令行工具。它支持 JSON 输出和筛选,可组合用于脚本及 LLM 工具调用。
主要功能
- 01管理工作流、执行记录和凭据
- 02管理项目、用户、标签和变量
- 03操作数据表及其行
- 04通过 Git 连接推广项目
- 05支持 JSON 输出和 jq 风格筛选
- 06执行安全审计并拉取源代码控制
适用场景
- 01在脚本中列出并筛选工作流
- 02检查失败执行的数据
- 03在实例间迁移项目更改
- 04审计凭据和节点
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ n8n-io/ n8n --skill n8n-cli安装后在 Claude Code 中输入/n8n-cli确认已加载。
详细介绍
n8n CLI
n8n-cli 命令行工具通过 REST API 管理 n8n 实例。
它会自动检测管道输出并切换为 JSON,使其可组合用于脚本和 LLM 工具调用。
设置
# Interactive login (saves to ~/.n8n-cli/config.json)
n8n-cli login
# Or configure directly
n8n-cli config set-url https://my-instance.n8n.cloud
n8n-cli config set-api-key n8n_api_...
# Or use environment variables (no config file needed)
export N8N_URL=https://my-instance.n8n.cloud
export N8N_API_KEY=n8n_api_...
全局标志
每条命令都支持以下标志:
| 标志 | 简写 | 说明 |
|---|---|---|
--url | -u | 实例 URL(覆盖配置/环境变量) |
--apiKey | -k | API 密钥(覆盖配置/环境变量) |
--format | -f | 输出格式:table、json、id-only |
--json | --format=json 的简写 | |
--jq | jq 风格的筛选器(隐含 --json),例如 '.[0].id'、'.[].name' | |
--quiet | -q | 禁止输出 |
--no-header | 隐藏表格标题(用于 awk/cut 解析) | |
--debug | 将 HTTP 详情输出到 stderr |
自动使用 JSON: 当 stdout 被管道重定向(而非 TTY)时,输出会自动默认为 JSON。
工作流
# List all workflows
n8n-cli workflow list
# Filter workflows
n8n-cli workflow list --active
n8n-cli workflow list --tag=production
n8n-cli workflow list --name="My Workflow"
n8n-cli workflow list --limit=5
# Get a single workflow (full JSON with nodes and connections)
n8n-cli workflow get <id>
# Extract just node names from a workflow
n8n-cli workflow get <id> --jq '.nodes[].name'
# Create a workflow from JSON
n8n-cli workflow create --file=workflow.json
cat workflow.json | n8n-cli workflow create --stdin
# Update a workflow
n8n-cli workflow update <id> --file=updated.json
cat updated.json | n8n-cli workflow update <id> --stdin
# Activate / deactivate
n8n-cli workflow activate <id>
n8n-cli workflow deactivate <id>
# Delete a workflow
n8n-cli workflow delete <id>
# Transfer to another project
n8n-cli workflow transfer <id> --project=<projectId>
# List tags on a workflow
n8n-cli workflow tags <id>
执行记录
# List recent executions
n8n-cli execution list
n8n-cli execution list --workflow=<id> --status=error --limit=10
# status options: canceled, error, running, success, waiting
# Get execution details
n8n-cli execution get <id>
n8n-cli execution get <id> --include-data # includes full node I/O
# Retry a failed execution
n8n-cli execution retry <id>
# Stop a running execution
n8n-cli execution stop <id>
# Delete an execution
n8n-cli execution delete <id>
凭据
# List credentials
n8n-cli credential list
# Get credential metadata (not secrets)
n8n-cli credential get <id>
# Get the schema for a credential type (shows required fields)
n8n-cli credential schema notionApi
n8n-cli credential schema slackOAuth2Api
# Create a credential
n8n-cli credential create --type=notionApi --name='My Notion' --data='{"apiKey":"..."}'
n8n-cli credential create --type=notionApi --name='My Notion' --file=cred.json
cat cred.json | n8n-cli credential create --type=notionApi --name='My Notion' --stdin
# Delete / transfer
n8n-cli credential delete <id>
n8n-cli credential transfer <id> --project=<projectId>
提示: 创建凭据前,使用 credential schema <type> 查看所需字段。
项目
n8n-cli project list
n8n-cli project get <id>
n8n-cli project create --name="My Project"
n8n-cli project update <id> --name="New Name"
n8n-cli project delete <id>
# Team management
n8n-cli project members <id>
n8n-cli project add-member <id> --user=<userId> --role=<role>
n8n-cli project remove-member <id> --user=<userId>
标签
n8n-cli tag list
n8n-cli tag create --name=production
n8n-cli tag update <id> --name=staging
n8n-cli tag delete <id>
变量
n8n-cli variable list
n8n-cli variable create --key=API_ENDPOINT --value=https://api.example.com
n8n-cli variable update <id> --key=API_ENDPOINT --value=https://new-api.example.com
n8n-cli variable delete <id>
数据表
# CRUD
n8n-cli data-table list
n8n-cli data-table get <id>
n8n-cli data-table create --name=Inventory --columns='[{"name":"item","type":"string"},{"name":"qty","type":"number"}]'
n8n-cli data-table delete <id>
# Row operations
n8n-cli data-table rows <id>
n8n-cli data-table add-rows <id> --file=rows.json
n8n-cli data-table update-rows <id> --file=rows.json
n8n-cli data-table upsert-rows <id> --file=rows.json
n8n-cli data-table delete-rows <id> --ids=row1,row2,row3
# All row commands support --stdin
cat rows.json | n8n-cli data-table add-rows <id> --stdin
用户
n8n-cli user list
n8n-cli user get <id>
推广
通过 Git 仓库在实例之间移动项目。提供方保存凭据,连接用于指定仓库,配置则设置其中一个方向:promote 推送到 Git,apply 从 Git 导入。
# 1. Create a provider. An SSH provider returns a public key to add as a deploy key.
# The response carries the provider fields at the top level, so `.id` and
# `.publicKey` both work with --jq and --format=id-only.
echo '{"name":"GitHub","type":"git","auth":{"authType":"ssh-key","keyType":"ed25519"}}' \
| n8n-cli promotion-provider create --stdin --json > provider.json
jq -r '.id' provider.json # use as providerId in step 2
jq -r '.publicKey' provider.json # add to the repository as a deploy key
# 2. Create a connection on that provider, with the directions you need.
# Leave out "configs" to configure no direction yet.
n8n-cli promotion-connection create --file=connection.json
# 3. Clone each direction before you use it.
n8n-cli promotion-connection clone <id> promote
n8n-cli promotion-connection clone <id> apply
# 4. Promote from this instance, or apply to it.
n8n-cli promotion-connection promote <id> -m "Promote team projects"
n8n-cli promotion-connection apply <id>
仅应用经过审核的提交:
# Review the changes. Take the commit SHA from the same response as the rows:
# a second request can read a newer commit.
n8n-cli promotion-connection list-changes <projectId> apply --sort=updatedAt --order=desc --json > reviewed-changes.json
jq '{commitSha, changes}' reviewed-changes.json
# The config ID and branch come from the connection.
n8n-cli promotion-connection get <id> --jq '.configs.apply.id'
n8n-cli promotion-connection get <id> --jq '.configs.apply.settings.branchName'
n8n-cli promotion-connection apply <id> \
--expected-config-id=<configId> --expected-branch=<branch> --expected-commit-sha=<full sha>
# Exit 3: the source changed since the review. Review again.
# Exit 4: preflight found missing bindings, access requirements, or conflicts. Resolve them,
# then run the apply-continue command that apply printed:
n8n-cli promotion-connection apply-continue <id> \
--expected-config-id=<configId> --expected-branch=<branch> --expected-commit-sha=<full sha>
# To also apply destructive data table changes and delete the team projects that the
# package does not hold, add --confirm-destructive-changes.
步骤 2 的连接 JSON:
{
"name": "Production",
"scope": "instance",
"providerId": "prov-1",
"target": { "schemaVersion": 1, "remoteUrl": "[email protected]:acme/flows.git" },
"configs": {
"promote": {
"settings": {
"schemaVersion": 1,
"baseBranchName": "main",
"createBranchOnPromotion": false
}
},
"apply": { "settings": { "schemaVersion": 1, "branchName": "main" } }
}
}
# Providers
n8n-cli promotion-provider list
n8n-cli promotion-provider get <id> # re-read the public key; the list omits it
echo '{"name":"New name"}' | n8n-cli promotion-provider update <id> --stdin
n8n-cli promotion-provider delete <id> # fails while a connection uses it
# Connections
n8n-cli promotion-connection list --scope=instance
n8n-cli promotion-connection list --provider=<providerId>
n8n-cli promotion-connection get <id>
echo '{"name":"New name"}' | n8n-cli promotion-connection update <id> --stdin
n8n-cli promotion-connection delete <id>
# Change one direction. The write replaces the whole configuration,
# so send every setting you want to keep.
echo '{"settings":{"schemaVersion":1,"branchName":"main"}}' \
| n8n-cli promotion-connection set-config <id> apply --stdin
echo '{"settings":{"schemaVersion":1,"baseBranchName":"main","createBranchOnPromotion":false}}' \
| n8n-cli promotion-connection set-config <id> promote --stdin
n8n-cli promotion-connection delete-config <id> apply
n8n-cli promotion-connection disconnect <id> promote
# Link projects to a "projects"-scoped connection
n8n-cli promotion-connection list-projects <id>
n8n-cli promotion-connection add-project <id> <projectId>
n8n-cli promotion-connection remove-project <id> <projectId>
要点:
- 所有 JSON 请求体都必须通过
--stdin或--file传入,切勿通过标志传入。这样可避免凭据出现在命令行中。 authType为ssh-key或token。token表示 HTTP(S) 用户名和密码,而不是 Git 主机 API 令牌。对于token提供方,publicKey为null。promote配置始终需要createBranchOnPromotion。promote和apply只能用于instance连接,并且需要先克隆对应方向。克隆一个方向不会使另一个方向也准备就绪。- 当退出代码为
3(source-changed)或4(blocked)时,apply和apply-continue不会导入任何内容。读取counts前,请检查$?或 JSON 输出中的status字段。使用--json时,blocked结果会在preflight下列出缺少的内容。 --expected-*标志必须一起使用:要么全部传入,要么都不传。提交 SHA 必须是完整 SHA。- 此组的 API 密钥作用域命名为
gitConnection:*。当工作流引用变量时,promote还需要variable:list。
其他
# Security audit
n8n-cli audit
n8n-cli audit --categories=credentials,nodes
# Source control
n8n-cli source-control pull
# View config
n8n-cli config show
组合使用模式
此 CLI 专为管道传递和组合使用而设计:
# Get all workflow IDs
n8n-cli workflow list --jq '.[].id'
# Get the name of the first workflow
n8n-cli workflow list --jq '.[0].name'
# Export a workflow to a file
n8n-cli workflow get 1234 --json > workflow-backup.json
# Find failing executions for a workflow
n8n-cli execution list --workflow=1234 --status=error --json
# Pipe workflow JSON for modification
n8n-cli workflow get 1234 --json | jq '.name = "Updated Name"' | n8n-cli workflow update 1234 --stdin
# Table output without headers for shell parsing
n8n-cli workflow list --no-header | awk '{print $1}'
# Debug API calls
n8n-cli workflow list --debug 2>debug.log
工作流 JSON 结构
创建或更新工作流时,JSON 遵循以下结构:
{
"name": "My Workflow",
"nodes": [
{
"name": "Start",
"type": "n8n-nodes-base.manualTrigger",
"position": [250, 300],
"parameters": {}
},
{
"name": "HTTP Request",
"type": "n8n-nodes-base.httpRequest",
"position": [450, 300],
"parameters": {
"url": "https://api.example.com/data",
"method": "GET"
}
}
],
"connections": {
"Start": {
"main": [[{ "node": "HTTP Request", "type": "main", "index": 0 }]]
}
}
}
要点:
- 内置节点的
nodes[].type遵循n8n-nodes-base.<nodeName>模式 connections以源节点名称为键,包含main输出数组- 每个连接都会指定目标
node、type(通常为main)和输出index - 使用
workflow get <id> --json查看实例中的实际示例
评论
支持 Markdown