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.
Methods
rq.variables.get(key)
Returns the value of a variable from the first scope that defines it.
Parameters:
key(string): The variable’s name
undefined if no scope has an enabled variable with that name. A number, boolean or array variable comes back as that type, and a data file value keeps the type the file gave it. A variable you set earlier in the same script comes back as set stored it.
Example:
rq.variables.set(key, value, type?)
Creates or updates a runtime variable.
Parameters:
key(string): The variable’s name. It must not be empty.value(any): The value to store. A runtime variable that already exists converts the value to its type:set("count", "42")on a number variable stores42. A new variable keeps the value exactly as you pass it.nullorundefinedremoves the variable, the same asunset(key).type(string, optional): The type to give the variable before the value is stored:"string","number","boolean","array","object"or"any". You can also pass{ type }.
rq.variables.has(key)
Checks whether any scope has a variable with this name that is enabled.
Parameters:
key(string): The variable’s name
true if some scope has the variable and it is enabled, otherwise false.
Example:
rq.variables.unset(key)
Removes a runtime variable, so get reads the name from the next scope down. A disabled variable is left alone. A value you typed into your runtime variables yourself is not deleted, and later requests read it again.
Parameters:
key(string): The variable’s name. It must not be empty.
rq.variables.clear()
Removes every runtime variable, disabled ones included, so get reads each name from the other scopes. Values you typed into your runtime variables yourself are not deleted, and later requests read them again.
Example:
rq.variables.toObject()
Returns every enabled variable the script can read as one object of names and values. When scopes define the same name, the scope get searches first wins.
Returns: An object such as { baseUrl: "https://api.example.com", authToken: "abc" }. Each value has the type get returns for it.
Example:
rq.variables.replaceIn(template)
Replaces each {{name}} in a template with the value of that variable, using the same search order as get.
Parameters:
template(string, object or array): Text containing{{name}}references. In an object or an array, every string inside is replaced.
String() writes it, so an array becomes a,b. A name no scope defines stays exactly as written, so you can see what was missing. A dynamic variable such as {{$guid}} also stays as written: call rq.$guid() and build the string yourself. Each reference is replaced once, so a {{name}} inside a value stays as written.
Example:
rq.variables.toObjectResolved(scope?, overrides?)
Returns a plain copy of rq.variables, the same as toJSON(), with every {{name}} inside it replaced. For a plain object of names and values, use toObject().
Parameters:
scope(object, optional): The object whosereplaceInreplaces the references. Defaults torq.variables, which reads every scope.overrides(array of objects, optional): Objects whose properties are copied onto the result, in order.
id, values and mutations.
Example:
rq.variables.toJSON()
Returns a plain copy of rq.variables. JSON.stringify(rq.variables) uses it.
Returns: An object with:
id: The same asrq.variables.idvalues: The runtime variables, as an array of{ key, value, type }items, withdisabled: trueon a disabled variable. An array variable’svalueis its JSON text.mutations: The changes this script recorded, as{ stream, compacted, autoCompact }
rq.variables.syncVariablesFrom(object, track?)
Copies the names and values of a plain object into the runtime variables. An existing runtime variable gets the new value, converted to its type, and a new name becomes a new runtime variable. Variables the object does not name are kept. The changes are saved the same way as set, but are not recorded in mutations.
Parameters:
object(object): The names and values to copy intrack(boolean, optional): Set totrueto get back what changed
track set to true, an object that maps each new name to "created" and each name whose value changed to "updated". A name whose value did not change is not listed. Without track, undefined.
Example:
rq.variables.syncVariablesTo(object?)
Copies every runtime variable, disabled ones included, into a plain object. Variables from the other scopes are not copied.
Parameters:
object(object, optional): The object to copy into. Any key in it that the runtime variables do not hold is deleted from it. Without it, a new object is used.
get returns for it.
Example:
rq.variables.values
The runtime variables, as a list. get also reads the other scopes, but their variables are not in this list. Unlike get, has and toObject, the list includes disabled variables.
Each item is one variable with key, value, type and disabled. Call the item’s get() to read its value as its type, since an array variable’s value is its JSON text, and its set(value) to change it.
The list has these methods:
- Read:
one(key)returns the variable with that name, andget(key)its value.has(key)checks whether the list holds that name.count()returns the number of variables,all()an array of them,idx(index)the one at a position, andindexOf(key)its position, or-1.toObject()returns an object of names and values, andtoJSON()an array of plain{ key, value, type }items. - Loop:
each(fn),map(fn),filter(fn),find(fn)andreduce(fn, initial)work like the array methods of the same name.fnreceives each variable and its position. - Change:
add({ key, value, type, disabled })adds a variable.upsert({ key, value, type, disabled })updates the variable with that name, or adds it when there is none.remove(key)removes the variable with that name, andremove(fn)removes every variablefnreturnstruefor.clear()removes every variable. - Text:
replace(text)replaces each{{name}}intextwith the list’s value for that name. - Copy:
syncFromObject(object, track?, prune?)works likesyncVariablesFrom, and withpruneset totrueit also removes the variables the object does not name.syncToObject(object?)works likesyncVariablesTo.
values are saved the same way as set, but are not recorded in mutations.
Example:
rq.variables.id
A random id, new each time a script runs.
Example:
rq.variables.mutations
A record of the set and unset calls this script made on rq.variables, in order. Recording is on from the start of the script. clear() records an unset for each variable it removes, and applyMutation records the call it makes. Changes made through values or syncVariablesFrom are not recorded.
stream(array): One entry per call:[key, value]forsetand[key]forunset.valueis what you passed, before any conversion.compacted(object): The last change to each name, filled bycompact()or byautoCompactcount(): Returns the number of recorded changescompact(): Keeps only the last change to each name, moving the entries fromstreamintocompactedapplyOn(target): Makes the recorded changes again on another object, such asrq.environment
rq.variables.enableTracking(options?)
Starts recording set and unset calls in mutations. Recording is already on when a script starts, so calling this does nothing until you call disableTracking(). After that, it starts a new, empty record.
Parameters:
options(object, optional):{ autoCompact: true }keeps only the last change to each name, inmutations.compacted
rq.variables.disableTracking()
Stops recording changes in mutations. What was already recorded stays in mutations.
Example:
rq.variables.applyMutation(instruction, key, value?)
Makes one change, named by instruction: "set" calls set(key, value) and "unset" calls unset(key), so the change goes to the runtime variables. Any other instruction does nothing.
Parameters:
instruction(string):"set"or"unset"key(string): The variable’s namevalue(any, optional): The value, for"set"
rq.variables.addLayer(list)
Adds a list of variables for get, has, toObject and replaceIn to read last, after the global variables. Writes still go to the runtime variables only. Every scope’s values is already read, so adding one changes nothing.
Parameters:
list: Another object’svalues, such asrq.globals.values
How values are saved
Runtime values are saved the same way asrq.environment values, with one difference: they last for the session only. Within one script, get returns the value set stored, and the next request reads the saved form.
A value you set from a script 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.
See Typed variables for how a variable’s type converts the value you set, and Disabled variables for how disabled variables behave.
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:

