Enroll in Selenium Training

In the last tutorial we learnt about the testing process in Postman. We also learnt about the collection runner and how we can use it to test many request at the same time. Now with this tutorial we will move ahead to Environment Variables in Postman without which sending requests becomes a very hectic and time consuming tasks.

In this tutorial, we will follow the following topics

  • Environment and Environment Variables
  • Local Environment Variables
  • Scope of Variables
  • Global Variables
  • Precedence of variables in Postman

Problem Statement

We often encounter different servers in our company or team. These can be either development server, a production server or maybe a testing server. Every server has different types of request APIs.  Since we know that a collection can include many requests within it, what if the URL changes? For example, they change their server request URL. If the team managing the server changes the request API and inform us, we have a lot of trouble in front of us. For running the requests successfully now we have to make the changes to each and every request. For 300 requests, we will have to change 300 times. This is purely a waste of time and resources. But since this happens a lot, Postman has a feature to deal with this in a few seconds and we will be good to go to use the requests again.

What is an Environment in Postman?

An environment in Postman is a set of key-value pairs. An environment helps us to differentiate between the requests. When we create an environment inside Postman, we can change the value of the key value pairs and the changes are reflected in our requests. An environment just provides boundaries to variables. When we create different environment we can make track of all the variables and how to use them inside our requests. There can be many variables inside one environment. At once, we can work only in one environment although we can create any number of environments in Postman. The below screenshot shows three environments that we created.

Environment Variables in Postman

What is an Environment Variable in Postman?

A variable in the Postman is same as in any programming language. A variable is an entity whose value can be changed. The key part in the key-value set in environment is called variable. This variable can have any value and in place of the key we can use the variable name in every request. This will be clear with an example shown below and steps shown thereafter.

Variable_Example

The above image shows three variables inside environment Test Environment 1

Environment Variables in Postman

Now, we will use Postman to create an Environment and Environment Variable and it is very easy to do that but it has three steps involved in the process:

  1. Create an Environment
  2. Create Environment Variables
  3. Use an Environment Variable in the Request

Step 1: How to Create an Environment in Postman

1.Create a new Collection and name it as EnvironmentChapter.

  1. Add Weather Api Request in the collection used in the Get Request chapter.

Environment_Chapter_Collection

  1. Click on the gear icon which says Manage Environment.

Gear_Icon_Manage_Environment

  1. Click on Add.

Add_Environment

  1. Name the environment as Weather API

Weather_API_Envrionment

Step 2: How to Create Environment Variables in Postman

1.Now in the same window, enter the following key-value pair. Where Key is the name of the variable and Value is the text string.

Key : URL

Value : http://restapi.demoqa.com

Weather_API_Envrionment

Click on Add and close the panel.

Step 3: How to Use Environment Variables in Postman

  1. Select the dropdown which says No Environment and select Weather API environment in that.

Dropdown_For_Environment

Now we can access all the variables of this environment.

  1. In the address bar change  http://restapi.demoqa.com to {{url}}

Change_to_URL_Variable

  1. Click on Send.

Now, we have created an environment and used a variable called URL here. This variable can be now used instead of the actual URL. You can see the response which is same as before we were using the full URL.

So, if by any chance the URL changes, we can just go to environment and change the URL value and it will be reflected in every request.

NOTE: Remember to save the request by clicking Save button. In the future chapters, we will use this modified request only.

Scope of Variables in Postman

A scope of anything is the boundary in which that thing can be accessed and perform. For example, if you are an engineer and do not have a passport, your scope is limited to India since you cannot go outside. While having a passport changes your scope to the world. Similarly, variables in Postman have two scopes

  • Local Scope
  • Global Scope

Local Scope

Local Scope Variables can only work inside the environment in which it was created. Changing the environment will stop the access to that variable and we will encounter an error.

The variable URL that we just created above is the local variable because it has a scope only till the environment Weather API. In the following steps we will explore the limitations of local variable by accessing local variable in other environment, where it is not present.

1.Go to the dropdown where we selected Weather API and select any other value (if you have) or No Environment.

Dropdown_For_No_Environment

  1. Click Send.

Wrong_Environment_Error

This error occured because Postman does not know about URL variable because we have changed the environment. Therefore, URL is a local variable having scope only till the Weather API environment.

Global Scope

Global Scope Variables can work outside the environment also. They are global and it does not matter which environment is selected. In the following image you can see three global variables by clicking the Eye icon.

Global_Variable_Example

Global Variables in Postman

Now, we will use Postman to create Global Variables.

Create an Environment: Just because global variables are not associated with any particular environment, there is no need to create an environment for global variables.

  1. Create Global Variables
  2. Use Global Variable in the Request

Step 1: How to create a Global Variable in Postman

1.Go to the same gear icon to open the environment panel which we did at the time of creating Local Variable.

  1. Select Globals to add a global variable.

Adding_Global_Variable

  1. Add the following key-value pair

Key : URL

Value : http://restapi.demoqa.com

Global_Variable

  1. Save and close the panel.

Step 2: How to use Global Variable in Postman Request

1.The request which we created above, just select the No environment from the environment dropdown.

No_Environment_Selected

  1. Press Send and now see the result.

Global_Variable_Response

It works now because we have created a global variable which can be used with every environment.

NOTE: Global scope cannot have duplicate/same names while variables having local scope can have the same name in different environments.

For convenience Postman also has a feature which lets you see all the current variables and environment. Just click on Eye icon and it list down all the Environments and Global variables.

Eye_Icon

and you can see the global variable under Globals written. We have not selected any environment therefore there is no information about the environment. You can try it out yourself.

Precedence in Variables

As we discussed, two global variables cannot have same name while two local variables can have same name provided they are in different environments. But what if one local variable and one global variable has same name? For example, you name a local variable ABC and a global variable ABC. Now when you select that respective environment both the variables will be activated. So, which will show its value? This confusion is solved by precedence.

Precedence generally means priority. While two or more things strike together, the one with higher priority (precedence) is preferred. In Postman for same name of environment specific variable and global variable, environment specific variable or local variable has higher precedence. It will overwrite the global one.

1.Now in the dropdown panel select Weather API instead of No Environment

Weather_API_Envrionment_Selected

Now we have two variables of same name accessible. One in Weather API environment and one which is global.

  1. Click on Eye icon to have a look

Eye_Icon_With_Same_Variables

Here we have a problem, both the variables have same values. But if you look in the image above, global URL has been sliced off with a line. This has happened because both the variables have the same name and the precedence will be given to the local variable so global variable will not be used.

3.Go to the Manage Environment (gear icon) and click on Weather API environment

Change_The_Weather_API

  1. Change the url value to anything you like. Here we have changed it to anonymous.

Edit_Environment

  1. Close the panel and look at the current environments again by selecting the Eye icon

Eye_Icon_All_Variables

Both the variables are now accessible and can be used. If you press send now, you will get the correct response from global variable which you would get from local variable if they had same name. This is how precedence works.

This is all from the Environment and variable chapter. Now you can use Postman efficiently and manage request like a professional. We will move on to the next chapter now.

Basic Authentication in Postman
Basic Authentication in Postman
Previous Article
Collections In Postman
Collections In Postman
Next Article
Harish Rajora
I am a computer science engineer. I love to keep growing as the technological world grows. I feel there is no powerful tool than a computer to change the world in any way. Apart from my field of study, I like reading books a lot and developing new stuff.
Reviewers
Lakshay Sharma's Photo
Lakshay Sharma

Similar Articles

Feedback