Skip to main content
Skip table of contents

Global, Flow and Environment Variables

Each flow contains one or multiple chains of nodes that branch, are combined with subflows, or linked to different flows. Managing variables and scopes of access to these variables can have different approaches.

Using function node to access global and flow variables

You can add context data with global and flow variables. Those can be created programmatically in the function node. Those variables can be set and fetched with two commands. In most cases, using the global variables is fitting most use cases.

Global Context

Flow Context

Description

Variables are accessible by every node across any flow tab within the same flow app.

Variables are only accessible by nodes on the same flow tab.

Set a variable with JavaScript

Use the global.set method to create or update a variable.

JS
// 1. Setting (or updating) a global variable
global.set('myGlobalVariable', 'Hello World');

// 2. Setting a nested global variable
//    Use '.' to create a hierarchical structure for easy grouping
global.set('user.name', 'Alice');

Use the flow.set method to create or update a variable.

JS
// 1. Setting (or updating) a flow variable
flow.set('myFlowVariable', 'Hello World');

// 2. Setting a nested global variable
//    Use '.' to create a hierarchical structure for easy grouping
flow.set('user.name', 'Alice');

Retrieve a variable with JavaScript

Use the global.get method to retrieve a variable from global context.

JS
// 3. Retrieving global variables
const myGlobalVar = global.get('myGlobalVariable');
const userName = global.get('user.name');
const mySecret = global.get('SECRETS.mySecret')

Use the flow.get method to retrieve a variable from flow context.

JS
// 3. Retrieving flow variables
const myFlowVar = flow.get('myFlowVariable');
const userName = flow.get('user.name');

When you use qibb’s Secret Manager, it automatically creates the nested global variables SECRETS and SECRETS_METADATA in your flow, making it easy to reference your secrets. Those variables are designed to be readonly and can only be updated from the Space Secrets Tab in the Portal.

For more information, see Managing secrets in your space and accessing them from your flows.

Using JSONATA to access global and flow variables

Certain nodes allow you to apply JSONATA, such as the qibb API nodes, inject or change nodes. You can read global and flow variables using JSONATA expressions:

qibb_api_node_jsonata_json_body.png

Use JSONATA expressions in qibb API nodes to easily access and map global variables directly within the JSON Body.

Global Context

Flow Context

Description

Variables are accessible by every node across any flow tab within the same flow app.

Variables are only accessible by nodes on the same flow tab.

Retrieve a variable with JSONATA

Use the $globalContext("NameOfYourVariable") method to retrieve a variable from global context.

JS
{  
   "client_id": $globalContext("SECRETS.Box_Client_ID"),
   "client_secret": $globalContext("SECRETS.Box_Client_Secret")
}

Use the $flowContext("NameOfYourVariable") method to retrieve a variable from flow context.

JS
{  
   "some_config": $flowContext("myFlowVariable"),
}

View global and flow variables in Sidebar

You can check existing flow or global variables directly from the right sidebar of the Flow Editor. Navigate to “Context Data” tab and click on the “🔄Refresh” button to see the variables.

view_global_variables_in_context_sidebar.png

View variables in “Context” tab within right sidebar

Working with environment variables

Environment variables can be added to subflows, see Working with subflows . New variables can be added to their Node options. The environment variables are then accessible in every node in the subflow. They can be used as variables in a function node or as JSONata expressions.

JS
//JavaScript
msg.payload = env.get('EnvVar')

//JSONata
$env('EnvVar')

Some nodes, like the Change node, provide the option to set or change the value of environment variables with the env variable option as part of a selection of data types.

Environment variables help to control the information that is available in a subflow. When working with multiple flows and subflows, controlling variables that are accessible at any given point in your project can get complicated. Using environment variables in subflows helps set scopes for values and structure the flow of data through a flow of nodes.

Configuration nodes

The context data can be used to create configuration nodes. Configuration nodes can store information that should be available to every node in the flow or project. The advantage of a configuration node is having a dedicated space for input values. Instead of updating hardcoded information in every node in your project, you can set them up to use context variables and edit only the configuration node if the values have to be updated.

Node-RED creates its configuration nodes. Those are automatically created. Users can't create a configuration node outside of the workspace.

You can create the functionality of a configuration node in different ways. You can use every node that alters or creates context data. Both Change and Function nodes let you access context data. You can add them at the start of your flow to set the values every time the flow gets triggered. Alternatively, you can place these nodes into their separate flow or disconnected them from the other nodes. You only have to trigger them to store the values. A simple trigger would be manual with an Inject node.

The Config node, which is preinstalled but not part of the core nodes of Node-RED, provides the exact functionality of a configuration node. You can set global and flow values. The node doesn't have to be triggered. The variables are created and stored immediately after a redeployment. You can set up multiple nodes and switch between them by setting the node you want to use to activate and deactivate the other configuration nodes.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.