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 variablevalue(any): The value to store (will be converted to string)
rq.globals.get(key)
Retrieves the value of the specified global variable.
Parameters:
key(string): The name of the global variable to retrieve
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
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:- Global Variables (
rq.globals) - Highest level, available everywhere - Collection Variables (
rq.collectionVariables) - Available to all requests in a collection - Environment Variables (
rq.environment) - Available to all collections in a specific environment
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: Always verify a variable exists before using it
- Initialize Early: Set global variables in a dedicated setup script or request
-
Clean Up: Remove globals that are no longer needed
-
Type Conversion: Remember values are stored as strings

