rq.globals object reads and changes global variables from a script. Global variables work like environment variables, but every request in every collection and environment can read them. This makes them the place for configuration or state that needs to be shared everywhere.
Methods
rq.globals.get(key)
Returns the value of a global variable.
Parameters:
key(string): The variable’s name
undefined if the variable does not exist or is disabled. A number, boolean or array variable comes back as that type. A variable you set earlier in the same script comes back as set stored it.
Example:
rq.globals.set(key, value, type?)
Creates or updates a global variable.
Parameters:
key(string): The variable’s name. It must not be empty.value(any): The value to store. A 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.globals.has(key)
Checks whether a global variable exists and is enabled.
Parameters:
key(string): The variable’s name
true if the variable exists and is enabled, otherwise false.
Example:
rq.globals.unset(key)
Removes a global variable. A disabled variable is left alone.
Parameters:
key(string): The variable’s name. It must not be empty.
rq.globals.clear()
Removes every global variable, disabled ones included.
Example:
rq.globals.toObject()
Returns the enabled global variables as one object of names and values. Disabled variables are left out.
Returns: An object such as { appVersion: "1.0.0", maxRetries: 3 }. Each value has the type get returns for it.
Example:
rq.globals.replaceIn(template)
Replaces each {{name}} in a template with the value of that global variable.
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 with no enabled variable, and a dynamic variable such as {{$guid}}, stays as written. Each reference is replaced once, so a {{name}} inside a value stays as written. To replace names from every scope, use rq.variables.replaceIn.
Example:
rq.globals.toObjectResolved(scope?, overrides?)
Returns a plain copy of rq.globals, 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. Passrq.variablesto replace names from every scope. Defaults torq.globals.overrides(array of objects, optional): Objects whose properties are copied onto the result, in order.
id, values and mutations.
Example:
rq.globals.toJSON()
Returns a plain copy of rq.globals. JSON.stringify(rq.globals) uses it.
Returns: An object with:
id: The same asrq.globals.idvalues: An array of{ key, value, type }items, one per variable, withdisabled: trueon a disabled variable. An array variable’svalueis its JSON text.mutations: The changes this script recorded, as{ stream, compacted, autoCompact }
rq.globals.syncVariablesFrom(object, track?)
Copies the names and values of a plain object into the global variables. An existing variable gets the new value, converted to its type, and a new name becomes a new 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.globals.syncVariablesTo(object?)
Copies every global variable, disabled ones included, into a plain object.
Parameters:
object(object, optional): The object to copy into. Any key in it that the global variables do not hold is deleted from it. Without it, a new object is used.
get returns for it.
Example:
rq.globals.values
Your global variables, as a 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.globals.id
The id of your global variables.
Example:
rq.globals.mutations
A record of the set and unset calls this script made on rq.globals, 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.globals.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.globals.disableTracking()
Stops recording changes in mutations. What was already recorded stays in mutations.
Example:
rq.globals.applyMutation(instruction, key, value?)
Makes one change, named by instruction: "set" calls set(key, value) and "unset" calls unset(key). Any other instruction does nothing.
Parameters:
instruction(string):"set"or"unset"key(string): The variable’s namevalue(any, optional): The value, for"set"
rq.globals.addLayer(list)
Adds a list of variables to read when the global variables have no enabled variable of that name. get, has, toObject and replaceIn read the lists in the order you add them. Writes still go to the global variables only.
Parameters:
list: Another object’svalues, such asrq.environment.values
How values are saved
rq.globals saves values the same way as rq.environment. Within one script, get returns the value set stored. When the script ends, the value is saved, and the next request reads the saved form: an object, for example, comes back as its JSON text.
See How values are saved for every kind of value, Typed variables for how a variable’s type converts the value you set, and Disabled variables for how disabled variables behave. Array variables work as described in Working with array variables.
Common Use Cases
Store Application Version
Keep track of the application or API version being tested:Track Global State
Maintain state that needs to be shared across all collections:Store User Preferences
Keep user preferences that apply globally:Global Timestamp
Set a global timestamp that all requests can reference:Feature Flags
Turn features on or off for every request. Comparing the text works whether the flag is a boolean or a string variable:Store Common Constants
Define constants that are used across all collections:Variable Scope Hierarchy
When the same name exists in several scopes,{{name}} in a request and rq.variables.get read the most specific one:
Best Practices
- Use Sparingly: Reserve global variables for truly global data. Prefer collection or environment variables when possible.
-
Naming Convention: Use clear, uppercase names for global constants
-
Document Purpose: Comment your global variable usage
-
Check Existence: Verify a variable exists before using it
- Initialize Early: Set global variables in a dedicated setup script or request
-
Clean Up: Remove globals once you are done with them
-
Type Conversion: A number, boolean or array variable comes back as that type, and a string variable as a string. Convert a string variable when you need something else:

