Templating variables helpers


Mockoon offers the following helpers which can help you set local or global variables, and access environment variables in your templates:

 setVar

Set a variable to be used later in the template. The value can be the result of another helper. To use it elsewhere in the template, refer to the variable with its name prefixed with an @: {{@varname}}. The variable can also be used as a helper parameter: {{#repeat @varname}}...{{/repeat}}. Variables declared in a block helper will be scoped to the block and unavailable outside.

Arguments (ordered)TypeDescription
0stringVariable name
1anyVariable value

Examples

Copy
{{setVar 'varname' 'value'}} {{setVar 'varname' (body 'id')}} usage: {{@varname}} {{#repeat @varname}}...{{/repeat}} declare a variable in a block helper: {{#repeat 5}} {{setVar 'random' (oneOf (array '1' '2' '3'))}} {{@random}} {{/repeat}} {{setVar 'myArray' (array '1' '2' '3')}} {{#each @myArray}} {{setVar 'eachIndex' @index}} {{@eachIndex}} {{/repeat}}

 getVar

Dynamically get a variable set with setVar.

Arguments (ordered)TypeDescription
0stringVariable name

Examples

Copy
{{setVar 'varname' 'value'}} {{getVar 'varname'}} {{getVar (concat 'var' 'name')}} {{getVar (body 'property')}}

 setGlobalVar

Set a global variable to be used anywhere templating is supported (body, headers, etc.). Global variables are available on all the routes of an environment and they are reset when the environment is restarted.

  • The variable name and values can be dynamically created using other helpers.
  • The variable can store any kind of data (arrays, objects, string, etc.).
  • To get the value of a global variable, use the {{getGlobalVar 'varName'}} helper below.
Arguments (ordered)TypeDescription
0stringVariable name
1anyVariable value

Examples

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

 getGlobalVar

Get a global variable's value set with setGlobalVar. Global variables are available on all the routes of an environment and they are reset when the environment is restarted.

  • The variable name and path can be dynamically created using other helpers.
  • The path supports two syntaxes, object-path or JSONPath Plus. When using object-path, properties containing dots are supported by escaping the dots: key.key\.with\.dot.
    Please note that a value can be retrieved at the path if the variable contains valid JSON.
  • Primitives and data structures can be retrieved by the helper and reused in other helpers (see example below).
  • The full variable content (array, object, etc.) can be fetched when the path is omitted ({{getGlobalVar 'varname'}}).

🛠️ Use our online JSONPath and object-path evaluator to test your JSONPath or object-path syntaxes and view the results in real-time.

Arguments (ordered)TypeDescription
0stringVariable name
1stringPath to the value property

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')}}}

 getEnvVar

Get an environment variable. If the environment variable is not set, the default value will be used.

By default, only environment variables with the MOCKOON_ prefix are available. Learn more in the environment variables documentation.

Arguments (ordered)TypeDescription
0stringEnvironment variable name
1anyDefault value

Examples

Copy
{{setVar 'varname' 'value'}} {{getEnvVar 'VARIABLE_NAME' 'default value'}} {{getEnvVar 'VARIABLE_NAME'}} {{getEnvVar (body 'property')}}