Variables in Requestly’s API Client enable you to manage dynamic values like API keys, base URLs, and other configurable parameters efficiently. Requestly supports three types of variables: Global, Environment, and Collection-level. This document elaborates on all types of variables and provides guidance on creating, using, and testing variables to streamline your API workflows.
What is a variable?
Requestly allows you to create and store reusable values as variables. By defining variables once, you can reference them anywhere in your API interactions, simplifying workflows and making collaboration with teammates easier.
In Requestly, any object enclosed within double curly braces {{variable}}
is treated as a variable. These placeholders are dynamically resolved based on the variable scopes.
These variables fall into four distinct types, each with specific scopes and purposes:
Global Variables
Global variables are accessible across all requests and collections in Requestly. They are ideal for values that need to remain consistent across multiple environments or API requests.
Collection Variables
Collection variables are confined to a specific set of API requests within a collection. These are particularly useful for sharing data across related API requests in a single group.
SubCollection Variables
SubCollection variables are confined to a specific group of requests within a collection. These are ideal for managing data across related requests within a smaller subset.
Environment Variables
Environment variables are scoped to specific environments, such as Development, Staging, or Production. They help configure requests dynamically based on the active environment.
Variable Precedence
Variable precedence in Requestly ensures that when multiple variables with the same name exist, the most relevant one is chosen based on scope. The precedence order is as follows:
Environment Variables → Collection Variables → SubCollection Variables → Global Variables
In other words:
-
Environment variables always take the highest priority and override any variable with the same name in other scopes.
-
If no matching environment variable exists, Requestly checks for the variable in the collection scope.
-
Global variables serve as a fallback when the variable is not found in the environment or collection scopes.
This approach simplifies managing configurations across different use cases, ensuring that environment-specific values are prioritized without interfering with globally defined values.
Or in developer's languages:
if (variable_name in environment_variables) { //Check in environment
return environment_variables[variable_name];
} else if (variable_name in collection_variables) { //Check in collection
return collection_variables[variable_name];
} else if (variable_name in global_variables) { //Check in global
return global_variables[variable_name];
} else {
return null; // Variable not found in any scope
}
How to Create Global Variables
Step 1: Access Global Variables
Click on the Environments icon in the side menu. The first item in the list is labeled as "Global variables."
Step 2: Add Variables
Add variables in the table by specifying the following details:
-
Key: The name of the variable that you will be referencing when sending requests. Note, global variables cannot have duplicate keys.
-
Type: The type of value the variable will store. It can be a string, number, or boolean.
-
Initial value (synced): Initial values will be synced across the workspace. These values will be used by default if no user-defined Current value is set for the variable.
-
Current value (local): Current values are user-defined entries that are not synced across the workspace. These values will override the defined Initial values. If the current value is empty or left blank, then the initial value of the variable will be used.
How to Create Environment Variables
Step 1: Access the Environments Tab
Click on the Environments tab in the side menu.
Step 2: Select an Environment
Select the environment you want to add variables to.
Step 3: Add Variables
Add variables in the table by specifying the following details:
-
Key: The name of the variable that you will be referencing when sending requests. Note, an environment cannot have keys with the same name.
-
Type: The type of value the variable will store. It can be a string, number, or boolean.
-
Initial value (synced): Initial values will be synced across the workspace. These values will be used by default if no user-defined Current value is set for the variable.
-
Current value (local): Current values are user-defined entries that are not synced across the workspace. These values will override the defined Initial values. If the current value is empty or left blank, then the initial value of the variable will be used.
Step 4: Switch Environments
To switch environments, use the dropdown on the top bar of the API Client’s sidebar. Select the desired environment to ensure that the variables associated with it are used in your API requests.
How to Create Collection Variables
Step 1: Access the Collections Tab
Sub-collection variables can be created and take precedence over their parent collection variables. If a variable in the sub-collection shares the same key as one in the parent collection, the sub-collection variable will be used during request execution.
Click on the Collection Name to open Collection Overview and switch to Variables tab.
Step 2: Add Variables
Add variables in the table by specifying the following details:
-
Key: The name of the variable that you will be referencing when sending requests. Note, an environment cannot have keys with the same name.
-
Type: The type of value the variable will store. It can be a string, number, or boolean.
-
Initial value (synced): Initial values will be synced across the workspace. These values will be used by default if no user-defined Current value is set for the variable.
-
Current value (local): Current values are user-defined entries that are not synced across the workspace. These values will override the defined Initial values. If the current value is empty or left blank, then the initial value of the variable will be used.
Use Variables in API Requests
Step 1: Create or Open a Request
Create or open an existing request in the API Client.
Step 2: Replace Static Values
Replace static values with your environment variables using the {{variable_name}}
syntax.
Step 3: View Variable Values
Hover over a variable to see its resolved value.
Test Your Setup
Step 1: Execute the Request
Click Send to execute the request.
Step 2: Verify Resolved Values
Check the resolved values in the Request Preview to ensure that variables are substituted correctly.
If a variable isn't resolving, verify that the variable name matches exactly (case-sensitive) and is present in the selected environment.