Using environments to switch contexts

  • App

A very common scenario while testing APIs is that the API infrastructure might be present on your local machine, a staging setup, and a production setup. Using variables you can call all these APIs without having to replace values manually. You can also use variables to test for different users (for example, they might need different access tokens), different input values, etc.

To test a request across different environments,

1. Create an environment

  • Click on “No environment” in the header.

  • Select “Manage environments” and then the “Add” button in the modal that comes up.

  • Fill in the values as shown in the screenshot below.

  • Press submit.

2. Create the next environments you will be testing.

To create another environment, we can duplicate this environment so that we don’t have to type in variable keys all over again.

3. Place environment variables in the request.

Variables can be used almost everywhere in Postman. They are available inside:

  • URL
  • URL params
  • Header values
  • form-data/url-encoded values
  • Raw body content
  • Helper fields

To use a variable you need to enclose the variable name with double curly braces – {{my_variable_name}}.

4. Now select an environment from the environment selection dropdown.

Once an environment is selected, Postman will replace all instances of a variable with it’s corresponding value. If no environment is selected, then Postman will try to find a matching global variable.

5. Hit Send.

6.Change the environment and hit send again. The request will be made to the new environment you picked.

7. Repeat until all environments are tested.

To see some examples of testing in multiple environments and other use cases for variables, check out this blog post.