rq.collectionVariables object provides methods to manage collection variables during script execution. Collection variables are scoped to a specific collection and are only accessible within the requests that belong to that collection. Unlike environment variables (scoped to a specific environment), collection variables persist across all environments within the same collection.
Which collection a script writes to
Requestly has sub-collections, so a request nested in a folder belongs to several collections at once: its immediate parent, that folder’s parent, and so on up to the root collection. Reading and writing use different rules, and both are deliberate:- Reading looks up the chain and the nearest collection wins. A request in
My Collection › Auth › LoginseesAuth’s value for a name if there is one, otherwise the root collection’s. - Writing targets the root collection by default, no matter how deeply the request is nested.
{ level: "current" }.
level accepts only "root" and "current". There is no way to name a collection in between. Collection names are not stable identifiers, and scripts have no access to collection IDs.
If the same name is declared on both a sub-collection and the root, a default write updates the root while the sub-collection’s copy keeps winning on read, so
get will still return the old value. Either use { level: "current" } to update the copy you are reading, or remove the duplicate declaration from the sub-collection.Methods
rq.collectionVariables.set(key, value, options?)
Creates or updates a collection variable with the given key and value. If the variable already exists, it will be updated with the new value.
Parameters:
key(string): The name of the collection variablevalue(any): The value to store. Scalar values are stored as strings; passing an array stores a real array, so a latergetreturns the array unchanged. Passingnullorundefinedclears the variable, which is equivalent tounset(key), so a latergetreturnsundefined(not the string"null").options(object, optional):{ level }:"root"(default) writes to the root collection,"current"writes to the collection the request sits directly inside. See Which collection a script writes to.
Passing an array to
set stores a real array, and get returns it as a real array with .first() and .last() helpers. See rq.environment for examples.rq.collectionVariables.get(key)
Retrieves the value of the specified collection variable.
Parameters:
key(string): The name of the collection variable to retrieve
undefined if it doesn’t exist.
Example:
rq.collectionVariables.unset(key, options?)
Removes the specified collection variable.
Parameters:
key(string): The name of the collection variable to removeoptions(object, optional):{ level }:"root"(default) or"current", the same asset(). Removes the copy held by that collection.
rq.collectionVariables.clear(options?)
Removes every variable declared on the target collection. Variables inherited from another collection in the chain are left alone and stay readable.
Parameters:
options(object, optional):{ level }:"root"(default) clears the root collection’s own variables,"current"clears those of the collection the request sits directly inside.
On a request that does not belong to any collection there is nowhere to write, so
set(), unset(), and clear() do nothing. They fail silently rather than throwing, so a pre-request script containing one still sends.Common Use Cases
Store API Base Path
Set a base path that all requests in the collection can use:Share Data Between Collection Requests
Pass data from one request to another within the same collection:Store Default Values
Set default values that can be overridden by environment variables:Track Collection State
Maintain state across requests in a collection:Store Computed Values
Calculate and store values that multiple requests will use:Best Practices
-
Naming Convention: Use clear, descriptive names that indicate the variable’s purpose
- Initialize in Setup Requests: Create a setup or initialization request that sets default collection variables
-
Validate Before Use: Check if a variable exists before using it
-
Clean Up: Remove variables that are no longer needed
-
Document Variables: Add comments in your scripts explaining what each variable is for

