Table of contents
1.
Introduction
2.
Environments in Postman
2.1.
What is the need for the environment?
3.
How to create Environments in Postman?
4.
How to add Environment Variables?
5.
How to use variables?
6.
Initial vs Current Value
7.
Scope of Variables in Postman
7.1.
🛑 Local Scope
7.2.
🛑 Global Scope
8.
Precedence of Variables
9.
Frequently Asked Questions
9.1.
For what purpose is Postman used?
9.2.
Is Postman a REST API?
9.3.
Why is Postman best for API testing?
10.
Conclusion
Last Updated: Mar 27, 2024

Managing Environments in Postman

Author Neha Chauhan
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

API (Application Programming Interface), is a software intermediary that allows two applications to talk to each other. When you use an application on your mobile phone, the application connects to the Internet and sends data to a server. 

The server then retrieves that data, interprets it, performs the necessary actions and sends it back to your phone. The application then interprets that data and presents you with the information you wanted in a readable way. All of this is done by an API.

Postman is an API development platform for designing, building and testing API (Application Programming Interface). It can create different HTTP requests (GET, POST, PUT), save environments for later use, and convert the API to code for various languages (like JavaScript, Python).

 

Managing Environment img

An environment is a set of variables in the form of key-value pairs. These variables help in differentiating the requests in Postman. As the value of these variables changes, the requests also change. The general definition of the Environment is “the conditions in which one lives or works” similarly, in Postman, an Environment is a group of variables that define how an API will work. It distinguishes between the various APIs. 

In this article, we will discuss what are Environments in Postman and how to create them. We will also discuss how to manage these Environments in Postman. 

 

 Get started

Environments in Postman

An environment is a set of variables that can be used in Postman requests. It is a set of key-value pairs. An environment helps in differentiating between the requests. To change the request, change these key-value variables. There can be many environments however, we can work only in one Environment at a time. 
 

What environment img

What is the need for the environment?

There are different types of servers like development servers, production servers and testing servers. Different servers have different requirements. Some of the development functionalities need not be required in the production or testing. Environments facilitate customizing the servers. 

There are various request APIs available for each server. If the server management team modifies the request API without any notification, there will be a lot of problems. Each and every request will have to be changed for the requests to run successfully. Simply put, this is a waste of time and money.

Postman, however, provides a feature to address this quickly so we may resume using the requests. This feature is called Environment.

Now that we have an understanding of what environment variables are and why we need them, let’s discuss how to create them. 

How to create Environments in Postman?

Let's learn img

 

In this article, we will work with an open-source API, ‘https://simple-books-api.glitch.me’. This API allows you to reserve a book.

🚀 Open postman workspace. On the right-hand side, you can see No Environments (if you have not selected any environments). 

No Environments tab shown

 

🚀 On the right-hand side of No Environments’, you can see ( environment quick look). Click on it. Go to ‘add’.

Adding an environment

 

🚀 Let’s name the environment ‘Ninja_Enviornment ’.

Naming the environment
 

Congratulations🎉 on creating your first Postman Environment. 

A new environment created

How to add Environment Variables?

Ninja Coder img

🚀 Using select the environment in which you want to add the variables. Then go to Edit’. 

Editing environment

 

🚀 Let’s make a key-value pair whose key is ‘url’ and value is ‘books’. In ‘variable’ enter the key and in ‘initial value’ enter the value of value

Adding values

 

We will discuss about ‘inital value’ and ‘current value’ later in the article.

🚀 Click on the ‘Save’ button. 

Saving the environment

Congratulations🎉 you have successfully added a variable to the environment.

How to use variables?

Now that we have created an environment and its variables. Let’s look at how to use them.

As mentioned before, we will be working with an open-source API, ‘https://simple-books-api.glitch.me’. This API allows you to reserve a book.

Request GET /books returns a list of books.

Let’s first send a request which returns a list of books by using, https://simple-books-api.glitch.me/books. 

The output looks like this:

[
    {
        "id": 1,
        "name": "The Russian",
        "type": "fiction",
        "available": true
    },
    {
        "id": 2,
        "name": "Just as I Am",
        "type": "non-fiction",
        "available": false
    },
    {
        "id": 3,
        "name": "The Vanishing Half",
        "type": "fiction",
        "available": true
    },
    {
        "id": 4,
        "name": "The Midnight Library",
        "type": "fiction",
        "available": true
    },
    {
        "id": 5,
        "name": "Untamed",
        "type": "non-fiction",
        "available": true
    },
    {
        "id": 6,
        "name": "Viscount Who Loved Me",
        "type": "fiction",
        "available": true
    }
]

 

Now, let’s learn how to use environment variables.

To use a variable, enclose it in {{..}}. 

Now, let’s send a request by adding {{url}} variable to our API.

Sending request

The output is:

[
    {
        "id": 1,
        "name": "The Russian",
        "type": "fiction",
        "available": true
    },
    {
        "id": 2,
        "name": "Just as I Am",
        "type": "non-fiction",
        "available": false
    },
    {
        "id": 3,
        "name": "The Vanishing Half",
        "type": "fiction",
        "available": true
    },
    {
        "id": 4,
        "name": "The Midnight Library",
        "type": "fiction",
        "available": true
    },
    {
        "id": 5,
        "name": "Untamed",
        "type": "non-fiction",
        "available": true
    },
    {
        "id": 6,
        "name": "Viscount Who Loved Me",
        "type": "fiction",
        "available": true
    }
]

 

You can notice that the output of https://simple-books-api.glitch.me/books and https://simple-books-api.glitch.me/{{url}} is the same. 

Initial vs Current Value

Postman allows you to share the environment with your team.

By default, the initial value is the same as the current 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.

Scope of Variables in Postman

Just like in programming languages, such as C++, Java, Python, and so on variables in Postman also have a scope. Variables are not accessible outside their respective scopes.

          ⭐ Local Scope

          ⭐ Global Scope

🛑 Local Scope

Local Scope Variables are only functional within the context of their original Environment. If the Environment is changed, we won’t be able to access that variable and will get an error.

The variable {{url}} that we have created is a local variable. We can make all the other local variables as we created the variable {{url}}

🛑 Global Scope

Global Scope Variables are also applicable outside of the Environment. They are global, so it does not matter which Environment is selected. 

Let’s see how we can create global variables.

🚀 Click on

🚀 You can see Globals. Click on Add’.

Adding globals

 

🚀 Now you can add the key-value pairs just like we did for the local variables. 

Precedence of Variables

☘️ Within one environment, there cannot be two variables of same name. 

☘️ Two local variables can have same name provided they are in different environments.

☘️ Two global variables cannot have same name. 

What will happen when there is a local variable and a global variable with the same name?

In Postman for the same name of local variable and global variable, local variable has higher precedence.

Frequently Asked Questions

For what purpose is Postman used?

Postman is an API client that makes it easy for developers to create, share, test and document APIs. Users are allowed to create and save simple and complex HTTP/s requests, as well as read their responses.

Is Postman a REST API?

Initially, we could only create REST API using postman. Now with upgradation, we can create REST, SOAP and GraphQL queries using Postman. 

Why is Postman best for API testing?

Postman is an application that allows us to test APIs utilizing a graphical user interface. Postman includes the collection feature and the possibility of creating different testing environments, which makes it Best for API testing.

Conclusion

Pat yourself on the back👏, for finishing this article 😎. In this article, we discussed what are Environments in Postman, how to create them and how to use them. We discussed the local and global scope of the variables. Along with all these we discussed the precedence of the variables. 

After reading this article, if you are interested in knowing more about Postman, we recommend you to read some of our articles 

🔥 Web Testing

🔥 API

🔥 API Testing

You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations, and much more!!

We wish you Good Luck!🎈 Please upvote our blog 🏆 and help other ninjas grow.

Live masterclass