Last Updated On
In this tutorial, we will discuss the use of Environment in Postman.
Table of Contents
- What is an environment?
- Why do we need an environment?
- How do we create an environment?
- Steps to use the Environment
What is an environment?
The environment is a set of key-value pairs. We can use environments to group related sets of values together and manage access to shared Postman data if you are working as part of a team.
Why do we need an environment?
Imagine we have 4 different environments – Dev, QA, UAT, and Pre Prod. These environments need different credentials to login to the application. So, we will create same request with different credentials or will keep on changing the credential of same request every time. This is a messy approach. So, the ideal scenario is we create environment variables for the changing attributes and select the environment for particular attributes. In this way, we have a single request with different combination of attributes.
How do we create an environment?
Select Environments on the left and select +.
Enter a name for your environment, and initialize it with any variables you need. You can also specify variables for the environment later.
Steps to use the Environment
Step 1 – Create an API Request
Create an API request in the collection.
I have created an API named as RegisterUser.
URL – https://reqres.in/api/register
Request Method – POST
Sample body
{
"email": "eve.holt@reqres.in",
"password": "pistol"
}
Save the request. Click on the “Send” button to verify the API returns a successful response as shown in the below image.
Step 2 – Create an environment and add key-value pairs (variables)
Create a new environment as explained in the above tutorial.
Add the values that are dynamic for different environments. Click the “Save” button to save the variables.
In the Key field, enter the name of the environment variable that will be used in the Postman Collection. In the Value field, enter the value that will replace the variable when the call is made. For example:
Enter a name for your variable, and specify its Initial and Current values. By default, the current value will copy the initial value.
- The Initial Value is synced to your account using the Postman servers. It’s shared with any collaborators who have access to the environment.
- The Current Value is used in your local instance of Postman and is never synced to your account or shared with your team unless you choose to persist it.
Step 3 – Select the environment from dropdown
Go to the new request and select the environment created just now. In this case, I have selected “QA” environment. Refer to the newly created variables in the request.
Step 4 – Refer the newly created variables in the request
Here, we can see that it is showing all 3 variables that we have defined in the previous step. To use an environment variable value in a request, reference it by name, surrounded with double curly braces:
Hover over a variable reference to get its current value.
Step 5 – Run the request
Click on the “Send” button. The request will be sent, and the response displayed as in shown the below image.
Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!