Skip to main content
The rq.globals object provides methods to manage global variables during script execution. Global variables work similarly to environment variables, but they are available to all collections and requests across all environments. This makes them ideal for storing truly global configuration or state that needs to be shared everywhere.

Methods

rq.globals.set(key, value)

Sets a global 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 global variable
  • value (any): The value to store (converted to a string). Passing null or undefined clears the variable — equivalent to unset(key), so a later get returns undefined (not the string "null").
Example:

rq.globals.get(key)

Retrieves the value of the specified global variable. Parameters:
  • key (string): The name of the global variable to retrieve
Returns: The value of the global variable, or undefined if it doesn’t exist. Example:

rq.globals.unset(key)

Removes the specified global variable. Parameters:
  • key (string): The name of the global variable to remove
Example:

rq.globals.clear()

Removes all global variables. Parameters: none. Example:

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

Implement global feature flags:

Store Common Constants

Define constants that are used across all collections:

Variable Scope Hierarchy

Understanding the hierarchy of variables in Requestly:
  1. Global Variables (rq.globals) - Highest level, available everywhere
  2. Collection Variables (rq.collectionVariables) - Available to all requests in a collection
  3. Environment Variables (rq.environment) - Available to all collections in a specific environment
When a variable with the same name exists at multiple levels, the most specific scope takes precedence:

Best Practices

  1. Use Sparingly: Reserve global variables for truly global data. Prefer collection or environment variables when possible.
  2. Naming Convention: Use clear, uppercase names for global constants
  3. Document Purpose: Comment your global variable usage
  4. Check Existence: Always verify a variable exists before using it
  5. Initialize Early: Set global variables in a dedicated setup script or request
  6. Clean Up: Remove globals that are no longer needed
  7. Type Conversion: Remember values are stored as strings