Managing Environments in Postman
Before we jump into environments and how to manage them, let's understand what Postman is and its role in API testing.
What is Postman?
Postman is an API platform tool for building and testing APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration so we can create better APIs—faster.
· The Postman app listens for any calls made by the client app or device.
· The Postman proxy captures the request and forwards the request onward to the server.
· The server returns a response through the Postman proxy back to the client.
What is Environment?
An environment is a set of variables we can use in our postman request. We can use environments to group related sets of values together and manage access to shared Postman data as we work as a team.
· While working with APIs, we often need different setups for our local machine, the development server, or the production server. Environments let us customize requests using variables so we can easily switch between different setups without changing the requests.
· We won’t have to remember all those values once they are in Postman. We can download environments, save them as JSON files, and upload them later.
· We can create, share, duplicate, export, and delete an environment. We can also import an environment as a single JSON file.
Creating an Environment
Postman displays the active environment in the environment selector, located in the header.
You can access all environments from Environments in the sidebar. Select the set active icon next to an environment to make it the active environment.
Step1: We can create an Environment in two ways.
Option1: Click the New button near workspace and choose Environment option from pop up.
Option2: To create a new Environment, select Environments on the left and select + button.
Step2: Enter the name of the Environment and click on Save.
The below screenshot indicates the 2 environments that we created – ‘Production’ and ‘Test’. Select an environment's name to open the environment editor.
Variables in Postman
· Postman variables work in the same way as that of the programming language variables. As we know, a variable is an entity, which stores value. We can change the value of the variable. We can store the values in variables and can use it throughout in requests, environments, collections, and scripts.
· In postman, a variable is a set of key-value pairs. Here the key is the variable and value are the value of the variable.
· Variables in Postman increase the user's efficiency to work and decrease the errors.
· When the variable value changes, we just need to update at one place and the change will reflect across all the requests automatically.
Environment Variable in Postman
Environment variables are the most heavily used kind of variables in Postman. An environment variable is a set of key-value pairs. The key represents the name of the variable and referencing the name of the variable allows to access its value. We can use a data editor to modify an environment.
They are tied to a selected environment that’s being used for executing the request. They have a narrower scope than the Global variables but broader than the Collection variables.
One environment can be active at a time. If we have a single environment, using collection variables can be more efficient, but environments enable us to specify role based access levels.
There are two types of environment variables:
· User environment variables (set for each user)
· System environment variables (set for everyone).
Creating an Environment Variable in Postman
1. Select Add a new variable and enter a name for the variable.
2. Select a Type for the new variable.
3. Add an Initial Value, and if we choose a Current Value.
4. Select Save to confirm our changes.
Add the following variables for the ‘Test’ environment,
Add the following variables for the ‘Production’ environment,
When To Use the Environment Variables?
1. Environment variables are ideal when working with different servers. For example, generally, while working on a project we work with a lot of different environments like dev, test, uat and prod, etc. which differ mostly just by URL and the rest of the things in the API endpoint request URL remain the same.
2. When there is a need for passing data or information from one request to another, environment variables are a good choice, as they have a broader scope than the Local variables and narrower scope than Global variables.
In the example below, we have created environment variables for the base ‘url’ and the resource ‘id’ as highlighted.
Choose ‘Test’ environment from the drop down below. As we can see, the newly created environment variables are substituted in place of ‘url’ and the resource Id.
Should you need to switch to another environment like ‘Production’, sure, why not?
Just Choose ‘Production’ environment from the drop down below and the Production values reflect automatically.
Once the environments are configured, we can share with the team members with one of the following two ways,
Click on view more actions beside Environment name and click on share.
Click on the Environment name which we want to share and click share button on header.
Here are the steps involved to share an environment with the team members.
Step 1: Enter the email address or distribution list for the group
Step 2: Assign the role to control the access. There are two types of roles,
· Editor Role: Users who are designated as editors can create and edit environments, helping define usable and reliable environments that other users can apply to their collections.
· Viewer Role: Users who are designated as viewers can view and put environments within workspaces to use but cannot make changes to the shared values that exist within an environment.
Step 3: Choose the appropriate way to collaborate with the teams. We can either choose to share only the current environment or choose to make everything on the workspace to be available for the team.
Step 4: Select ‘Share Environment’ button to share with the team members.
I hope you find it useful, Happy Learning!!