Introduction

In this step-by-step article series next, we are going to learn about the Environments features of the postman. Before going to learn about the environments, please read the previous article of this series for a better understanding:

This article covers the following topics,

Environments

The environments are nothing but the set of variables defined, variables are stored in the form of key-value pairs and to use in the postman request. You can create the environments like Development, Staging, Production, etc., and define the variables.

Syntax - To access the environment variables.

{{variable_name}}

There is a scope defined for each environment variable as below,

Create the Environments and Variables

Let's create the environments and variables in it, as in the below steps:

Step 1

Open the Postman web version or the desktop app and select 'Environments' from the left side as below screenshot:

In the above screenshot, the right side portion is empty because there are no environments currently.

Step 2

Next, create an environment. For that you need to click on the plus icon or the bottom 'Create Environment' link as below:

Once you click, it opens the screen for creating an environment on the right side portion as below:

Postman

Step 3

Next, provide the values as shown below and click on the Save button at the end:

Postman

In the above screenshot, when you click on the top three dots it shows the option such as Set as active environment, Export, Duplicate, Delete, etc. as shown in the above screenshot.

Step 4

Next, once you created the environments that list you can see them in two places first on the left side Environments tab and on the right side top dropdown, and set the default environment as 'Dev' as below:

Postman

Access the environment variables

Let's see how can we access those environment variables in the request as below steps:

Step 1

Click on the plus icon to create a request as below:

Step 2

It will open the create request page on the right side and provide the details as below:

Postman

In the above screenshot, you can see the URL we have used for GET request as '{{base_url}}/users/jsgund'. Here '{{base_url}}' means the environment variable and when we click on send, it will replace the variable with the actual value as 'https://api.github.com'. On the right-side top dropdown, you can see the selected environment for this request as 'Dev'.

Step 3

Next, if you want to check the environment variables for the selected 'Dev', click on the right-side eye icon to view the variables as above:

Postman

Or just mouse over on the '{{base_url}}' environment variable you can get the details about that variable as below:

Step 4

Next, just click on the send button, and let's see the result we get as below:

Postman

The environment variables are easy to use and update in one place and it will use the updated value for that environment.

Set and Get environment variable from script

There is another way also to set or create the environment variable in the 'Pre-req' and 'Test' script as below:

The syntax for Set:

pm.environment.set("base_url", "https://api.github.com");

The syntax for Get:

pm.environment.get("variable_key");

In the above code you can see the 'pm' object, using this you can access and manipulate the variables in Postman using the 'pm' API.

Stay tuned to learn new things about Postman….

Conclusion

In this article, we learned about Environment and Variables, the scope of variables, and creating and using in the request. If you have any suggestions or queries regarding this article, please contact me.

“Learn It, Share it.”

Read Next - Learn Postman Step By Step - Preset Headers