Skip to main content
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 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").
Example:

rq.environment.get(key)

Retrieves the value of the specified environment variable. Parameters:
  • key (string): The name of the environment variable to retrieve
Returns: The value of the environment variable, or 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
Example:

rq.environment.clear()

Removes all variables from the current environment. Parameters: none. Example:

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:

Working with array variables

An array variable stores a list of values. When you set an array, Requestly stores it as a real array, and get returns it as a real array rather than a comma-joined string:
You can also read the first or last items with .first() and .last():

Best Practices

  1. Use Descriptive Names: Choose clear, descriptive names for environment variables (e.g., authToken instead of token)
  2. Check for Existence: Always check if a variable exists before using it:
  3. Clean Up: Remove sensitive data when no longer needed using unset()
  4. Type Handling: Most environment variable values are stored as strings, so convert them when needed. Array-typed variables are the exception and are returned as real arrays: