Variables in Postman
- Postman variables work in the same way as that of the programming variables. These are the symbols capable of taking various values. You can store the values in variables and can use it throughout in requests, environments, collections, and scripts.
- Variables in Postman increase the user’s efficiency to work and decrease the errors.
Environment in Postman
A collection of key-value pairs is called an environment. Each name of the variable represents its keys. And referencing the name of the variable allows you to access its value.
It is a set of variables that differentiate among the requests. Such as, we can have an environment for testing, one for development and another for production. We can change the value of the variables to pass the data between requests and tests.
Creating an Environment
To create an environment, follow the following steps:
- Go to New -> Environment.
- Or you can create an environment from Environment quick look. For that, click on Environment quick look button visible as an eye icon available in the top right corner of the builder section, as shown in the image below.
Now select Add.
Enter the name of the Environment and click on Add.
We can also download, share, delete, duplicate, or import the environment.
Sharing an environment will help you to run your requests against the same set of data.
Environment Variable in Postman
The postman variable is similar to the programming language variable. As we know, a variable is an entity, which stores value. You can change the value of the variable. In Postman, a variable is a set of key-value pairs. Here the key is the variable and value is the value of the variable.
Creating an Environment Variable in Postman
To create an environment variable follow the following steps:
- Go to Environment quick look button visible as an eye icon available in the top right corner of the builder section.
- Select the Edit option.
- Enter the below mentioned key-value pairs in which Key = url and Initial Value = http://restapi.demoqa.com
Then select the Update button and close the Manage Environment window. Now you have an environment variable with the name ‘url’, and the value of the variable is the http://restapi.demoqa.com.
Using an Environment Variable in Postman
- Select the environment from the drop-down. Now we can access the entire available variable in the environment.
- In the address bar or we can say URL text field enter the following url: {{url}}/utilities/weatherfull/city/Bengaluru
- Click on send. You will get the following response:
So, the environment is created with a variable named “url”. This means we can now use the variable instead of the actual URL.
Global Variables in Postman
Here we will see how to create global variables. Unlike the environment variable, in the case of global variables, we don’t need to create an environment first.
Creating a Global Variable in Postman
- Open the Postman app and click on Environment quick look button visible as an eye icon available in the top right corner of the builder section, as shown in the image below.
- Now select Edit option as shown in the image below available next to Globals.
- Enter the below mentioned key-value pairs in which Key = url and Initial Value = http://restapi.demoqa.com
- Select the save button and close the Manage Environments Here your global variable is created with the name ‘url’.
Using a Global Variable in Postman
Select the No Environment from the drop-down of Postman, from the top right corner of Postman.
- In the address bar enter the following url: {{url}}/utilities/weatherfull/city/Bengaluru
- Click on Send and see the response.
Variable Scopes
Variable scope is the boundary within which these variables can be accessed and executed. The following variable scopes are supported by Postman, which is used for different tasks. The narrowest variable scope is Local, and then next is Data, Environment, Collection, and the broadest one is Global.
In case, the variable name is the same, in two scopes with a different value, then the value of the narrowest variable scope will be used while running the request. Let’s say the name of both local and global variables is My Variable. Then Postman will use a local variable value to run the request, as a local variable is the narrowest variable scope.
Let’s see these variable scopes one by one:
- Local Variables
These are temporary variables that will only function inside the environment they were created in. When you change the environment, the variable will stop its access, and an error will occur. - Collection Variables
These are independent of environments and are available for all the requests in a collection. - Environment Variables
These are the most used variable scope in Postman. At a time, a single environment can be active. They are bound to a specified environment used to execute the request. - Global Variables
These are independent of environments and function outside the environment. Users are not required to create an environment for global variables. Through global variables, we can access data between collections, test scripts, requests, and environments. - Data Variables
Data variables come from external CSV and JSON files to define data sets that you can use while collections are running through Newman or the Collection Runner.