- Dec 13, 2019
- admin
- 3
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
1. Click on “Manage Environments” icon
- Adding New Local Environment Variable’s
11.Click on “Edit” link next to “WeatherEnv” to add the Environment Variables
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.
2. Click Send.
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 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.
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
supergirl
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
altyazili
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