8
n8n 中文网amn8n.com

x402

中级

这是一个Crypto Trading领域的自动化工作流,包含 15 个节点。主要使用 If, Code, Webhook, OneShot, OneShotSynch 等节点。 使用x402支付协议和1Shot API实现工作流货币化

前置要求
  • HTTP Webhook 端点(n8n 会自动生成)
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "EfbdYnnqgNACIN81",
  "meta": {
    "instanceId": "62f017ec8f130d172e2e5f39bbf09515036bfd403dfa60fe06f5ab14b78705d0",
    "templateCredsSetupCompleted": true
  },
  "name": "x402",
  "tags": [
    {
      "id": "ChlnN7rtKHJMnWmM",
      "name": "x402",
      "createdAt": "2025-06-19T04:23:28.149Z",
      "updatedAt": "2025-06-19T04:23:28.149Z"
    }
  ],
  "nodes": [
    {
      "id": "bf854266-4250-4491-af67-ff8fc3b63ac6",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -500,
        -50
      ],
      "webhookId": "92c5ca23-99a7-437d-85da-84aef8bd2a25",
      "parameters": {
        "path": "92c5ca23-99a7-437d-85da-84aef8bd2a25",
        "options": {},
        "responseMode": "responseNode"
      },
      "typeVersion": 2
    },
    {
      "id": "82409682-6932-4714-9f7c-27c60a692245",
      "name": "检查是否存在 X-HEADER",
      "type": "n8n-nodes-base.if",
      "position": [
        -280,
        -50
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "2ff83590-61f3-41d8-8500-a96103c3844c",
              "operator": {
                "type": "string",
                "operation": "exists",
                "singleValue": true
              },
              "leftValue": "={{ $json.headers['x-payment'] }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "efb6f824-6679-4ad4-9b05-c20ef27a706d",
      "name": "解码和验证 X-Payment",
      "type": "n8n-nodes-base.code",
      "position": [
        -60,
        -125
      ],
      "parameters": {
        "jsCode": "try {\n    // Decode the x-payment header from base64\n    const xPaymentHeader = $input.first().json.headers['x-payment'];\n    const decodedXPayment = Buffer.from(xPaymentHeader, 'base64').toString('utf-8');\n\n    // Parse the decoded value into a JSON object\n    const decodedXPaymentJson = JSON.parse(decodedXPayment);\n\n    // Add the parsed JSON object to the input\n    $input.first().json.decodedXPayment = decodedXPaymentJson;\n\n    return $input.all();\n} catch (error) {\n    // Return an error object if the token format is invalid\n    return { error: \"invalid token format\" };\n}"
      },
      "typeVersion": 2
    },
    {
      "id": "6cad76b8-1a56-4028-92f4-a6fb696e1647",
      "name": "模拟支付",
      "type": "n8n-nodes-1shot.oneShot",
      "position": [
        380,
        -175
      ],
      "parameters": {
        "params": "={\n  \"from\": \"{{ $json.decodedXPayment.payload.authorization.from }}\",\n  \"to\": \"{{ $json.decodedXPayment.payload.authorization.to }}\",\n  \"value\": \"{{ $json.decodedXPayment.payload.authorization.value }}\",\n  \"validAfter\": \"{{ $json.decodedXPayment.payload.authorization.validAfter }}\",\n  \"validBefore\": \"{{ $json.decodedXPayment.payload.authorization.validBefore }}\",\n  \"nonce\": \"{{ $json.decodedXPayment.payload.authorization.nonce }}\",\n  \"signature\": \"{{ $json.decodedXPayment.payload.signature }}\"\n} ",
        "operation": "simulate",
        "contractMethodId": "b63aaaa1-059d-4c38-928a-33ad17d66827"
      },
      "credentials": {
        "oneShotOAuth2Api": {
          "id": "nkfF9AitCKUCrErK",
          "name": "1Shot account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "a2f18d76-8bea-4d1c-9287-6c0a7594d6da",
      "name": "支付模拟成功时",
      "type": "n8n-nodes-base.if",
      "position": [
        600,
        -175
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "81c67679-e256-4fd2-bed7-8f4272c2392b",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.success.toString() }}",
              "rightValue": "true"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "1f182441-25da-41cc-913d-8e724866d7d3",
      "name": "1Shot API 提交并等待",
      "type": "n8n-nodes-1shot.oneShotSynch",
      "position": [
        820,
        -250
      ],
      "parameters": {
        "params": "={\n\"to\": \"{{ $('Ensure Well Formatted Payment Payload').item.json.decodedXPayment.payload.authorization.to }}\",\n\"from\": \"{{ $('Ensure Well Formatted Payment Payload').item.json.decodedXPayment.payload.authorization.from }}\",\n\"value\": \"{{ $('Ensure Well Formatted Payment Payload').item.json.decodedXPayment.payload.authorization.value }}\",\n\"validAfter\": \"{{ $('Ensure Well Formatted Payment Payload').item.json.decodedXPayment.payload.authorization.validAfter }}\",\n\"validBefore\": \"{{ $('Ensure Well Formatted Payment Payload').item.json.decodedXPayment.payload.authorization.validBefore }}\",\n\"nonce\": \"{{ $('Ensure Well Formatted Payment Payload').item.json.decodedXPayment.payload.authorization.nonce }}\",\n\"signature\": \"{{ $('Ensure Well Formatted Payment Payload').item.json.decodedXPayment.payload.signature }}\"\n}",
        "additionalFields": {},
        "contractMethodId": "b63aaaa1-059d-4c38-928a-33ad17d66827"
      },
      "credentials": {
        "oneShotOAuth2Api": {
          "id": "nkfF9AitCKUCrErK",
          "name": "1Shot account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "cfa44cb3-de5c-4ef2-9950-cca2d2bdcfbc",
      "name": "确保支付载荷格式正确",
      "type": "n8n-nodes-base.if",
      "position": [
        160,
        -120
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "6666e1f6-5e99-49d4-9f9a-a5a35cde4f74",
              "operator": {
                "type": "string",
                "operation": "exists",
                "singleValue": true
              },
              "leftValue": "={{ $json.decodedXPayment.payload.signature }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "605abcb3-2acc-4db4-a87a-efae930a015d",
      "name": "响应:缺少或无效的支付头",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        380,
        50
      ],
      "parameters": {
        "options": {
          "responseCode": 402
        },
        "respondWith": "json",
        "responseBody": "{\n  \"x402Version\": \"1\",\n  \"error\": \"X-PAYMENT header has incorrect format\"\n}"
      },
      "typeVersion": 1.3
    },
    {
      "id": "8790cc26-6626-47fe-ae0f-8142b3fde734",
      "name": "响应:支付无效",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        1040,
        -75
      ],
      "parameters": {
        "options": {
          "responseCode": 402
        },
        "respondWith": "json",
        "responseBody": "{\n  \"x402Version\": \"1\",\n  \"error\": \"X-PAYMENT header did not verify\"\n}"
      },
      "typeVersion": 1.3
    },
    {
      "id": "5b29d382-d7dc-484c-b700-3de0c5935c8a",
      "name": "响应:200 - 支付成功",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        1040,
        -300
      ],
      "parameters": {
        "options": {
          "responseCode": 200
        },
        "respondWith": "text",
        "responseBody": "\"Payment Received!\" "
      },
      "typeVersion": 1.3
    },
    {
      "id": "ff4b8de5-6be4-4cce-abc6-05e3902eb535",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -600,
        -320
      ],
      "parameters": {
        "width": 360,
        "height": 220,
        "content": "## x402支付端点"
      },
      "typeVersion": 1
    },
    {
      "id": "7edff090-85d5-48e6-9d86-203bbf3cea11",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        420,
        -400
      ],
      "parameters": {
        "width": 360,
        "content": "## 任何EVM上的任何ERC-20代币"
      },
      "typeVersion": 1
    },
    {
      "id": "9b46463f-be85-47e2-a05c-96a696f1965f",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1300,
        -360
      ],
      "parameters": {
        "width": 320,
        "height": 200,
        "content": "## 在此处放置您的工作流"
      },
      "typeVersion": 1
    },
    {
      "id": "4e74105c-cf34-45bb-a805-9e3dec1b2435",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -980,
        140
      ],
      "parameters": {
        "width": 840,
        "height": 280,
        "content": "## 示例Curl命令"
      },
      "typeVersion": 1
    },
    {
      "id": "ea9adf09-d52c-4ef5-a468-db1dc3247270",
      "name": "便签4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        80,
        -300
      ],
      "parameters": {
        "content": "## 确保必需的支付详情"
      },
      "typeVersion": 1
    }
  ],
  "active": true,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "f5595fe7-92b3-4096-aa95-fbd5c78b0a66",
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Check for presence of X-HEADER",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simulate Payment": {
      "main": [
        [
          {
            "node": "On Successful Payment Simulation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "1Shot API Submit & Wait": {
      "main": [
        [
          {
            "node": "Response: 200 - Payment Successful",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Response: Payment Invalid",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Decode & Validate X-Payment": {
      "main": [
        [
          {
            "node": "Ensure Well Formatted Payment Payload",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check for presence of X-HEADER": {
      "main": [
        [
          {
            "node": "Decode & Validate X-Payment",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Response: Missing or Invalid Payment Headers",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "On Successful Payment Simulation": {
      "main": [
        [
          {
            "node": "1Shot API Submit & Wait",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Response: Payment Invalid",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ensure Well Formatted Payment Payload": {
      "main": [
        [
          {
            "node": "Simulate Payment",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Response: Missing or Invalid Payment Headers",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

中级 - 加密货币交易

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
中级
节点数量15
分类1
节点类型7
难度说明

适合有一定经验的用户,包含 6-15 个节点的中等复杂度工作流

作者
1Shot API

1Shot API

@oneshotapi

1Shot API lets you bring onchain actions into your n8n workflows.

外部链接
在 n8n.io 查看

分享此工作流