Postman Environment Variables
Tags: postman, rest, api, testing
Published: 2020-02-26 09:45:28

*Originally posted on the [Paymentshield Tech blog](https://paymentshield.tech)*

Postman is great for quick API tests. But can we get even quicker by saving time with environment variables? I'm Nick, I'm a software development intern at [Paymentshield](https://paymentshield.co.uk), on placement from Manchester Metropolitan, and I'm going to talk a little about how we can use this powerful feature!

## What is Postman?

Postman is an API (Application Programming Interface) development tool which helps to build, test and modify APIs. In Postman, you can  make various types of HTTP requests (GET, POST, PUT, DELETE, PATCH), save environments for later use, and convert the API to code for various languages (like JavaScript and Python).

## What is an environment, and why use them?

An environment is a set of variables that allows you to switch the context of your requests.  Environments are useful as they allow you to create robust requests that you can reuse; by storing values in variables, you can reference it throughout your collections, environments and requests – and if you need to update the value, you only have to change it in one place. This increases your ability to work efficiently and minimize the likelihood of error.

## Setup

To add environmental parameters to Postman, first we have to create an environment . To do this, click on the cog wheel located near the top right of the application (highlighted in yellow below)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qqqzpnse36f5vuh17mxs.png)

After you click the cog wheel, an overlay called "Manage Environments" should appear. Click on the Add button.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xdncldl5ujhjab6n73pk.png)

Provide an environment name (like Development), and then enter the variable information you want in  this environment. Once you are finished entering all your variables, click Add.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/f0vgdcsrxd5iqx9o0wf1.png)

After you have added the environment, close the "Manage Environments" overlay and then click on the dropdown near the top right. Change the environment to the one you just created.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kp8rpler4r9f1ltj27ky.png)

On a new request, navigate to the Headers tab. Enter your key, and for the value you want to surround the variable name with two curly braces, for example: '{% raw %} {{userid}} {% endraw %}'.

 > Tip: entering a curly brace into the value box will display to you the possible variables to use in the environment.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/acn34l6hgs8xwvyjysdq.png)

Do this for all of the `key:values` that you need in your header.

Alternatively, you may want to use the Bulk-Edit feature. To add your `key:values` this way, click on "Bulk Edit".

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8lnejxry5rhf23sdsvnq.png)

You can enter your key value pairs in here by the syntax: `key:value`. When using variables as your value, you must wrap the variable with two curly braces, like '{% raw %} {{userid}} {% endraw %}'.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/15ik9evgu7fp0kdk7z2d.png)

Enter the API URL and then send your request.

One last tip: Quickly set your variables from the data in a response body by highlighting the data you want to be stored in an existing variable, right clicking it, and selecting the environment and variable name, as pictured below. This will update it everywhere you've used the matching variable.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/n4ak0iykqh6im33v3xly.png)


## Wrap up

Hopefully this quick guide has helped you with learning a bit more about:

 * What Postman is and why we use it
 * What environments are and why we should use them
 * How to setup your environment
 * How to actively apply environment variables within your requests
 
How do you use Postman?