Environment Variable Types
Variables are used to store information to be referenced in different places. Different API (request parameters, scripts, etc.) can refer to the same variable, and updating the value of the variable updates all the corresponding. Using variables can improve productivity.
Quick Start
- Open Environment Management (the burger in the upper right corner) and select the Global Variables tab.
- Add a variable named my_variable, set the current value to hello, and click Save.
- Go to an API, and enter {{my_variable}} in the parameter value in the run tab.
- Click the run button, and send the request. The system will replace {{my_variable}} with hello during runtime and send out the request.
- You can access the base URL through script by pm.request.getBaseUrl().
The Difference between Local and Remote Values
- Local variables have priority over remote Values.
- Local variables are appropriate for sensitive data such as tokens, accounts, passwords, etc. since they are only saved locally.
- Remote values are synced to the cloud, mainly used to share data values between team members.
Since the current values are only stored locally, using cleaning software to clean the Apidog file cache will cause the current values to be deleted. So please be cautious.
Variables Type
- Environment Variables: Environment variables are the variables that change when the environment changes. Environment variables are managed in the environment management sections. View documentation of environment management here.
- Global Variables: Global variables are similar to environmental variables. The only difference is that global variables do not change when the environment changes.
- Local Variables:Only valid during a single API run, a test case debug, or a test collection debug. They will be discarded afterward.
Using Variables
- All variables can be accessed through double curly brackets, for example, {{token}}.
- Variables can be used in multiple places, including API running tab, API use case, and quick request.
- You can use variables for the additional variables in the environment as well.
You can use variables and dynamic variables for the request body in json or raw format. See usage below
{
"field1": "{{stringVariable}}",
"field2": {{intVariable}},
"field3": {{arrayVariable}},
"field4": {{objectVariable}},
"field5": {{$timestamp}}
}
TIP
- You need to add double quotes when using variables for string type in json format. Do not add double quotes for all other types, as shown in the example above.
- If you use a variable without double quotes, please do not use the formatting feature. Ignore any prompts for incorrect JSON formatting if there are any.
If the value of the variable is in the form of an object or an array, the value of the attribute in the variable can be read by {{variableName.attributeName}}
or {{variableName[0].attributeName}}
. For example:
- The object variable user is as follows:
{
"id": 1,
"name": "jack"
}
- You can reference the name property of the user object as
{{user.name}}
in the API parameter. - You can reference the name property in the user object as
pm.variables.get("user.name")
in customized scripts.
- The array variable user is as follows:
[
{
id: 1,
name: "jack",
},
];
- You can reference the name property of the first element in the user array as
{{user[0].name}}
in the API parameter. - You can reference the name property in the first element of the user array as
pm.variables.get("user[0].name")
in customized scripts.
Reading the value of a property in a variable (object or array) as {{user.name}}
follows the JSON path syntax specification. You can replace the \$
symbol in the JSON path with the variable name.
View details of JSON path here.
Variable Precedence
Double curly brackets can reference all variables. When different types of variables with the same name exist, the system decides which to use based on precedence.
Variable Precedence Order: Local Variables > Debugging Variables > Environment Variables > Global Variables
Using Variables in Script
Environment Variables
//Set up Environment Variables
pm.environment.set("variable_key", "variable_value");
//Access Environment Variables
var variable_key = pm.environment.get("variable_key");
//Unset Environment Variables
pm.environment.unset("variable_key");
Write object or array (non-string type) into environment variable
Environment variables need to be in string type. If you want to write an object or array type, you need to use JSON.stringfy
to convert it into a string.
var array = [1, 2, 3, 4];
pm.environment.set("array", JSON.stringify(array));
var obj = { a: [1, 2, 3, 4], b: { c: "val" } };
pm.environment.set("obj", JSON.stringify(obj));
When reading the value, you need to use JSON.parse to convert it back.
try {
var array = JSON.parse(pm.environment.get("array"));
var obj = JSON.parse(pm.environment.get("obj"));
} catch (e) {
// Exception handling
}
Global Variables
// Set up global variables
pm.globals.set("variable_key", "variable_value");
// Access Global Variables
var variable_key = pm.globals.get("variable_key");
// unset Global Variables
pm.globals.unset("variable_key");
Local Variables
// Set up Local Variables
pm.variables.set("variable_key", "variable_value");
// Access Local Variables
var variable_key = pm.variables.get("variable_key");
// unset Local Variables
pm.variables.unset("variable_key");