Environment Variables in Postman



Environment Variables in Postman

In this tutorial, we will learn about Environment Variables in Postman.

This tutorial will cover the following topics.

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

Problem Statement

We often having different environments (ex: Development, Intigration Testing, UAT) in our team or company. Each environment having different server URL’s to invoke the API’s.

For example we have created a Collection with 100 requests, which were running successfully, but the server request URL got changed. In this scenario, to make our requests successfully, we need to change the serever request URL for each request, that will take time to change all the 100 requests. This is purely a waste of time and resources. But since this happens a lot, Postman has a feature to deal with this in few seconds and we will be good to go to use the requests again.

  • Environment and Environment Variables

A variable in the Postman is same as a variable 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.

  • Adding New Local Environment
To add new Local Environment follow the below steps..

 1. Click on “Manage Environments” icon

2. Click on “Add” button
3. Enter the Environment Name (ex: WeatherEnv)
4. Enter new variable (ex: WeatherURL) in “Add a new variable” textbox
5. Enter the Initial Value “http://restapi.demoqa.com”
6. Click on “Add” button
Recently added Environment (ex: WeatherEnv) will be displayed in “Manage Environments” screen.
7. Close the “Manage Environments” window by clicking on “close” (x) button.
8.Click on “Envionment” dropdown to verify that newly added Environment (ex:WeatherEnv) is displayed.
9.Select the Newly added Environment (Ex:WeatherEnv)
10.Click on “Environment Quick look” icon to view the Environment variables

 

  • Adding New Local Environment Variable’s

11.Click on “Edit” link next to “WeatherEnv” to add the Environment Variables

 

12.Enter new variable (ex: City) in “Add a new variable” textbox
13. Enter the Initial Value “Hyderabad”
14.Click on “Update” button
15. Click on “close” button to close the Manage Environments window
16.Click on “Environment Quick look” icon to view the Environment variables
16. Newly added Environment variable (“city”) should be displayed under “WeatherEnv”
The scope of the “city” variable is limited to “WeatherEnv”. IF we select any other Environment, “city” variable is not
available because it is not a “Global Environment variable”.
Using local environment variable’s in Request
17.use the local variable in request URL
To use the Environment variable, we should keep environment variable in double curly baces
{{WeatherURL}}
Now the request URL will be: {{WeatherURL}}/utilities/weather/city/Hyderabad
18. Mouseover on Environment variable {{WeatherURL}} in request URL.
Value of the Environment variable should be displayed.
19. Send the Request using Local Environment variable {{WeatherURL}} in API URL. we should get the same response whatever we got it from
http://restapi.demoqa.com/utilities/weather/city/Hyderabad
20. Select different Environment (No Environment) from “Environment” dropdown
21. Send the request
22 We should get the error “Could not get any response”.
We are getting this error, because the {{WeatherURL}} variable is not available in environment (No Environment) selected.
23. Mouseover on Environment variable {{WeatherURL}} in request URL.
“Unresolved variable” error should be displayed.

This tutorial is in-progress..Coming soon..

  • Scope of Variables

A scope of variable is the boundary in which that thing can be accessed and perform. Environment variables in Postman have two scopes

  • Local Scope
  • Global Scope

 

Local Scope

Local Scope Variables can only work inside the local environment (ex: WeatherEnv) 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

2. 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.

“CityName” was added as “Global” variable with value: “Hyderabad”. This variable can be used no matter whatever Environment is selected.

Global Scope Variables

  • Global Variables

1.Click on “Environment Quick look” icon to view the Environment variables

2. If there are any “Global” variables, they will be displayed under “Globals”

 

3. Click on “Edit” to add a new “Global” variable.

4. Enter the “Variable” name (Ex:CityName), Initial Value (ex: Hyderabad)

5. Click on “Save” button

 

6.Click on “Environment Quick look” icon to view the “global” environment variables

7.Recently added “Global” variable (Ex; CityName) should be displayed under “Globals”

8. Start using the “CityName” global variable in request url.

9. Mouse over on global variable  {{CityName}}

“Initial”,”Current” values should be displayed as “Hyderabad”.

“Scope” should be displayed as “Global”.

Scope of the global variable’s is “Global”, that means, global variable can be used in any request irrespective of the “environment” selected while sending the request to the server. In case of Local variable’s, to use any local variable in request, we need to select the corresponding environment where the local variable is added.

 

 

  • Precedence of variables in Postman

Precedence of thevariables means Prioriry of the variables. In postman, 2 Global variables cannot have same name, but 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 CityName and a global variable CityName. 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 / priority of variables.

If we have local, global variables with same name local variable has higher precedence. It will overwrite the global one.

Tags: ,
3 Comments

www.xmc.pl Enterprise

That is really fascinating, You are an overly professional blogger. I’ve joined your feed and sit up for seeking more of your great post. Additionally, I have shared your website in my social networks

http://www.xmc.pl

Reply

I am sure this post has touched all the internet people, its really really good article on building up new web site.| Tami Kevin Malynda

Reply

Awesome write-up. I am a normal visitor of your site and appreciate you taking the time to maintain the nice site. I will be a frequent visitor for a long time. Lurleen Vaughn Raul

Reply
Leave a comment

Your email address will not be published. Required fields are marked *

Subscribe now

Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!

https://bridgejunks.com/ https://crownmakesense.com/ https://brithaniabookjudges.com/ https://hughesroyality.com/ https://rhythmholic.com/ https://bandar89.simnasfikpunhas.com/ https://www.100calshop.co.il/products/thailand/ https://myasociados.com/ https://solyser.com/ http://konfidence.cz/ https://muscadinepdx.com/ https://bandar89.parajesandinos.com.ve/ https://goremekoop.com/ https://oncoswisscenter.com/ https://www.turunclifehotel.com/bandar89/ https://www.houseofproducts.biz/ https://taimoormphotography.com/
BIJI18 BIJI18 BIJI18