n8n
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesen8n Workflow Automation Skill
n8n工作流自动化技能
This skill enables creating and managing n8n workflows for automation tasks.
本技能可用于创建和管理n8n工作流以实现自动化任务。
Prerequisites
前提条件
n8n instance running with API access:
bash
N8N_HOST=localhost
N8N_PORT=5678
N8N_API_KEY=your-api-key运行具备API访问权限的n8n实例:
bash
N8N_HOST=localhost
N8N_PORT=5678
N8N_API_KEY=your-api-keyCore Concepts
核心概念
Workflow Structure
工作流结构
Every n8n workflow is JSON with this structure:
json
{
"name": "Workflow Name",
"nodes": [],
"connections": {},
"settings": {
"executionOrder": "v1"
}
}每个n8n工作流都是如下结构的JSON:
json
{
"name": "Workflow Name",
"nodes": [],
"connections": {},
"settings": {
"executionOrder": "v1"
}
}Node Structure
节点结构
Each node has:
json
{
"id": "unique-id",
"name": "Display Name",
"type": "n8n-nodes-base.nodetype",
"typeVersion": 1,
"position": [x, y],
"parameters": {},
"credentials": {}
}每个节点包含以下内容:
json
{
"id": "unique-id",
"name": "Display Name",
"type": "n8n-nodes-base.nodetype",
"typeVersion": 1,
"position": [x, y],
"parameters": {},
"credentials": {}
}Connection Structure
连接结构
Connections define data flow between nodes:
json
{
"Source Node": {
"main": [
[{"node": "Target Node", "type": "main", "index": 0}]
]
}
}连接定义了节点之间的数据流:
json
{
"Source Node": {
"main": [
[{"node": "Target Node", "type": "main", "index": 0}]
]
}
}Common Workflow Patterns
常见工作流模式
1. Webhook-Triggered Workflow
1. Webhook触发的工作流
Creates an HTTP endpoint that triggers workflow execution:
json
{
"name": "Webhook Handler",
"nodes": [
{
"id": "webhook",
"name": "Webhook",
"type": "n8n-nodes-base.webhook",
"typeVersion": 2,
"position": [250, 300],
"webhookId": "my-webhook",
"parameters": {
"path": "my-endpoint",
"httpMethod": "POST",
"responseMode": "responseNode"
}
},
{
"id": "respond",
"name": "Respond",
"type": "n8n-nodes-base.respondToWebhook",
"typeVersion": 1.1,
"position": [450, 300],
"parameters": {
"respondWith": "json",
"responseBody": "={{ $json }}"
}
}
],
"connections": {
"Webhook": {
"main": [[{"node": "Respond", "type": "main", "index": 0}]]
}
}
}Access at:
http://localhost:5678/webhook/my-endpoint创建一个HTTP端点来触发工作流执行:
json
{
"name": "Webhook Handler",
"nodes": [
{
"id": "webhook",
"name": "Webhook",
"type": "n8n-nodes-base.webhook",
"typeVersion": 2,
"position": [250, 300],
"webhookId": "my-webhook",
"parameters": {
"path": "my-endpoint",
"httpMethod": "POST",
"responseMode": "responseNode"
}
},
{
"id": "respond",
"name": "Respond",
"type": "n8n-nodes-base.respondToWebhook",
"typeVersion": 1.1,
"position": [450, 300],
"parameters": {
"respondWith": "json",
"responseBody": "={{ $json }}"
}
}
],
"connections": {
"Webhook": {
"main": [[{"node": "Respond", "type": "main", "index": 0}]]
}
}
}访问地址:
http://localhost:5678/webhook/my-endpoint2. HTTP Request Pattern
2. HTTP请求模式
Make external API calls:
json
{
"id": "http",
"name": "HTTP Request",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [450, 300],
"parameters": {
"method": "POST",
"url": "https://api.example.com/endpoint",
"authentication": "predefinedCredentialType",
"nodeCredentialType": "myApiCredential",
"sendBody": true,
"specifyBody": "json",
"jsonBody": "={{ JSON.stringify($json) }}"
},
"credentials": {
"myApiCredential": {"id": "cred-id", "name": "My Credential"}
}
}发起外部API调用:
json
{
"id": "http",
"name": "HTTP Request",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [450, 300],
"parameters": {
"method": "POST",
"url": "https://api.example.com/endpoint",
"authentication": "predefinedCredentialType",
"nodeCredentialType": "myApiCredential",
"sendBody": true,
"specifyBody": "json",
"jsonBody": "={{ JSON.stringify($json) }}"
},
"credentials": {
"myApiCredential": {"id": "cred-id", "name": "My Credential"}
}
}3. Conditional Branching (IF Node)
3. 条件分支(IF节点)
Route data based on conditions:
json
{
"id": "if",
"name": "IF",
"type": "n8n-nodes-base.if",
"typeVersion": 2,
"position": [450, 300],
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict"
},
"conditions": [
{
"leftValue": "={{ $json.status }}",
"rightValue": "success",
"operator": {
"type": "string",
"operation": "equals"
}
}
],
"combinator": "and"
}
}
}根据条件路由数据:
json
{
"id": "if",
"name": "IF",
"type": "n8n-nodes-base.if",
"typeVersion": 2,
"position": [450, 300],
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict"
},
"conditions": [
{
"leftValue": "={{ $json.status }}",
"rightValue": "success",
"operator": {
"type": "string",
"operation": "equals"
}
}
],
"combinator": "and"
}
}
}4. Loop Over Items (SplitInBatches)
4. 循环处理项目(SplitInBatches节点)
Process items in batches:
json
{
"id": "batch",
"name": "Loop Over Items",
"type": "n8n-nodes-base.splitInBatches",
"typeVersion": 3,
"position": [450, 300],
"parameters": {
"batchSize": 10,
"options": {}
}
}批量处理项目:
json
{
"id": "batch",
"name": "Loop Over Items",
"type": "n8n-nodes-base.splitInBatches",
"typeVersion": 3,
"position": [450, 300],
"parameters": {
"batchSize": 10,
"options": {}
}
}n8n REST API
n8n REST API
List Workflows
列出工作流
bash
curl -s "http://localhost:5678/api/v1/workflows" \
-H "X-N8N-API-KEY: $N8N_API_KEY"bash
curl -s "http://localhost:5678/api/v1/workflows" \
-H "X-N8N-API-KEY: $N8N_API_KEY"Get Workflow
获取工作流
bash
curl -s "http://localhost:5678/api/v1/workflows/{id}" \
-H "X-N8N-API-KEY: $N8N_API_KEY"bash
curl -s "http://localhost:5678/api/v1/workflows/{id}" \
-H "X-N8N-API-KEY: $N8N_API_KEY"Create Workflow
创建工作流
bash
curl -s -X POST "http://localhost:5678/api/v1/workflows" \
-H "X-N8N-API-KEY: $N8N_API_KEY" \
-H "Content-Type: application/json" \
-d '{"name": "New Workflow", "nodes": [...], "connections": {...}}'bash
curl -s -X POST "http://localhost:5678/api/v1/workflows" \
-H "X-N8N-API-KEY: $N8N_API_KEY" \
-H "Content-Type: application/json" \
-d '{"name": "New Workflow", "nodes": [...], "connections": {...}}'Update Workflow
更新工作流
bash
curl -s -X PUT "http://localhost:5678/api/v1/workflows/{id}" \
-H "X-N8N-API-KEY: $N8N_API_KEY" \
-H "Content-Type: application/json" \
-d '{"name": "Updated", "nodes": [...], "connections": {...}}'bash
curl -s -X PUT "http://localhost:5678/api/v1/workflows/{id}" \
-H "X-N8N-API-KEY: $N8N_API_KEY" \
-H "Content-Type: application/json" \
-d '{"name": "Updated", "nodes": [...], "connections": {...}}'Activate/Deactivate
激活/停用
bash
curl -s -X POST "http://localhost:5678/api/v1/workflows/{id}/activate" \
-H "X-N8N-API-KEY: $N8N_API_KEY"
curl -s -X POST "http://localhost:5678/api/v1/workflows/{id}/deactivate" \
-H "X-N8N-API-KEY: $N8N_API_KEY"bash
curl -s -X POST "http://localhost:5678/api/v1/workflows/{id}/activate" \
-H "X-N8N-API-KEY: $N8N_API_KEY"
curl -s -X POST "http://localhost:5678/api/v1/workflows/{id}/deactivate" \
-H "X-N8N-API-KEY: $N8N_API_KEY"Get Executions
获取执行记录
bash
curl -s "http://localhost:5678/api/v1/executions?workflowId={id}&limit=10&includeData=true" \
-H "X-N8N-API-KEY: $N8N_API_KEY"bash
curl -s "http://localhost:5678/api/v1/executions?workflowId={id}&limit=10&includeData=true" \
-H "X-N8N-API-KEY: $N8N_API_KEY"Expression Syntax
表达式语法
n8n uses expressions for dynamic values:
| Syntax | Description |
|---|---|
| Access current item field |
| Access nested field |
| Access output from specific node |
| First input item |
| All input items |
| Convert to JSON string |
n8n使用表达式来实现动态值:
| 语法 | 描述 |
|---|---|
| 访问当前项目的字段 |
| 访问嵌套字段 |
| 访问特定节点的输出 |
| 第一个输入项目 |
| 所有输入项目 |
| 转换为JSON字符串 |
Common Node Types
常见节点类型
| Node | Type | Purpose |
|---|---|---|
| Webhook | | HTTP trigger |
| HTTP Request | | API calls |
| Respond to Webhook | | Return HTTP response |
| IF | | Conditional branching |
| Switch | | Multi-way branching |
| Set | | Transform data |
| Code | | Custom JavaScript |
| Split In Batches | | Loop processing |
| Merge | | Combine branches |
| 节点 | 类型 | 用途 |
|---|---|---|
| Webhook | | HTTP触发器 |
| HTTP Request | | API调用 |
| Respond to Webhook | | 返回HTTP响应 |
| IF | | 条件分支 |
| Switch | | 多分支路由 |
| Set | | 数据转换 |
| Code | | 自定义JavaScript |
| Split In Batches | | 循环处理 |
| Merge | | 合并分支 |
MCP Integration
MCP集成
n8n can expose workflows as MCP tools via the built-in MCP server:
- Enable MCP in workflow settings:
"availableInMCP": true - Access MCP endpoint:
http://localhost:5678/mcp-server/http - Use supergateway for Claude Code integration
n8n可通过内置的MCP服务器将工作流作为MCP工具暴露:
- 在工作流设置中启用MCP:
"availableInMCP": true - 访问MCP端点:
http://localhost:5678/mcp-server/http - 使用supergateway集成Claude Code
Tips
小贴士
- Webhook Response: Use with Respond node for control
responseMode: "responseNode" - Credentials: Store API keys in n8n credentials, reference by ID
- Error Handling: Add Error Trigger node for failure notifications
- Testing: Use path during development
/webhook-test/ - ADF Format: Jira/Confluence require Atlassian Document Format for rich text
- Webhook响应:结合使用和Respond节点以实现控制
responseMode: "responseNode" - 凭据:将API密钥存储在n8n凭据中,通过ID引用
- 错误处理:添加Error Trigger节点以接收失败通知
- 测试:开发期间使用路径
/webhook-test/ - ADF格式:Jira/Confluence需要Atlassian文档格式(ADF)来支持富文本