Skip to main content
The rq.variables object is the one accessor that looks in every scope. Use it when you want a value and do not care which scope holds it, which is most of the time. The scope specific objects (rq.environment, rq.collectionVariables, rq.globals, rq.iterationData) each read one scope only. Reads search the scopes in this order, and the first one that defines the name wins:
A disabled variable, or one you removed earlier in the same run, is skipped, so the next scope down shows through. Writes always go to the runtime scope, whichever scope the value originally came from. rq.variables.set never edits your environment, collection, or global variables. To change one of those, use its own object.
Migrating from Postman? rq.variables matches pm.variables: same search order, same runtime scoped writes. Importing a collection converts pm.variables calls for you.

Methods

rq.variables.get(key)

Returns the value of a variable from the first scope that defines it. Parameters:
  • key (string): The name of the variable
Returns: The value, or undefined if no scope defines it. Number and boolean typed variables come back as numbers and booleans, and array typed variables as real arrays. Example:

rq.variables.has(key)

Checks whether any scope defines the variable. Parameters:
  • key (string): The name of the variable
Returns: true if some scope defines it, otherwise false. Example:

rq.variables.set(key, value)

Sets a runtime variable. If a variable of that name already exists in the runtime scope, this overrides its value for the current session. Parameters:
  • key (string): The name of the variable
  • value (any): The value to store. Passing null or undefined clears it, the same as unset(key).
Example:
Script writes are temporary. A value you set from a script lives for the session and is never saved back to your runtime variables list, so a reload restores whatever you typed there yourself. If you need a value to survive, write it to the environment, the collection, or globals instead.

rq.variables.unset(key)

Removes the script’s runtime value for a variable. If you typed a value for that name yourself, yours shows through again. Parameters:
  • key (string): The name of the variable to remove
Example:

rq.variables.clear()

Removes every runtime value the script has set. Parameters: none. Example:

rq.variables.toObject()

Returns every variable visible to the script as a single object, with higher priority scopes overwriting lower ones. Returns: An object of name and value pairs. Values are typed the same way get returns them. Example:

rq.variables.replaceIn(template)

Substitutes {{name}} references in a string, using the same search order as get. Parameters:
  • template (string): A string containing {{name}} references
Returns: The string with every reference it could resolve replaced. A name no scope defines is left exactly as written, so you can see what was missing. Example:
replaceIn resolves stored variables only. Three things it does not do:
  • Dynamic variables stay as text. rq.variables.replaceIn("{{$guid}}") returns the literal {{$guid}}. Call rq.$guid() and build the string yourself.
  • One pass, so no nesting. If a variable’s own value contains {{another}}, the inner reference is left as written.
  • Strings only. An object or an array passed in comes back unchanged.
replaceIn is available on rq.variables only, not on the scope specific objects.

Common Use Cases

Read a value without knowing its scope

The usual reason to reach for rq.variables. The same script keeps working after someone moves a variable from the collection to the environment:

Fail early with a clear message

Pass a value to the next request

Runtime scope is the right home for a value that only bridges one request to the next:

Build a URL from a template

Override a data file column for one run

A value you set from a script beats a column of the same name in the Collection Runner’s data file, which is what lets a pre-request script refresh an expiring token seeded from the file:

Choosing between rq.variables and a scope specific object