06 Variables
title: "Postman Variables: Global, Environment, Collection" description: "Use Postman variables across scopes: global, environment, collection, and local. Learn variable precedence, dynamic variables, data files, and variable management for flexible API testing." weight: 6 date: 2026-06-28 lastmod: 2026-06-28 tags: [api-development, postman] }
Postman variables store dynamic values that can be reused across requests. They come in four scopes: global (always available), environment (context-specific), collection (collection-specific), and local (request-scoped). Variables follow a precedence hierarchy.
What You'll Learn
- Variable scopes and precedence
- Environment variables for different deployments
- Collection variables for shared values
- Dynamic variables (timestamp, guid, random)
- Data variables from CSV/JSON files
Why It Matters
Variables eliminate hardcoded values, making collections reusable across environments. Environment variables enable switching between dev, staging, and production with one click.
Real-World Use
Postman collections for multi-environment deployments use environment variables for base URL, API keys, and database identifiers. Collection variables store resource IDs created during test runs.
flowchart TD
Precedence[Variable Precedence] --> Data[Data Variables
Highest Priority]
Data --> Local[Local Variables]
Local --> Environment[Environment Variables]
Environment --> Collection[Collection Variables]
Collection --> Global[Global Variables
Lowest Priority]
Teacher Mindset
Use global variables for values that never change (API version). Use environment variables for deployment-specific values (URL, tokens). Use collection variables for values shared across requests (resource IDs). Use local variables for temporary values.
Code Examples
// Example 1: Setting and getting variables
// Set variables from response
const user = pm.response.json();
pm.collectionVariables.set('user_id', user.id);
pm.environment.set('auth_token', user.token);
pm.variables.set('temp_id', user.id); // Local variable
// Get variables
const baseUrl = pm.variables.get('base_url');
const token = pm.variables.get('auth_token');
// Check existence
const hasToken = pm.environment.has('auth_token');
// Example 2: Dynamic variables in pre-request
// Postman built-in dynamic variables
// {{$guid}} - Random UUID
// {{$timestamp}} - Current timestamp
// {{$randomInt}} - Random integer
// {{$randomEmail}} - Random email
pm.variables.set('order_id', '{{$guid}}');
pm.variables.set('created_at', '{{$timestamp}}');
pm.variables.set('customer_email', '{{$randomEmail}}');
// Custom dynamic value
const now = new Date();
pm.variables.set('today', now.toISOString().split('T')[0]);
// Example 3: Environment management
// Create environment variables programmatically
const environments = {
dev: {
base_url: 'http://localhost:3000',
api_key: 'dev-key-123'
},
staging: {
base_url: 'https://staging.api.example.com',
api_key: 'staging-key-456'
},
production: {
base_url: 'https://api.example.com',
api_key: '{{prod_api_key}}' // Secret from CI
}
};
// Switch environment in pre-request
const env = pm.environment.get('current_env') || 'dev';
const config = environments[env];
pm.environment.set('base_url', config.base_url);
Common Mistakes
- Hardcoding values instead of using variables
- Not understanding variable precedence (global vs environment conflicts)
- Storing secrets in collection variables visible to all team members
- Using wrong variable scope (environment vs collection)
- Not using dynamic variables for unique test data
Practice
- Create a global variable for API version.
- Create environment variables for base_url (dev, staging, prod).
- Use collection variables to store a created resource ID.
- Use {{$guid}} and {{$timestamp}} dynamic variables.
- Challenge: Write a script that copies environment variables from one environment to another.
FAQ
Mini Project
Set up three environments (dev, staging, production) with base_url and api_key variables. Create a collection variable for tracking a user_id across requests. Use dynamic variables for creating unique test data. Write a pre-request script that selects the environment.
What's Next
Next, you will learn about pre-request scripts for setting up request data.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro