Postman Tutorial
- 1. Introduction to Postman
- 2. Sending Requests & Body Types
- 3. Environments & Variable Scopes
- 4. Collections & Folders
- 5. Writing API Tests & Assertions
- 6. Automated Collection Runner
- 7. Postman CLI: Newman
- 8. Integration with CI/CD Pipelines
- 9. Mock Servers
- 10. API Monitoring & Uptime Alerting
- 11. Collaborative Workspaces
- 12. Generating API Documentation
- 13. Designing APIs (OpenAPI / Swagger)
- 14. Advanced Authorization
- 15. Postman Flows & Best Practices
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/usersThe Hierarchy of Variable Scopes
Variables have different lifetimes and access visibilities. Postman processes them in order from broadest (outermost) to narrowest (innermost) scope:
- Global Variables: Broadest scope. Active across all collections, folders, and environments inside your active workspace.
- 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.
- Collection Variables: Bound strictly to a single collection. Accessible to all requests inside that collection regardless of which environment is selected.
- Data Variables: External values fed into requests during automated collection runs using CSV or JSON data sheets.
- 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");