配置#
功能可用性
Embed功能需要Embed许可证。有关何时使用Embed、费用及许可流程的更多信息,请参阅n8n网站上的Embed。
认证#
您可以通过设置用户管理来保护n8n,这是n8n内置的身份验证功能。
凭证覆盖#
为了向用户提供OAuth登录功能,可以在全局范围内覆盖credentials。这些凭证数据对用户不可见,但后端会自动使用它们。
在编辑器界面中,n8n默认隐藏所有被覆盖的字段。这意味着用户可以通过点击凭据上的"connect"按钮使用OAuth进行身份验证。
n8n提供两种应用凭证覆盖的方式:使用环境变量和使用REST API。
使用环境变量#
您可以通过设置环境变量CREDENTIALS_OVERWRITE_DATA为{ CREDENTIAL_NAME: { PARAMETER: VALUE }}来配置凭证覆盖。
警告
虽然这是可行的,但不推荐这样做。环境变量在n8n中不受保护,因此数据可能会泄露给用户。
使用REST API#
推荐的方式是通过自定义REST端点加载数据。将CREDENTIALS_OVERWRITE_ENDPOINT设置为该端点应提供的路径。
注意
出于安全考虑,这些端点每次只能调用一个。
例如:
-
通过设置n8n运行环境中的环境变量来激活端点:
1export CREDENTIALS_OVERWRITE_ENDPOINT=send-credentials -
然后需要一个包含凭证的JSON文件来进行覆盖。例如,一个用于覆盖Asana和GitHub凭证的
oauth-credentials.json文件可能如下所示:1 2 3 4 5 6 7 8 9 10
{ "asanaOAuth2Api": { "clientId": "" , "clientSecret": "" }, "githubOAuth2Api": { "clientId": "" , "clientSecret": "" } } -
然后通过使用curl发送将其应用到实例:
1curl -H "Content-Type: application/json" --data @oauth-credentials.json http://localhost:5678/send-credentials
注意
在某些情况下,凭证是基于其他凭证的。例如,googleSheetsOAuth2Api扩展了googleOAuth2Api。
在这种情况下,您可以在父凭证(googleOAuth2Api)上设置参数,供所有子凭证(googleSheetsOAuth2Api)使用。
环境变量#
n8n有许多环境变量可供配置。以下是托管解决方案中最相关的环境变量:
| 变量 | 类型 | 默认值 | 描述 |
|---|---|---|---|
EXECUTIONS_TIMEOUT |
Number | -1 |
Sets a default timeout (in seconds) to all workflows after which n8n stops their execution. Users can override this for individual workflows up to the duration set in EXECUTIONS_TIMEOUT_MAX. Set EXECUTIONS_TIMEOUT to -1 to disable. |
EXECUTIONS_DATA_PRUNE |
Boolean | true |
Whether to delete data of past executions on a rolling basis. |
EXECUTIONS_DATA_MAX_AGE |
Number | 336 |
The execution age (in hours) before it's deleted. |
EXECUTIONS_DATA_PRUNE_MAX_COUNT |
Number | 10000 |
Maximum number of executions to keep in the database. 0 = no limit |
NODES_EXCLUDE |
Array of strings | - | Specify which nodes not to load. For example, to block nodes that can be a security risk if users aren't trustworthy: NODES_EXCLUDE: "[\"n8n-nodes-base.executeCommand\", \"n8n-nodes-base.readWriteFile\"]" |
NODES_INCLUDE |
Array of strings | - | Specify which nodes to load. |
N8N_TEMPLATES_ENABLED |
Boolean | true |
Enable workflow templates (true) or disable (false). |
N8N_TEMPLATES_HOST |
String | https://api.n8n.io |
Change this if creating your own workflow template library. Note that to use your own workflow templates library, your API must provide the same endpoints and response structure as n8n's. Refer to Workflow templates for more information. |
后端钩子#
可以定义外部钩子,当特定操作运行时,n8n会执行这些钩子。例如,您可以使用这些钩子来记录数据、修改数据,或通过抛出错误来禁止某个操作。
可用钩子#
| 钩子 | 参数 | 描述 |
|---|---|---|
credentials.create |
[credentialData: ICredentialsDb] |
Called before new credentials get created. Use to restrict the number of credentials. |
credentials.delete |
[id: credentialId] |
Called before credentials get deleted. |
credentials.update |
[credentialData: ICredentialsDb] |
Called before existing credentials are saved. |
frontend.settings |
[frontendSettings: IN8nUISettings] |
Gets called on n8n startup. Allows you to, for example, overwrite frontend data like the displayed OAuth URL. |
n8n.ready |
[app: App] |
Called once n8n is ready. Use to, for example, register custom API endpoints. |
n8n.stop |
Called when an n8n process gets stopped. Allows you to save some process data. | |
oauth1.authenticate |
[oAuthOptions: clientOAuth1.Options, oauthRequestData: {oauth_callback: string}] |
Called before an OAuth1 authentication. Use to overwrite an OAuth callback URL. |
oauth2.callback |
[oAuth2Parameters: {clientId: string, clientSecret: string \| undefined, accessTokenUri: string, authorizationUri: string, redirectUri: string, scopes: string[]}] |
Called in an OAuth2 callback. Use to overwrite an OAuth callback URL. |
workflow.activate |
[workflowData: IWorkflowDb] |
Called before a workflow gets activated. Use to restrict the number of active workflows. |
workflow.afterDelete |
[workflowId: string] |
Called after a workflow gets deleted. |
workflow.afterUpdate |
[workflowData: IWorkflowBase] |
Called after an existing workflow gets saved. |
workflow.create |
[workflowData: IWorkflowBase] |
Called before a workflow gets created. Use to restrict the number of saved workflows. |
workflow.delete |
[workflowId: string] |
Called before a workflow gets delete. |
workflow.postExecute |
[run: IRun, workflowData: IWorkflowBase] |
Called after a workflow gets executed. |
workflow.preExecute |
[workflow: Workflow: mode: WorkflowExecuteMode] |
Called before a workflow gets executed. Allows you to count or limit the number of workflow executions. |
workflow.update |
[workflowData: IWorkflowBase] |
Called before an existing workflow gets saved. |
注册钩子#
通过注册包含钩子函数的钩子文件来设置钩子。
要注册钩子,请设置环境变量 EXTERNAL_HOOK_FILES。
您可以将变量设置为单个文件:
EXTERNAL_HOOK_FILES=/data/hook.js
或者包含多个以分号分隔的文件:
EXTERNAL_HOOK_FILES=/data/hook1.js;/data/hook2.js
后端钩子文件#
Hook文件是遵循以下格式的常规JavaScript文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
后端钩子函数#
钩子或钩子文件可以包含多个钩子函数,所有函数会依次执行。
如果钩子函数的参数是对象,可以通过修改该参数的数据来改变n8n的行为。
你也可以在任何钩子函数中使用this.dbCollections访问数据库(参考后端钩子文件中的代码示例)。
前端外部钩子#
与后端外部钩子类似,可以在前端代码中定义由n8n执行的外部钩子,当用户执行特定操作时触发。例如,您可以使用它们来记录数据和修改数据。
可用钩子#
| 钩子 | 描述 |
|---|---|
credentialsEdit.credentialTypeChanged |
Called when an existing credential's type changes. |
credentials.create |
Called when someone creates a new credential. |
credentialsList.dialogVisibleChanged |
|
dataDisplay.nodeTypeChanged |
|
dataDisplay.onDocumentationUrlClick |
Called when someone selects the help documentation link. |
execution.open |
Called when an existing execution opens. |
executionsList.openDialog |
Called when someone selects an execution from existing Workflow Executions. |
expressionEdit.itemSelected |
|
expressionEdit.dialogVisibleChanged |
|
nodeCreateList.filteredNodeTypesComputed |
|
nodeCreateList.nodeFilterChanged |
Called when someone makes any changes to the node panel filter. |
nodeCreateList.selectedTypeChanged |
|
nodeCreateList.mounted |
|
nodeCreateList.destroyed |
|
nodeSettings.credentialSelected |
|
nodeSettings.valueChanged |
|
nodeView.createNodeActiveChanged |
|
nodeView.addNodeButton |
|
nodeView.createNodeActiveChanged |
|
nodeView.mount |
|
pushConnection.executionFinished |
|
showMessage.showError |
|
runData.displayModeChanged |
|
workflow.activeChange |
|
workflow.activeChangeCurrent |
|
workflow.afterUpdate |
Called when someone updates an existing workflow. |
workflow.open |
|
workflowRun.runError |
|
workflowRun.runWorkflow |
Called when a workflow executes. |
workflowSettings.dialogVisibleChanged |
|
workflowSettings.saveSettings |
Called when someone saves the settings of a workflow. |
注册钩子#
You can set hooks by loading the hooks script on the page. One way to do this is by creating a hooks file in the project and adding a script tag in your editor-ui/public/index.html file:
1 | |
前端钩子文件#
前端外部钩子文件是遵循以下格式的常规JavaScript文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
前端钩子函数#
您可以为每个钩子定义多个钩子函数。每个钩子函数调用时会传入以下参数:
store: Vuex存储对象。您可以使用它来更改或从存储中获取数据。metadata: 包含钩子提供的任何数据的对象。要查看传递的内容,请在editor-ui包中搜索该钩子。