postman-project/ ├── collections/ │ └── MyAPI.postman_collection.json ├── environments/ │ ├── dev.postman_environment.json │ ├── staging.postman_environment.json │ └── prod.postman_environment.json ├── scripts/ │ ├── pre-request-scripts.js │ └── test-scripts.js ├── README.md └── postman_globals.json
Collection variables in Postman - Framework Patterns
- Collection Layer: Contains API requests grouped logically. Uses collection variables for shared values.
- Environment Layer: Holds environment-specific variables like base URLs and credentials.
- Scripts Layer: Includes pre-request and test scripts to run before or after requests.
- Globals Layer: Stores global variables accessible across collections and environments.
- Configuration Layer: Manages environment files and collection variables for flexible testing.
Collection variables are defined inside the Postman collection JSON or via the Postman app UI. They store values shared by all requests in the collection.
Use collection variables to hold values like API keys, tokens, or common parameters that do not change per environment.
For environment-specific values (like base URLs), use environment variables instead.
Example of setting a collection variable in a pre-request script:
pm.collectionVariables.set("api_key", "12345abcde");
To access a collection variable in a request URL or header, use {{variable_name}} syntax.
Postman collections can be run via Newman, the command-line runner.
Newman supports generating reports in formats like HTML, JSON, and JUnit XML.
Example Newman command with HTML report:
newman run collections/MyAPI.postman_collection.json \ -e environments/dev.postman_environment.json \ --reporters cli,html \ --reporter-html-export reports/report.html
Integrate Newman runs into CI/CD pipelines (GitHub Actions, Jenkins, GitLab CI) to automate API testing on code changes.
- Use collection variables for values shared across all environments and requests, like API keys.
- Keep sensitive data out of collection variables; use environment variables or secure vaults.
- Use descriptive names for variables to improve readability and maintenance.
- Document variable usage in README or collection description for team clarity.
- Regularly review and clean unused variables to keep collections tidy.
Where in this Postman project structure would you add a new collection variable for an API token used by all requests?
Practice
collection variables in Postman?Solution
Step 1: Understand what collection variables do
Collection variables hold values that can be reused across all requests in a collection, making tests easier to manage.Step 2: Compare options with this purpose
Only To store reusable values accessible by all requests in a collection describes storing reusable values for all requests, which matches the purpose of collection variables.Final Answer:
To store reusable values accessible by all requests in a collection -> Option AQuick Check:
Collection variables = reusable values [OK]
- Confusing collection variables with environment variables
- Thinking collection variables store API responses
- Assuming collection variables control UI settings
api_key inside a request URL in Postman?Solution
Step 1: Recall Postman variable syntax
Postman uses double curly braces {{variable_name}} to insert variables into requests.Step 2: Match syntax with options
Only https://api.example.com/data?key={{api_key}} uses {{api_key}}, which is the correct way to reference a collection variable.Final Answer:
https://api.example.com/data?key={{api_key}} -> Option DQuick Check:
Variable syntax = {{variable_name}} [OK]
- Using plain variable name without braces
- Using square brackets or dollar sign instead of {{}}
- Confusing environment and collection variable syntax
base_url set to https://api.test.com, what will be the full URL for this request?GET {{base_url}}/usersSolution
Step 1: Substitute the collection variable
The variable {{base_url}} is replaced by its value https://api.test.com in the request URL.Step 2: Combine the parts of the URL
After substitution, the URL becomes https://api.test.com/users as the path /users is appended.Final Answer:
GET https://api.test.com/users -> Option AQuick Check:
Variable replaced by value = full URL [OK]
- Not replacing the variable and leaving {{base_url}} as is
- Appending variable value incorrectly
- Ignoring the path after the variable
token but your request still sends Authorization: Bearer {{token}} literally without replacing it. What is the likely cause?Solution
Step 1: Understand variable replacement behavior
If Postman sends {{token}} literally, it means the variable was not found or set correctly.Step 2: Identify common causes
Most often, the variable was not saved or defined in the collection variables, so Postman cannot replace it.Final Answer:
The collection variabletokenwas not saved properly -> Option BQuick Check:
Missing variable value = no replacement [OK]
- Assuming single braces work for variables
- Thinking headers can't use variables
- Ignoring case sensitivity issues
user_id in a request body JSON like this:{ "id": "{{user_id}}", "action": "delete" }But the API expects
id as a number, not a string. How should you fix the request body to send user_id as a number?Solution
Step 1: Understand JSON data types
In JSON, numbers should not be inside quotes; quotes make them strings.Step 2: Adjust variable usage in JSON
Remove quotes around {{user_id}} so Postman inserts the number directly, matching API expectation.Final Answer:
{ "id": {{user_id}}, "action": "delete" } -> Option CQuick Check:
Numbers in JSON = no quotes [OK]
- Keeping quotes around numeric variables
- Using wrong variable syntax with $ or []
- Sending arrays instead of single values
Ready to prove it?
Pass the test and this pattern moves unknown
