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 are shared between all routes of an environment. Their values are reset when the environment is stopped or restarted.
⚠️These variables are not shared between environments.
Global variables are available everywhere templating helpers are supported. However, it makes most sense to use them in the response body and rules.
💡 While you can use global variables in data buckets, keep in mind that bucket content will be generated when the server starts, when global variables are not yet set.
To set and access global variables, two templating helpers are available.
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
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<!-- Using object-path syntax --> <!-- using JSONPath syntax --> ... ... <!-- Stringify the variable content -->
To clear all the global variables, use the PURGE /mockoon-admin/global-vars
or POST /mockoon-admin/global-vars/purge
endpoints from the admin API.