Skip to main content
The 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 › Login sees Auth’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.
Writing to the root by default is what lets one request share a value with a request in a different folder: the common “log in here, use the token there” pattern. A sibling folder is never on another folder’s read path, so a value written to a folder would be invisible to it. To write to the collection the request sits directly inside, pass { 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 variable
  • value (any): The value to store. Scalar values are stored as strings; passing an array stores a real array, so a later get returns the array unchanged. Passing null or undefined clears the variable, which is equivalent to unset(key), so a later get returns undefined (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.
Example:
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
Returns: The value of the collection variable, or 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 remove
  • options (object, optional): { level }: "root" (default) or "current", the same as set(). Removes the copy held by that collection.
Example:

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.
Example:
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:
Then use it in your request URL:

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

  1. Naming Convention: Use clear, descriptive names that indicate the variable’s purpose
  2. Initialize in Setup Requests: Create a setup or initialization request that sets default collection variables
  3. Validate Before Use: Check if a variable exists before using it
  4. Clean Up: Remove variables that are no longer needed
  5. Document Variables: Add comments in your scripts explaining what each variable is for