Variables in Postman

Bisma Latif
7 min readDec 30, 2022
Postman Variables cover image

In the second article for the series, API testing with Postman we will look into Variables in Postman. In this article we will learn:

1. What are variables?

2. Why do we need variables in Postman?

3. Creating a Variable in Postman

4. Types of Variables in Postman.

5. Preference for Types of Variables

In Postman, variables are values that can be stored and reused throughout a collection or environment. They allow you to parameterize your requests and responses, making it easier to test and debug your API requests and responses. Variables are key-value pairs and we can give any name to the variable and value. The key-value pair can store different values so tomorrow if you want to change this value we can change this value and store a different value the variable and hence, use them in requests as required.

why exactly do we need variables?

Take an example let us say this URL www.example.com is being used in multiple files or multiple API requests in postman.

So in all these requests, the same URL is being used now because this is common to all the requests. Whenever there is any change in this particular URL I will have to go into every request and make the changes.

This can be very time-consuming error prone and not a very efficient way.

So what I can do is create a variable, give it a name say URL and give the value which is this particular URL (www.example.com). Now I can use this variable i.e. URL in all of the API requests.

Advantages of Variables in Postman:

Some other advantages of using variables in Postman include:

1. You can use variables to store sensitive information, such as API keys or access tokens, in a secure manner.

2. You can use variables to store data extracted from a response, which can be useful when testing APIs that return large amounts of data.

3. You can use variables to create dynamic requests and responses, which can be helpful when testing APIs that require different input values depending on the context.

Creating a Variable in Postman:

To create a variable in postman:

  1. Navigate to Collection and click on the Variable tab.

2. Give a name to your variable. Currently, we are creating the variable for URL, so we are going to add Url as the name.

3. Add your Url in the Current Value column.

Now there are two columns initial value and Current value. Why two columns and what does it do?

We have the initial value and current value. So current value is what will be used while I run the request and this will not be shared with the team and the initial value is what will be shared with the team.

So sometimes when you are working on your local system and you do not want to share the values with your team and with everyone who is linked to this particular collection you can keep your values in the current value column.

To share your overall collection as well as the initial value, you click on persist all, the initial value column will update itself to the current value.

Note: The steps mentioned above are for collection variables, whose scope is limited to a collection.

How to Use the Variable you just created?

You can use variables in your request and response bodies, headers, and URL parameters. To use a variable in a request or response, you can use double curly braces ({{}}) to enclose the variable name. For example, to use a variable named Url, you would use {{Url}} in your request or response.

It is important to note that you can only access your variable once you have saved your collection, otherwise, you will get an error for “Unresolved Reference”:

Types of Variables in Postman:

There are several types of variables that you can use in Postman:

Global variables: These are variables that are available in all collections and environments. They can be used to store values that are used across multiple requests, such as API keys or access tokens.

Collection variables: These are variables that are specific to a collection and can be used to store values that are used in multiple requests within the same collection.

Environment variables: These are variables that are specific to an environment and can be used to store values that are used in multiple requests within the same environment.

Data variables: These are variables that are used to store data that is extracted from a response using the pm.response.json() function. They are often used in conjunction with the pm.iterationData.set() function to store data from multiple responses in a loop.

Local variables: These are variables that are specific to a single request and can be used to store values that are used only in that request.

Create a Global Variable:

To create a global variable in Postman, follow these steps:

  1. Click the Globals button in the top right corner of the Postman window.

2. Click the Add button in the Globals tab.

3. In the Name field, enter the name of your global variable.

4. In the Value field, enter the value of your global variable.

5. Click the Save button.

Create a Local Variable:

To create a local variable in Postman, follow these steps:

1. Open the request or response in which you want to use the local variable.

2. In the request or response body, headers, or URL parameters, use double curly braces ({{}}) to enclose the name of your local variable. For example, to create a local variable named, you would use {{myVariable}} in your request or response.

3. Click the Pre-request Script tab in the request editor.

4. In the script editor, use the pm. variables. set() function to set the value of your local variable. For example:

pm.variables.set("myVariable", "Hello, World!");

Create an Environment Variable:

To create an environment variable in Postman, follow these steps:

1. Click the Manage Environments button in the top right corner of the Postman window.

2. Click the Add button in the Environments tab.

3. In the Name field, enter the name of your environment variable.

4. In the Value field, enter the value of your environment variable.

5. Click the Add button to add the environment variable to the list of environment variables.

6. Click the Update button to save the changes to your environment.

Create Data Variables:

To create a data variable in Postman, you can use the pm.iterationData.set() function to store data extracted from a response in a loop.

For example, suppose you have an API that returns a list of users, and you want to store the names of all the users in a data variable. Here’s how you could do it:

Send a request to the API and extract the list of users from the response using the pm.response.json() function. Iterate over the list of users using a for loop, and use the pm.iterationData.set() function to store the name of each user in a data variable.

Here’s what the script might look like:

var users = pm.response.json();

for (var i = 0; i < users.length; i++) {
var user = users[i];
pm.iterationData.set("name", user.name);
}

The pm.iterationData.the set() function will store the value of the name variable for each iteration of the loop, so you can use it in subsequent requests or tests. To use the data variable in a request or test, you can enclose the variable name in double curly braces ({{}}). For example, to use the name data variable, you would use {{name}} in your request or test.

Variable Priority/Preference:

At times, we have the same name variables at different levels. For instance, I have two variables named Url in both global and collection variables. In that case, based on the order of preference will be given to the collection level variable. The priority of variables in postman is as under:

1. Local.

2. Data.

3. Environment.

4. Collection.

5. Global.

After the end of this article, you will be able to create variables in Postman. If you have any questions, drop me a comment.

Till then Happy Testing!

--

--

Bisma Latif

A writer, coder, and an avid reader, who puts her soul in everything she does!