← Back to Chapters

Environments in Postman

? Environments in Postman

? Quick Overview

Environments in Postman allow you to store and reuse variables such as base URLs, tokens, and credentials. They help you switch easily between development, testing, staging, and production without modifying requests manually.

? Key Concepts

  • Environment variables are key-value pairs
  • Different environments can share the same requests
  • Variables are referenced using double curly braces
  • Environment scope overrides global variables

? Syntax / Theory

Variables inside environments are accessed using the syntax {{variable_name}}. Postman resolves these values dynamically based on the currently selected environment.

? View Code Example
// Using an environment variable in a request URL
GET {{base_url}}/users

? Code Example(s)

? View Code Example
// Setting an environment variable in Tests tab
pm.environment.set("auth_token", pm.response.json().token);

? Live Output / Explanation

Explanation

When the request runs, Postman extracts the token from the response and stores it in the active environment. This token can then be reused in headers or other requests.

?️ Interactive Example

Imagine switching between Development and Production environments using the environment dropdown. The same request instantly points to a different server without changes.

?️ Simulator: Select Environment
Postman UI (Input):
GET {{base_url}}/api/v1/users
⬇️
Resolved URL (Network Call):
GET http://localhost:3000/api/v1/users
? View Code Example
// Authorization header using environment variable
Authorization: Bearer {{auth_token}}

? Use Cases

  • Switching API base URLs across environments
  • Managing authentication tokens
  • Testing APIs with different user roles
  • CI/CD automation with Newman

✅ Tips & Best Practices

  • Use clear and consistent variable names
  • Do not store sensitive secrets in shared environments
  • Export environments for backup and sharing
  • Clean unused variables regularly

? Try It Yourself

  1. Create a new environment named Dev
  2. Add a variable base_url
  3. Use it inside a GET request
  4. Switch environments and observe the change