Global Variables

Sets or retrieves workflow-scoped global variables using workflowStaticData

Overview

This node allows users to set or retrieve global variables within an n8n workflow. Global variables are stored in the workflow's static data, making them accessible across different executions and nodes within the same workflow. This is useful for maintaining state, counters, flags, or any data that needs to persist beyond a single execution.

Common scenarios:

  • Storing a counter that increments each time the workflow runs.
  • Saving user preferences or session data to be accessed by multiple nodes.
  • Retrieving configuration values set earlier in the workflow.

Example:

  • Use the "Set Variable" operation to store a JSON object representing user details.
  • Later, use the "Get Variable" operation to retrieve this data and make decisions based on it.

Properties

Name Meaning
Operation Choose between "Set Variable" (store a value) or "Get Variable" (retrieve a value).
Variable Name The unique, case-sensitive name of the global variable to set or get (e.g., myCounter).
Value (Only for "Set Variable") The value to store in the global variable. Can be simple or JSON.

Output

The node outputs an array of items corresponding to the input items:

  • For Set Variable:

    • The output JSON includes all original input fields plus a _globalVariableSet object containing:
      • name: the variable name set.
      • value: the value stored.
      • status: always "success" if no error occurred.
  • For Get Variable:

    • The output JSON includes all original input fields plus a new field named after the variable name containing its stored value.

If an error occurs and the node is configured to continue on failure, the output item will contain an error field with the error message and the original input item under originalItem.

The node does not output binary data.

Dependencies

  • No external services or API keys are required.
  • Uses n8n's built-in workflow static data storage to persist variables.

Troubleshooting

  • Empty Variable Name: The node throws an error if the variable name is empty. Ensure you provide a valid, non-empty variable name.
  • Invalid JSON in Value: When setting a variable, if the value is a string that looks like JSON, the node attempts to parse it. Malformed JSON strings may cause unexpected behavior.
  • Variable Not Found: When getting a variable that was never set, the output will include the variable name with an undefined or null value.
  • Continue On Fail: If enabled, errors do not stop the workflow but are returned as part of the output for easier debugging.

Links and References

Discussion