3. Environments & Variable Scopes

Hardcoding values (like URLs, API keys, or user IDs) into individual requests is highly inefficient. If your server URL changes from http://localhost:8080 to https://api.staging.com, you would have to edit every single request manually. Postman solves this using **Variables and Environments**.

Variable Syntax: Double Curly Braces

Variables in Postman are referenced inside text fields (like URL bar, headers grid, and JSON payloads) using double curly braces:

{{variable_name}}

For example, if you declare a variable named baseUrl, your request endpoint is defined as:

{{baseUrl}}/api/v1/users

The Hierarchy of Variable Scopes

Variables have different lifetimes and access visibilities. Postman processes them in order from broadest (outermost) to narrowest (innermost) scope:

  1. Global Variables: Broadest scope. Active across all collections, folders, and environments inside your active workspace.
  2. Environment Variables: Context-specific sets (e.g. "Development," "Staging," "Production"). You activate an environment using the dropdown menu in the top right corner. Only variables within the active environment are resolved.
  3. Collection Variables: Bound strictly to a single collection. Accessible to all requests inside that collection regardless of which environment is selected.
  4. Data Variables: External values fed into requests during automated collection runs using CSV or JSON data sheets.
  5. Local Variables: Inside test scripts or pre-request scripts, these exist only during the execution of that specific request.

Dynamic Variables: Built-in Data Generators

When testing signup APIs or data insertion logs, you need unique values (like random names, emails, or UUIDs) to prevent database collisions. Postman includes hundreds of built-in **Dynamic Variables** prefixed with a dollar sign:

  • {{$guid}}: Generates a unique v4 UUID.
  • {{$randomEmail}}: Generates a random, validly-formatted email address.
  • {{$randomFullName}}: Generates a random first and last name.
  • {{$randomInt}}: Generates a random integer between 0 and 1000.
  • {{$timestamp}}: Generates the current Unix timestamp in seconds.

Example: Sending a Random Profile Payload

{
  "name": "{{$randomFullName}}",
  "email": "{{$randomEmail}}",
  "apiKey": "secure-key-{{$guid}}"
}

Configuring Variables in Pre-Request & Test Scripts

You can read, update, or clear variables dynamically using Javascript code in the **Pre-request Script** or **Tests** tabs:

// 1. Set a global variable
pm.globals.set("tempKey", "12345");

// 2. Set an environment variable (active environment only)
pm.environment.set("accessToken", "token_xyz");

// 3. Read a variable dynamically from the active scope hierarchy
let currentUrl = pm.variables.get("baseUrl");

// 4. Clear an environment variable
pm.environment.unset("accessToken");
Security Rule: Never save sensitive secrets (like live AWS credentials or billing keys) inside the Initial Value column of variables if sharing collections! The initial value is synced to Postman cloud servers and visible to teammates. Always use the Current Value column, which stays strictly local to your machine!