Skip to main content

Environments & Variables

The Problem Without Environments

Imagine you're testing an API that runs locally at http://localhost:3000 during development, at https://staging.api.example.com in staging, and https://api.example.com in production.

Without environments, you'd manually change the base URL in every request whenever you switch. With 20 requests in a collection, that's 20 edits every time.

Environments solve this by letting you define variables once and switch the entire context with a click.

What are Environments?

An environment is a named set of key-value pairs (variables). You create one environment per context (development, staging, production), define the same variable names in each, and switch between them from the top-right dropdown.

Environment: Development
BASE_URL = http://localhost:3000
API_KEY = dev_key_123

Environment: Production
BASE_URL = https://api.example.com
API_KEY = prod_key_abc

In your requests, you use {{BASE_URL}} instead of a hardcoded URL — Postman substitutes the value from the active environment.

Creating an Environment

  1. Click the Environments tab in the left sidebar (or the grid icon)
  2. Click + to create a new environment
  3. Name it (e.g., Development)
  4. Add your variables:
VariableInitial ValueCurrent Value
BASE_URLhttp://localhost:3000http://localhost:3000
API_KEYyour_actual_key
  1. Click Save
Initial Value vs Current Value
  • Initial Value is shared with your team when you export/share the environment. Don't put secrets here.
  • Current Value is only on your machine, never synced. Put sensitive values (API keys, passwords) here.

Using Variables in Requests

Reference any variable with double curly braces: {{VARIABLE_NAME}}

URL: {{BASE_URL}}/api/users

In the Headers tab:

Authorization: Bearer {{TOKEN}}
Content-Type: application/json

In the Body (raw JSON):

{
"api_key": "{{API_KEY}}",
"userId": "{{USER_ID}}"
}

Switching Environments

In the top-right corner of Postman, you'll see a dropdown showing the active environment (or "No Environment"). Click it to switch. Your entire collection instantly uses the new environment's values.

Variable Scope

Postman has four levels of variables, from narrowest to widest scope:

ScopeWhere setUse for
LocalScript onlyTemporary values within a single request
DataCollection runner CSV/JSONTest data in automated runs
CollectionCollection settingsValues shared across the whole collection
EnvironmentEnvironment settingsValues that change per environment
GlobalPostman globalsValues shared across all collections

When the same variable name exists at multiple scopes, the narrower scope wins.

Setting Variables with Scripts

You'll often want to capture a value from a response and use it in later requests — like capturing a JWT token after login.

In the Tests tab of your login request:

Login Request → Tests
const response = pm.response.json();

if (pm.response.code === 200) {
pm.environment.set("TOKEN", response.token);
pm.environment.set("USER_ID", response.user.id);
}

Now every subsequent request that uses {{TOKEN}} in its Authorization header will automatically use the token from the login response.

A Complete Environment Setup Example

Here's a realistic setup for a REST API:

Environment: Development

VariableCurrent Value
BASE_URLhttp://localhost:3000
TOKEN(set by login script)
USER_ID(set by login script)
ADMIN_EMAILadmin@test.com
ADMIN_PASSWORDtest-pass-123

Login Request (POST {{BASE_URL}}/auth/login):

Body:

{
"email": "{{ADMIN_EMAIL}}",
"password": "{{ADMIN_PASSWORD}}"
}

Tests script (captures the token):

pm.environment.set("TOKEN", pm.response.json().token);

Protected Request (GET {{BASE_URL}}/api/profile):

Header:

Authorization: Bearer {{TOKEN}}

Now your entire workflow is environment-driven: run the login request once, and all subsequent requests automatically use the captured token.

Exporting and Sharing Environments

To share an environment with your team:

  1. Click the ... menu next to the environment
  2. Click Export
  3. Save the JSON file and share it (via Git, Slack, etc.)

Your teammates import it (File → Import) and fill in their own Current Values for secrets.

Store environments in your repo

Export your environment files (without sensitive current values) and commit them to your repository. This way new team members can quickly import the correct environment setup.