The
rq.environment object provides methods to dynamically manage environment variables during script execution. Environment variables are scoped to a specific environment and can be used across multiple requests within that environment.
Methods
rq.environment.set(key, value)
Sets an environment 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 environment variablevalue(any): The value to store (will be converted to string)
rq.environment.get(key)
Retrieves the value of the specified environment variable.
Parameters:
key(string): The name of the environment variable to retrieve
undefined if it doesn’t exist.
Example:
rq.environment.unset(key)
Removes the specified environment variable from the current environment.
Parameters:
key(string): The name of the environment variable to remove
Common Use Cases
Store Authentication Token
After receiving a login response, store the auth token for use in subsequent requests:Auto Increment Page Numbers
Automatically increment a page number for pagination:Store API Response Data
Extract and store data from API responses for use in other requests:Conditional Token Management
Check if a token exists and set it only if needed:Clean Up Sensitive Data
Remove sensitive information after use:Track Request Counts
Keep track of how many times a request has been made:Best Practices
-
Use Descriptive Names: Choose clear, descriptive names for environment variables (e.g.,
authTokeninstead oftoken) -
Check for Existence: Always check if a variable exists before using it:
-
Clean Up: Remove sensitive data when no longer needed using
unset() -
Type Handling: Remember that all environment variable values are stored as strings, so convert them when needed:

