Skip to content

自定义变量(Custom variables)#

功能可用性

  • 可用于自托管企业版和专业云计划。
  • 仅实例所有者和管理员可以创建变量。

自定义变量是只读变量,可用于在 n8n 工作流中存储和重用值。

🌐 Custom variables are read-only variables that you can use to store and reuse values in n8n workflows.

变量作用域和可用性

  • 全局变量 在你的 n8n 实例中对所有人可用,适用于所有项目。
  • 项目范围变量 仅在它们创建的特定项目中可用。
  • 项目范围变量从 1.118.0 版本开始可用。之前的版本仅支持可从左侧菜单访问的全局变量。

创建变量(Create variables)#

你可以从概览页面或特定项目访问 变量 选项卡。

🌐 You can access the Variables tab from either the overview page or a specific project.

要创建新变量:

🌐 To create a new variable:

  1. 变量选项卡上,选择添加变量
  2. 输入一个。键的最大长度为50个字符,值的最大长度为1000个字符。n8n限制键和值中可使用的字符为小写和大写字母、数字及下划线(A-Za-z0-9_)。
  3. 选择 范围(仅在从概览页面创建时可用):
    • 全局:该变量在 n8n 实例中的所有项目中均可用。
    • 项目:该变量仅在特定项目中可用(你可以选择项目)。
    • 从项目页面创建时,范围会自动设置为该项目。
  4. 选择 保存。该变量现在可以根据其作用范围在工作流程中使用。

编辑和删除变量(Edit and delete variables)#

要编辑或删除变量:

🌐 To edit or delete a variable:

  1. 变量 标签上,悬停在你想要更改的变量上。
  2. 选择 编辑删除

在工作流中使用变量(Use variables in workflows)#

你可以在代码节点和表达式中访问变量:

🌐 You can access variables in the Code node and in expressions:

1
2
// Access a variable
$vars.<variable-name>

所有变量均为字符串。

🌐 All variables are strings.

在工作流执行过程中,n8n 会将变量替换为变量值。如果变量没有值,n8n 会将其值视为 undefined。在这种情况下,工作流不会自动失败。

🌐 During workflow execution, n8n replaces the variables with the variable value. If the variable has no value, n8n treats its value as undefined. Workflows don't automatically fail in this case.

变量优先级

当一个项目作用域的变量与全局变量具有相同的键时,项目作用域变量的值优先,并在该项目的工作流中覆盖全局变量的值。

变量是只读的。你必须使用界面来更改这些值。如果你需要在工作流程中设置和访问自定义数据,请使用 工作流程静态数据

🌐 Variables are read-only. You must use the UI to change the values. If you need to set and access custom data within your workflow, use Workflow static data.