Global variables


Mockoon offers the possibility to share data between your routes using global variables. These variables can be set and accessed using the setGlobalVar and getGlobalVar helpers at runtime.

 Global variables scope

Global variables are shared between all routes of an environment. Their values are reset when the environment is stopped or restarted.

 Global variables support

Global variables are available everywhere templating helpers are supported. However, it makes most sense to use them in the response body and rules.

 Usage

To set and access global variables, two templating helpers are available.

 Set a global variable

To set a global variable, use the setGlobalVar helper. This helper takes two arguments: the variable name and its value. You can dynamically set the parameters using other helpers.

Some examples:

Copy
{{setGlobalVar 'varName' 'value'}} {{setGlobalVar 'varName' (bodyRaw 'id')}} {{setGlobalVar (queryParam 'param1') (bodyRaw 'id')}}

 Get a global variable

To get a global variable, use the getGlobalVar helper. This helper takes two arguments: the variable name and an optional path. You can dynamically set the parameters using other helpers and use the fetched data in other helpers. Some examples:

Copy
{{getGlobalVar 'varname'}} {{getGlobalVar (bodyRaw 'property')}} {{getGlobalVar (urlParam 'id')}} <!-- Using object-path syntax --> {{getGlobalVar 'varName' 'path.to.property'}} {{getGlobalVar 'varName' 'deep.property\.with\.dot'}} <!-- using JSONPath syntax --> {{getGlobalVar 'varName' '$.array.[*].property'}} {{#repeat (getGlobalVar 'varname')}}...{{/repeat}} {{#each (getGlobalVar 'varName')}}...{{/each}} <!-- Stringify the variable content --> {{{stringify (getGlobalVar 'varName')}}}