n8n

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

n8n 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-key

Core 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-endpoint

2. 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:
SyntaxDescription
={{ $json.field }}
Access current item field
={{ $json.body.param }}
Access nested field
={{ $('Node Name').item.json.field }}
Access output from specific node
={{ $input.first().json }}
First input item
={{ $input.all() }}
All input items
={{ JSON.stringify($json) }}
Convert to JSON string
n8n使用表达式来实现动态值:
语法描述
={{ $json.field }}
访问当前项目的字段
={{ $json.body.param }}
访问嵌套字段
={{ $('Node Name').item.json.field }}
访问特定节点的输出
={{ $input.first().json }}
第一个输入项目
={{ $input.all() }}
所有输入项目
={{ JSON.stringify($json) }}
转换为JSON字符串

Common Node Types

常见节点类型

NodeTypePurpose
Webhook
n8n-nodes-base.webhook
HTTP trigger
HTTP Request
n8n-nodes-base.httpRequest
API calls
Respond to Webhook
n8n-nodes-base.respondToWebhook
Return HTTP response
IF
n8n-nodes-base.if
Conditional branching
Switch
n8n-nodes-base.switch
Multi-way branching
Set
n8n-nodes-base.set
Transform data
Code
n8n-nodes-base.code
Custom JavaScript
Split In Batches
n8n-nodes-base.splitInBatches
Loop processing
Merge
n8n-nodes-base.merge
Combine branches
节点类型用途
Webhook
n8n-nodes-base.webhook
HTTP触发器
HTTP Request
n8n-nodes-base.httpRequest
API调用
Respond to Webhook
n8n-nodes-base.respondToWebhook
返回HTTP响应
IF
n8n-nodes-base.if
条件分支
Switch
n8n-nodes-base.switch
多分支路由
Set
n8n-nodes-base.set
数据转换
Code
n8n-nodes-base.code
自定义JavaScript
Split In Batches
n8n-nodes-base.splitInBatches
循环处理
Merge
n8n-nodes-base.merge
合并分支

MCP Integration

MCP集成

n8n can expose workflows as MCP tools via the built-in MCP server:
  1. Enable MCP in workflow settings:
    "availableInMCP": true
  2. Access MCP endpoint:
    http://localhost:5678/mcp-server/http
  3. Use supergateway for Claude Code integration
n8n可通过内置的MCP服务器将工作流作为MCP工具暴露:
  1. 在工作流设置中启用MCP:
    "availableInMCP": true
  2. 访问MCP端点:
    http://localhost:5678/mcp-server/http
  3. 使用supergateway集成Claude Code

Tips

小贴士

  1. Webhook Response: Use
    responseMode: "responseNode"
    with Respond node for control
  2. Credentials: Store API keys in n8n credentials, reference by ID
  3. Error Handling: Add Error Trigger node for failure notifications
  4. Testing: Use
    /webhook-test/
    path during development
  5. ADF Format: Jira/Confluence require Atlassian Document Format for rich text
  1. Webhook响应:结合使用
    responseMode: "responseNode"
    和Respond节点以实现控制
  2. 凭据:将API密钥存储在n8n凭据中,通过ID引用
  3. 错误处理:添加Error Trigger节点以接收失败通知
  4. 测试:开发期间使用
    /webhook-test/
    路径
  5. ADF格式:Jira/Confluence需要Atlassian文档格式(ADF)来支持富文本