Skip to content

嵌入式环境中的工作流管理#

/// 信息 | 功能可用性 Embed 功能需要嵌入许可证。有关何时使用 Embed、成本及授权流程的更多信息,请参阅 n8n 官网上的 Embed 页面。 ///

当管理一个跨团队或跨组织的嵌入式 n8n 部署时,您很可能需要为多个用户运行相同(或相似)的工作流。有两种可用方案:

解决方案 优点 缺点
为每个用户创建独立工作流 工作流启动方式无限制(可使用任何触发器) 需要管理多个工作流
创建单一工作流,执行时传递用户凭据 简化工作流管理(只需修改一个工作流) 运行工作流需要您的产品主动调用

Warning

本文档引用的 API 可能随时变更。请确保在每次版本升级时检查功能兼容性。

按用户分配工作流#

需要遵循三个基本步骤:

  • 获取每个用户的凭据,以及工作流可能需要的任何额外参数
  • 为该用户创建 n8n 凭据
  • 创建工作流

1. 获取用户凭据#

在此步骤中,您需要收集该用户需要认证的所有节点/服务的凭据,以及特定工作流所需的任何额外参数。所需的凭据和参数取决于您的工作流和要实现的目标。

2. 创建用户凭据#

获取所有相关凭据信息后,您可以在 n8n 中创建相应的服务凭据。这可以通过编辑器 UI 或 API 调用来完成。

使用编辑器界面#

  1. 从菜单中选择 Credentials(凭证) > New(新建)
  2. 使用下拉菜单选择要创建的 Credential type(凭证类型),例如 Airtable创建新凭证下拉菜单
  3. Create New Credentials(创建新凭证) 弹窗中,输入相应用户的凭证详情,并选择可以访问这些凭证的节点。 创建新凭证弹窗
  4. 点击 Create(创建) 完成并保存。

使用 API#

也可以通过调用编辑器界面使用的前端 API 来实现相同效果。API 端点格式为:https://<n8n-domain>/rest/credentials

例如,要创建上述编辑器界面示例中的凭证,请求应为:

1
POST https://<n8n-domain>/rest/credentials

请求体内容:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{
   "name":"MyAirtable",
   "type":"airtableApi",
   "nodesAccess":[
      {
         "nodeType":"n8n-nodes-base.airtable"
      }
   ],
   "data":{
      "apiKey":"q12we34r5t67yu"
   }
}

响应将包含新凭证的 ID,在为此用户创建工作流时将使用该 ID:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
{
   "data":{
      "name":"MyAirtable",
      "type":"airtableApi",
      "data":{
         "apiKey":"q12we34r5t67yu"
      },
      "nodesAccess":[
         {
            "nodeType":"n8n-nodes-base.airtable",
            "date":"2021-09-10T07:41:27.770Z"
         }
      ],
      "id":"29",
      "createdAt":"2021-09-10T07:41:27.777Z",
      "updatedAt":"2021-09-10T07:41:27.777Z"
   }
}

3. 创建工作流#

最佳实践是先创建一个"基础"工作流,然后为每个新用户复制并自定义该工作流,添加他们的凭证(以及其他详细信息)。

您可以使用编辑器界面或 API 调用来复制和自定义模板工作流。

使用编辑器界面#

  1. 从菜单中选择 Workflows > Open 打开需要复制的模板工作流。

  2. 选择 Workflows > Duplicate,然后输入新工作流的名称并点击 Save复制工作流

  3. 更新所有相关节点以使用该用户(上文创建的)的凭据。

  4. 点击 Save 保存此工作流,并通过右上角的切换按钮将其设为 Active(激活状态)。

使用 API 接口#

  1. 通过以下端点获取模板工作流的 JSON 数据:https://<n8n-domain>/rest/workflows/<workflow_id>
    1
    GET https://<n8n-domain>/rest/workflows/1012
    

响应将包含所选工作流的 JSON 数据:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
{
  "data": {
    "id": "1012",
    "name": "Nathan's Workflow",
    "active": false,
    "nodes": [
      {
        "parameters": {},
        "name": "Start",
        "type": "n8n-nodes-base.start",
        "typeVersion": 1,
        "position": [
          130,
          640
        ]
      },
      {
        "parameters": {
          "authentication": "headerAuth",
          "url": "https://internal.users.n8n.cloud/webhook/custom-erp",
          "options": {
            "splitIntoItems": true
          },
          "headerParametersUi": {
            "parameter": [
              {
                "name": "unique_id",
                "value": "recLhLYQbzNSFtHNq"
              }
            ]
          }
        },
        "name": "HTTP Request",
        "type": "n8n-nodes-base.httpRequest",
        "typeVersion": 1,
        "position": [
          430,
          300
        ],
        "credentials": {
          "httpHeaderAuth": "beginner_course"
        }
      },
      {
        "parameters": {
          "operation": "append",
          "application": "appKBGQfbm6NfW6bv",
          "table": "processingOrders",
          "options": {}
        },
        "name": "Airtable",
        "type": "n8n-nodes-base.airtable",
        "typeVersion": 1,
        "position": [
          990,
          210
        ],
        "credentials": {
          "airtableApi": "Airtable"
        }
      },
      {
        "parameters": {
          "conditions": {
            "string": [
              {
                "value1": "={{$json[\"orderStatus\"]}}",
                "value2": "processing"
              }
            ]
          }
        },
        "name": "IF",
        "type": "n8n-nodes-base.if",
        "typeVersion": 1,
        "position": [
          630,
          300
        ]
      },
      {
        "parameters": {
          "keepOnlySet": true,
          "values": {
            "number": [
              {
                "name": "=orderId",
                "value": "={{$json[\"orderID\"]}}"
              }
            ],
            "string": [
              {
                "name": "employeeName",
                "value": "={{$json[\"employeeName\"]}}"
              }
            ]
          },
          "options": {}
        },
        "name": "Set",
        "type": "n8n-nodes-base.set",
        "typeVersion": 1,
        "position": [
          800,
          210
        ]
      },
      {
        "parameters": {
          "functionCode": "let totalBooked = items.length;\nlet bookedSum = 0;\n\nfor(let i=0; i < items.length; i++) {\n  bookedSum = bookedSum + items[i].json.orderPrice;\n}\nreturn [{json:{totalBooked, bookedSum}}]\n"
        },
        "name": "Function",
        "type": "n8n-nodes-base.function",
        "typeVersion": 1,
        "position": [
          800,
          400
        ]
      },
      {
        "parameters": {
          "webhookUri": "https://discord.com/api/webhooks/865213348202151968/oD5_WPDQwtr22Vjd_82QP3-_4b_lGhAeM7RynQ8Js5DzyXrQEnj0zeAQIA6fki1JLtXE",
          "text": "=This week we have {{$json[\"totalBooked\"]}} booked orders with a total value of {{$json[\"bookedSum\"]}}. My Unique ID: {{$node[\"HTTP Request\"].parameter[\"headerParametersUi\"][\"parameter\"][0][\"value\"]}}"
        },
        "name": "Discord",
        "type": "n8n-nodes-base.discord",
        "typeVersion": 1,
        "position": [
          1000,
          400
        ]
      },
      {
        "parameters": {
          "triggerTimes": {
            "item": [
              {
                "mode": "everyWeek",
                "hour": 9
              }
            ]
          }
        },
        "name": "Cron",
        "type": "n8n-nodes-base.cron",
        "typeVersion": 1,
        "position": [
          220,
          300
        ]
      }
    ],
    "connections": {
      "HTTP Request": {
        "main": [
          [
            {
              "node": "IF",
              "type": "main",
              "index": 0
            }
          ]
        ]
      },
      "Start": {
        "main": [
          []
        ]
      },
      "IF": {
        "main": [
          [
            {
              "node": "Set",
              "type": "main",
              "index": 0
            }
          ],
          [
            {
              "node": "Function",
              "type": "main",
              "index": 0
            }
          ]
        ]
      },
      "Set": {
        "main": [
          [
            {
              "node": "Airtable",
              "type": "main",
              "index": 0
            }
          ]
        ]
      },
      "Function": {
        "main": [
          [
            {
              "node": "Discord",
              "type": "main",
              "index": 0
            }
          ]
        ]
      },
      "Cron": {
        "main": [
          [
            {
              "node": "HTTP Request",
              "type": "main",
              "index": 0
            }
          ]
        ]
      }
    },
    "createdAt": "2021-07-16T11:15:46.066Z",
    "updatedAt": "2021-07-16T12:05:44.045Z",
    "settings": {},
    "staticData": null,
    "tags": []
  }
}

  1. 保存返回的 JSON 数据,并为新用户更新所有相关的凭证和字段。

  2. 使用更新后的 JSON 作为请求体,在以下端点创建新工作流:https://<n8n-domain>/rest/workflows

    1
    POST https://<n8n-domain>/rest/workflows/
    

响应将包含新工作流的 ID,该 ID 将在下一步中使用。

  1. 最后,激活新工作流:
    1
    PATCH https://<n8n-domain>/rest/workflows/1012
    

在 JSON 负载中传递额外的 active 值:

1
2
3
4
5
// ...
"active":true,
"settings": {},
"staticData": null,
"tags": []

单一工作流#

实现此方法需要遵循四个步骤:

  • 获取每个用户的凭据,以及根据工作流可能需要提供的额外参数。请参考上文获取用户凭据部分。
  • 为该用户创建 n8n 凭据。请参考上文创建用户凭据部分。
  • 创建工作流。
  • 根据需要调用工作流。

创建工作流#

该工作流的具体内容和范围会根据实际用例有很大差异,但需要注意以下几个设计实现要点:

  • 此工作流必须由Webhook节点触发。
  • 传入的 webhook 调用必须包含用户凭据和所需的任何其他工作流参数。
  • 每个需要使用用户凭据的节点都应使用表达式,以便节点的凭据字段读取 webhook 调用中提供的凭据。
  • 保存并激活工作流,确保为 Webhook 节点选择了生产环境 URL。更多信息请参考webhook 节点

调用工作流#

对于每个新用户,或任何需要调用的现有用户,调用定义为工作流触发器的 webhook 并提供必要的凭据(以及任何其他工作流参数)。