0
0
PostmanComparisonBeginner · 4 min read

Environment vs Global vs Collection Variable in Postman: Key Differences

In Postman, 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 TypeScopeVisibilityTypical Use CasePriority OrderExample
Environment VariableSpecific to selected environmentVisible only when environment is activeManage environment-specific data like URLs, tokensOverrides collection and global variablesbase_url = https://api.dev.example.com
Global VariableAvailable across all environments and collectionsAlways visible in PostmanStore data shared across multiple environments or collectionsLowest priorityapi_key = 12345
Collection VariableLimited to a specific collectionVisible only within that collectionStore data relevant to a particular collection's requestsOverrides global but below environment variablestimeout = 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.

javascript
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');
});
Output
PASS Check base_url environment variable
↔️

Collection Variable Equivalent

This example shows how to set and use a collection variable in a Postman test script.

javascript
pm.collectionVariables.set('timeout', '5000');

pm.test('Check timeout collection variable', () => {
    const timeout = pm.collectionVariables.get('timeout');
    pm.expect(timeout).to.eql('5000');
});
Output
PASS Check timeout collection variable
🎯

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.

Key Takeaways

Environment variables are best for managing settings per environment and have the highest priority.
Collection variables scope data to a single collection and override global variables but not environment variables.
Global variables are accessible everywhere but have the lowest priority and should be used for truly shared data.
Variable priority order is: environment > collection > global.
Use the right variable type to keep your tests organized and easy to maintain.