Skip to content

自定义变量#

¥Custom variables

Feature availability

  • 适用于自托管企业版和云专业版套餐。

¥Available on Self-hosted Enterprise and Pro Cloud plans.

  • 只有实例所有者和管理员才能创建变量。

¥Only instance owners and admins can create variables.

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

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

Variable scope and availability

  • 全局变量在你的 n8n 实例上对所有项目的用户都可用。

¥Global variables are available to everyone on your n8n instance, across all projects.

  • 项目作用域变量仅在创建它们的特定项目中可用。

¥Project-scoped variables are available only within the specific project they're created in.

  • 项目作用域变量在 1.118.0 及更高版本中可用。以前的版本仅支持可从左侧菜单访问的全局变量。

¥Project-scoped variables are available in 1.118.0 and above. Previous versions only support global variables accessible from the left side menu.

创建变量#

¥Create variables

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

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

要创建新变量:

¥To create a new variable:

  1. 在“变量”选项卡中,选择“添加变量”。

¥On the Variables tab, select Add Variable. 2. 输入键和值。键的最大长度为 50 个字符,值的最大长度为 1000 个字符。n8n 限制键和值中可使用的字符,仅允许使用小写字母、大写字母、数字和下划线(A-Za-z0-9_)。

¥Enter a Key and Value. The maximum key length is 50 characters, and the maximum value length is 1000 characters. n8n limits the characters you can use in the key and value to lowercase and uppercase letters, numbers, and underscores (A-Z, a-z, 0-9, _). 3. 选择范围(仅在从概览页面创建时可用):

¥Select the Scope (only available when creating from the overview page):

  • 全局:该变量在 n8n 实例中的所有项目中均可用。

    ¥Global: The variable is available across all projects in the n8n instance.

  • 项目:该变量仅在特定项目中可用(你可以选择要使用的项目)。

    ¥Project: The variable is available only within a specific project (you can select which project).

  • 从项目页面创建时,范围会自动设置为该项目。

    ¥When creating from a project page, the scope is automatically set to that project. 4. 选择“保存”。该变量现在可以根据其作用域在工作流中使用。

¥Select Save. The variable is now available for use in workflows according to its scope.

编辑和删除变量#

¥Edit and delete variables

要编辑或删除变量:

¥To edit or delete a variable:

  1. 在“变量”选项卡中,将鼠标悬停在要更改的变量上。

¥On the Variables tab, hover over the variable you want to change. 2. 选择“编辑或删除”。

¥Select Edit or Delete.

在工作流中使用变量#

¥Use variables in workflows

你可以在“代码”节点和 expressions 节点中访问变量:

¥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.

Variable precedence

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

¥When a project-scoped variable has the same key as a global variable, the project-scoped variable value takes precedence and overrides the global variable value within that project's workflows.

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

¥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.