通过Webhook在n8n中创建工作流
中级
这是一个Engineering领域的自动化工作流,包含 11 个节点。主要使用 If, Set, Code, Webhook, HttpRequest 等节点。 通过Webhook和n8n API以编程方式创建动态工作流
前置要求
- •HTTP Webhook 端点(n8n 会自动生成)
- •可能需要目标 API 的认证凭证
使用的节点 (11)
分类
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
"id": "mchujUnGa9RKf7dO",
"meta": {
"instanceId": "6f30db2c3fce1d1703bcd68e01088879e38bc7981b202c52c370fb8fb136ded4",
"templateCredsSetupCompleted": true
},
"name": "通过 Webhook 在 n8n 中创建工作流",
"tags": [],
"nodes": [
{
"id": "41cfbc58-c8d5-4a22-aec1-72a4b7db3d63",
"name": "便签 4",
"type": "n8n-nodes-base.stickyNote",
"position": [
-2360,
-2100
],
"parameters": {
"width": 720,
"height": 500,
"content": "1. **Webhook**:接收发送到 `/webhook/create-workflow` 的 POST 请求,其中包含工作流 JSON。"
},
"typeVersion": 1
},
{
"id": "62c4a402-1397-4aad-82de-c38672a382a7",
"name": "便签6",
"type": "n8n-nodes-base.stickyNote",
"position": [
-1600,
-1740
],
"parameters": {
"color": 3,
"width": 820,
"height": 360,
"content": "1. **验证错误**:当初始有效负载验证失败时,设置一个 JSON 响应,其中 `success: false` 并使用传入的 `$json.message` 作为错误消息。"
},
"typeVersion": 1
},
{
"id": "df771acf-ef37-49f9-8a9f-ec60fd955d86",
"name": "便签7",
"type": "n8n-nodes-base.stickyNote",
"position": [
-1520,
-2320
],
"parameters": {
"width": 720,
"height": 500,
"content": "1. **创建工作流**:使用基于头的身份验证,向 `/api/v1/workflows` 发送包含已验证工作流有效负载的 POST 请求。"
},
"typeVersion": 1
},
{
"id": "c798d2a9-577d-4f46-b269-ca2b4447560c",
"name": "Webhook",
"type": "n8n-nodes-base.webhook",
"position": [
-2280,
-1880
],
"webhookId": "baad3add-f74a-447a-9764-75967674fa9f",
"parameters": {
"path": "create-workflow",
"options": {},
"httpMethod": "POST",
"responseMode": "lastNode"
},
"notesInFlow": false,
"typeVersion": 1
},
{
"id": "3ca95b00-83b2-4f75-874f-43a27e298bf1",
"name": "验证 JSON",
"type": "n8n-nodes-base.code",
"position": [
-2060,
-1880
],
"parameters": {
"jsCode": "// Validate the received JSON\nconst payload = $input.item.json.body;\n\n// Check that required fields exist\nif (!payload) {\n return {\n json: {\n success: false,\n message: \"No payload received in the request\"\n }\n };\n}\n\n// Ensure correct format for a workflow\nconst requiredFields = ['name', 'nodes'];\nfor (const field of requiredFields) {\n if (!payload[field]) {\n return {\n json: {\n success: false,\n message: `The field '${field}' is required in the workflow`\n }\n };\n }\n}\n\n// Ensure connections and settings exist\nif (!payload.connections) payload.connections = {};\nif (!payload.settings) payload.settings = {};\n\n// Verify that there is at least one node\nif (!Array.isArray(payload.nodes) || payload.nodes.length === 0) {\n return {\n json: {\n success: false,\n message: \"The workflow must have at least one node\"\n }\n };\n}\n\n// Validate each node's required fields\nfor (const node of payload.nodes) {\n const nodeRequiredFields = ['id', 'name', 'type', 'position'];\n for (const field of nodeRequiredFields) {\n if (!node[field]) {\n return {\n json: {\n success: false,\n message: `Node '${node.name || 'unknown'}' is missing required field '${field}'`\n }\n };\n }\n }\n \n // Ensure parameters exists\n if (!node.parameters) node.parameters = {};\n if (!node.typeVersion) node.typeVersion = 1;\n}\n\n// Prepare the workflow object for the API\nconst apiWorkflow = {\n name: payload.name,\n nodes: payload.nodes,\n connections: payload.connections,\n settings: payload.settings\n};\n\n// If everything is valid, proceed\nreturn {\n json: {\n success: true,\n apiWorkflow: apiWorkflow\n }\n};"
},
"typeVersion": 1
},
{
"id": "b3894755-0583-4c00-b574-e993a6f0b08f",
"name": "验证是否成功?",
"type": "n8n-nodes-base.if",
"position": [
-1840,
-1880
],
"parameters": {
"conditions": {
"boolean": [
{
"value1": "={{ $json.success }}",
"value2": true
}
]
}
},
"typeVersion": 1
},
{
"id": "ef3a310b-7673-40cc-985a-4e87dc99de70",
"name": "创建工作流",
"type": "n8n-nodes-base.httpRequest",
"position": [
-1440,
-2080
],
"parameters": {
"url": "=http://{{ $('Webhook').item.json.headers.host }}/api/v1/workflows",
"options": {},
"authentication": "headerAuth"
},
"credentials": {
"httpHeaderAuth": {
"id": "TS0ZOhjrOm9oUaWR",
"name": "N8N"
}
},
"typeVersion": 1,
"continueOnFail": true
},
{
"id": "7e2c1a11-1d4c-4075-a03c-f6ed93307d19",
"name": "API 是否成功?",
"type": "n8n-nodes-base.if",
"position": [
-1220,
-2080
],
"parameters": {
"conditions": {
"number": [
{
"value1": "={{ $response.statusCode }}",
"value2": 299,
"operation": "smallerEqual"
}
]
}
},
"typeVersion": 1
},
{
"id": "0371ce73-ccb6-4e87-8620-4aab8529b170",
"name": "成功响应",
"type": "n8n-nodes-base.set",
"position": [
-960,
-2180
],
"parameters": {
"values": {
"string": [
{
"name": "success",
"value": "true"
},
{
"name": "message",
"value": "Workflow created successfully"
},
{
"name": "workflowId",
"value": "={{ $json.data[0].id }}"
},
{
"name": "workflowName",
"value": "={{ $json.data[0].name }}"
},
{
"name": "createdAt",
"value": "={{ $json.data[0].createdAt }}"
},
{
"name": "url",
"value": "=http://localhost:5678/workflow/{{ $json.data[0].id }}"
}
]
},
"options": {}
},
"typeVersion": 1
},
{
"id": "daec93d7-b9d8-4407-93ab-389b705b0c11",
"name": "验证错误",
"type": "n8n-nodes-base.set",
"position": [
-1500,
-1540
],
"parameters": {
"values": {
"string": [
{
"name": "success",
"value": "false"
},
{
"name": "message",
"value": "={{ $json.message }}"
}
]
},
"options": {}
},
"typeVersion": 1
},
{
"id": "cd2cd403-63c2-42af-a715-9f080067f7d1",
"name": "API 错误",
"type": "n8n-nodes-base.set",
"position": [
-920,
-1540
],
"parameters": {
"values": {
"string": [
{
"name": "success",
"value": "false"
},
{
"name": "message",
"value": "Error creating workflow"
},
{
"name": "error",
"value": "={{ JSON.stringify($json) }}"
},
{
"name": "statusCode",
"value": "={{ $response.statusCode }}"
}
]
},
"options": {}
},
"typeVersion": 1
}
],
"active": true,
"pinData": {
"Webhook": [
{
"json": {
"body": {
"name": "Workflow Created by ID mchujUnGa9RKf7dO",
"nodes": [
{
"id": "start-node",
"name": "Start",
"type": "n8n-nodes-base.manualTrigger",
"position": [
100,
100
],
"parameters": {},
"typeVersion": 1
},
{
"id": "set-node",
"name": "Set",
"type": "n8n-nodes-base.set",
"position": [
300,
100
],
"parameters": {
"values": {
"string": [
{
"name": "message",
"value": "This workflow was created using the webhook in mchujUnGa9RKf7dO!"
}
]
}
},
"typeVersion": 1
}
],
"settings": {},
"connections": {
"Start": {
"main": [
[
{
"node": "Set",
"type": "main",
"index": 0
}
]
]
}
}
},
"query": {},
"params": {},
"headers": {
"host": "127.0.0.1:5678",
"connection": "keep-alive",
"content-type": "application/json",
"transfer-encoding": "chunked"
},
"webhookUrl": "http://localhost:5678/webhook/create-workflow",
"executionMode": "production"
}
}
]
},
"settings": {
"executionOrder": "v1"
},
"versionId": "849462be-761a-455e-a99b-ffe52b2249d7",
"connections": {
"Webhook": {
"main": [
[
{
"node": "Validate JSON",
"type": "main",
"index": 0
}
]
]
},
"Validate JSON": {
"main": [
[
{
"node": "Validation Successful?",
"type": "main",
"index": 0
}
]
]
},
"API Successful?": {
"main": [
[
{
"node": "Success Response",
"type": "main",
"index": 0
}
],
[
{
"node": "API Error",
"type": "main",
"index": 0
}
]
]
},
"Create Workflow": {
"main": [
[
{
"node": "API Successful?",
"type": "main",
"index": 0
}
]
]
},
"Validation Successful?": {
"main": [
[
{
"node": "Create Workflow",
"type": "main",
"index": 0
}
],
[
{
"node": "Validation Error",
"type": "main",
"index": 0
}
]
]
}
}
}常见问题
如何使用这个工作流?
复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。
这个工作流适合什么场景?
中级 - 工程
需要付费吗?
本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。
相关工作流推荐
CSV到JSON转换器(错误处理与Slack通知)
具有错误处理和Slack通知功能的CSV到JSON转换器
If
Set
Code
+8
18 节点n8n Team
工程
PUQ Docker NextCloud 部署
部署 Docker NextCloud,WHMCS/WISECP 的 API 后端
If
Set
Ssh
+6
44 节点PUQcloud
工程
获取所有Scaleway服务器信息副本
通过动态筛选获取Scaleway服务器信息
If
Set
Code
+7
24 节点Pablo
工程
实时Notion Todoist双向同步模板
使用Redis的Notion Todoist实时双向同步
If
Set
Code
+26
246 节点Mario
销售
🤖 高级 Slackbot(n8n)
🤖 高级 Slackbot(n8n)
If
Set
Code
+9
34 节点Mutasem
工程
AI智能助手:与Supabase存储和Google Drive文件对话
AI智能助手:与Supabase存储和Google Drive文件对话
If
Set
Wait
+20
62 节点Mark Shcherbakov
工程
工作流信息
难度等级
中级
节点数量11
分类1
节点类型6
作者
Mauricio Perera
@rckflrAutomation consultant with over 10 years of experience specializing in AI, no-code, and workflow optimization. I’ve delivered tailored AI and NLP solutions across real estate, healthcare, and more, enhancing efficiency and customer experiences. Proficient in tools like Make, Airtable, and Zapier, I also integrate GPT models to create scalable, innovative automations. Contact me to discuss custom n8n workflows or advanced automations to streamline your processes.
外部链接
在 n8n.io 查看 →
分享此工作流