Variables in Postman

Hirudini Udugama
3 min readJan 21, 2022

Variables are used to store values. To reuse values in multiple places, avoid repetitions, avoid re-work when changes occurred for values are the reasons to use variables.

How to create variables?

In the previous article we created two requests as Get User and Get all Users. There we can see that “https://reqres.in” as a common factor.

Basically we can create variables in

  1. Collection level

2. Global level

3. Environment level

How to create a collection level variable?

Click on more option on the collection -> Edit

Under this variable tab we can add collection variables as key value pairs.

Now let’s create this https://reqres.in as a variable with the variable name “url” and click on the save button

How to refer to variables?

Let’s refer to this new variable inside the collection on the requests of Get User and Get all Users. For that we can use the variable name inside double curly braces instead of the https://reqres.in

And then click on the save button. By clicking on the send button we can get the response of this request. Same as we can refer to the variable in Get all Users request.

How to create an environment level variable?

By clicking on the eye icon in postman window this window will appears on the available environments. By clicking on edit icon in the selected environment we can add environment variables as key — value pairs.

How to create a global level variable?

Same as the environment variables, by clicking on the add icon in the global option we can create global environments.

How to get variables through scripts?

We can write scripts in the test tab. To log anything in the console we can use console.log()

Here we can see the request logs and printed statement of “Hello world” in the console.

To get a variable in the collection level we can use pm.variables.get(“Variable Name”). Also we can store this value in a variable

let urlVar = pm.variables.get(“url”)

How to set variables through scripts?

We can set values by pm.variables.set()

Same as we can get the global variables.

Same as the above we can set and get variables for environment level.

Summary of set and get variables:

pm.variables.get();

pm.variables.set();

pm.globals.get();

pm.globals.set();

pm.environment.get();

pm.environment.set();

Also we can add these variables at the collection level. For that click on collection -> Edit -> Pre request scripts

--

--