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:
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
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
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 variablevalue(any): The value to store. Passingnullorundefinedclears it, the same asunset(key).
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
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
Common Use Cases
Read a value without knowing its scope
The usual reason to reach forrq.variables. The same script keeps working after someone moves a variable from the collection to the environment:

