Postman - Environment Variables



Variables give the option to hold and repeat parameters in the requests, collections, scripts and so on. If we need to modify a value, we need to do it in only one place. Thus, the variables help to minimise the chance of errors and increase efficiency.

In Postman, an environment consists of a key-value pair. It helps to identify each request separately. As we create environments, we can modify key-value pairs and that will produce varied responses from the same request.

The key in the key−value pair in the environment is known as the Environment variable. There can be multiple environments and each of them can also have multiple variables. However, we can work with a single environment at one time.

In short, an environment allows the execution of requests and collections in a varied data set. We can create environments for production, testing and development. Each of these environments will have different parameters like URL, password, and so on.

Create Environment

Follow the steps given below to create an environment in Postman −

Step 1 − Navigate to the New menu and then click on Environment.

New menu

Step 2MANAGE ENVIRONMENTS pop-up gets opened. We have to enter the Environment name. Then, add a variable name and value.

Here, we have added the variable u and the value as https://jsonplaceholder.typicode.com/users. Close the pop-up.

MANAGE ENVIRONMENTS

Step 3 − The new Environment (ENV1) gets reflected as one of the items in the No Environment dropdown.

Dropdown

Step 4 − Select the ENV1 environment and enter {{u}} in the address bar. To utilise an Environment variable in a request, we have to enclose it with double curly braces ({{<Environment variable name>}}).

Step 5 − Then, click on Send. This variable can be used instead of the actual URL. We have received the Response code 200 0K (meaning the request is successful).

URL

Environment Variables Scope

The scope of an Environment variable is within the environment for which it is created. This means it has a local scope confined to that environment. If we select another environment, and try to access the same Environment variable, we shall get an error.

In this chapter, we have created an Environment variable u within the ENV1 environment and on sending a GET request, we got the desired response.

However, if we try to use the same Environment variable u from another Environment, say Environment_Test, we will receive errors.

The following screen shows the error, which we may get if we use the same Environment variable u from another environment −

Variables Scope
Advertisements