Environment vs Global vs Collection Variable in Postman: Key Differences
environment variables are specific to a selected environment and help manage settings per environment, global variables are accessible across all environments and collections, and collection variables are limited to a specific collection for scoped reuse. Each type controls variable scope and priority during API testing.Quick Comparison
This table summarizes the key differences between environment, global, and collection variables in Postman.
| Variable Type | Scope | Visibility | Typical Use Case | Priority Order | Example |
|---|---|---|---|---|---|
| Environment Variable | Specific to selected environment | Visible only when environment is active | Manage environment-specific data like URLs, tokens | Overrides collection and global variables | base_url = https://api.dev.example.com |
| Global Variable | Available across all environments and collections | Always visible in Postman | Store data shared across multiple environments or collections | Lowest priority | api_key = 12345 |
| Collection Variable | Limited to a specific collection | Visible only within that collection | Store data relevant to a particular collection's requests | Overrides global but below environment variables | timeout = 5000 |
Key Differences
Environment variables are tied to a specific environment you select in Postman, such as development, staging, or production. They allow you to switch contexts easily by changing the environment, which updates all variables scoped to that environment. This is useful for managing different API endpoints or credentials without changing your requests.
Global variables have the broadest scope and are accessible regardless of the active environment or collection. They are useful for data you want to share everywhere, like a user ID or a token used across multiple projects. However, because they have the lowest priority, they can be overridden by environment or collection variables with the same name.
Collection variables are scoped to a single collection and are only available within that collection's requests and scripts. They provide a middle ground, allowing you to store data specific to a project or API without affecting other collections or environments. Their priority is higher than global variables but lower than environment variables, so they can be overridden by environment variables if both exist.
Code Comparison
Here is an example showing how to set and use an environment variable in a Postman test script.
pm.environment.set('base_url', 'https://api.dev.example.com'); pm.test('Check base_url environment variable', () => { const baseUrl = pm.environment.get('base_url'); pm.expect(baseUrl).to.eql('https://api.dev.example.com'); });
Collection Variable Equivalent
This example shows how to set and use a collection variable in a Postman test script.
pm.collectionVariables.set('timeout', '5000'); pm.test('Check timeout collection variable', () => { const timeout = pm.collectionVariables.get('timeout'); pm.expect(timeout).to.eql('5000'); });
When to Use Which
Choose environment variables when you need to switch between different setups like dev, test, or production without changing your requests. They are perfect for URLs, tokens, or credentials that vary by environment.
Choose collection variables when you want to keep data specific to a project or API collection, such as timeouts or feature flags, that should not affect other collections or environments.
Choose global variables for data that is truly universal across all your workspaces and environments, like a user ID or a shared API key, but use them sparingly to avoid conflicts.