Postman Environments and Variables

Every professional API setup has at least three versions: development, testing, and production. Each version uses a different base URL and often a different API key. Environments in Postman let you switch between these setups instantly without editing every request manually.

The Problem Variables Solve

Suppose you are testing a payments API. Your base URL changes depending on where you are in the project:

Development:  https://dev.payments-api.com
Testing:      https://staging.payments-api.com
Production:   https://api.payments-api.com

Without variables, you update every single request URL each time you switch environments. With variables, you change one value and every request updates automatically.

How Variables Work – The Template Diagram

Variable defined in Environment:
  baseUrl = https://dev.payments-api.com

Request URL using the variable:
  {{baseUrl}}/users/1

What Postman sends:
  https://dev.payments-api.com/users/1

Switch Environment:
  baseUrl = https://api.payments-api.com

What Postman sends now:
  https://api.payments-api.com/users/1

The double curly braces {{variableName}} are Postman's syntax for inserting a variable value.

Types of Variables in Postman

Variable TypeScopeBest Used For
GlobalAll workspaces and environmentsValues that never change across any context
EnvironmentOne environment onlyBase URL, API keys per environment
CollectionAll requests in one collectionShared values for one project
LocalCurrent request onlyTemporary values set in scripts
DataOne Collection Runner runValues loaded from a CSV or JSON data file

When multiple variable types have the same name, Postman uses this priority: Local beats Data, which beats Environment, which beats Collection, which beats Global.

Create an Environment

  1. Click the Environments tab in the left sidebar
  2. Click the + button to create a new environment
  3. Name it Development
  4. Add variables by filling in the table:
Variable NameInitial ValueCurrent Value
baseUrlhttps://jsonplaceholder.typicode.comhttps://jsonplaceholder.typicode.com
userId11
  1. Click Save

Repeat this process to create a Staging environment with a different base URL.

Initial Value vs Current Value

  • Initial Value — saved with the environment and visible to teammates when you share the environment. Do not put real passwords or secret API keys here.
  • Current Value — only visible to you locally. Safe to put sensitive values here. This value overrides the initial value during your current session.

Use Variables in a Request

  1. Select your Development environment from the dropdown in the top-right corner of Postman
  2. Open a request and type {{baseUrl}}/users/{{userId}} in the URL bar
  3. Click Send

Postman replaces {{baseUrl}} with https://jsonplaceholder.typicode.com and {{userId}} with 1 before sending. The URL bar shows the variables highlighted in orange so you can see where they appear.

Set Variables Dynamically from a Script

You can set variable values automatically from a test script. A common use case is saving an access token after a login request.

// In the Scripts tab → Post-response section:
const response = pm.response.json();
pm.environment.set("accessToken", response.token);

After the login request runs, the token is saved to the accessToken variable in your environment. Every subsequent request that uses {{accessToken}} picks up the saved value automatically.

View and Edit Active Variables

Click the eye icon in the top-right corner of Postman (next to the environment dropdown) to open a quick view of all current variable values. You can edit values directly from this panel without opening the full environment editor.

Share an Environment

Export an environment to a JSON file the same way you export collections:

  1. Click the three-dot menu on the environment in the sidebar
  2. Select Export
  3. Save the JSON file

Share this file with teammates. They import it into their Postman and have the same environment setup immediately. Remember: share initial values, never current values that contain real secrets.

Summary

Environments hold variables that change between development, staging, and production setups. You use {{variableName}} syntax anywhere in Postman — URLs, headers, body, scripts — to insert variable values. Switching environments updates all requests instantly. Scripts can set variables dynamically so that values like login tokens save and reuse themselves automatically.

Leave a Comment

Your email address will not be published. Required fields are marked *