Postman Collections & Environment File
Collections
A Collection is a folder-like grouping of related requests, letting testers organize an entire API's endpoints (or a full test suite) in one place, complete with sub-folders and shared settings.
Environment Files
An Environment is a named set of variables (like base URL, API key, or auth token) that can be swapped in and out — allowing the same collection of requests to run against Dev, QA, or Production without editing each request individually.
Why This Combination Matters
Together, Collections and Environments let a tester maintain one reusable, organized suite of requests that can target any environment simply by switching the active Environment.
Variable Scopes and Which One Wins
Postman resolves a variable such as {{baseUrl}} by looking through several scopes. When the same name exists in more than one, the narrowest scope wins:
| Scope | Lives in | Good for |
|---|---|---|
| Local | A single request run, set by a script | Temporary values during one execution |
| Data | A CSV or JSON file used by the runner | Data-driven iterations |
| Environment | The active environment | Base URLs, credentials and tokens per server |
| Collection | The collection itself | Values shared by every request, whatever the server |
| Global | The whole workspace | Rarely needed; easy to misuse |
Environment variables also have an initial value, which is shared when the environment is exported or synced, and a current value, which stays on your machine. Put secrets only in the current value or use the secret variable type.
Worked Example: Dev and QA From One Collection
Create two environments, Dev and QA, each with baseUrl, username and password. Requests in the collection never contain a hardcoded server:
POST {{baseUrl}}/auth/login
Body: { "username": "{{username}}", "password": "{{password}}" }
GET {{baseUrl}}/orders
Header: Authorization: Bearer {{token}}
The login request saves the token in its Tests (post-response) script so every later request can use it:
pm.test("Login succeeds", () => {
pm.response.to.have.status(200);
});
const body = pm.response.json();
pm.environment.set("token", body.token);
The orders request then checks its own response:
pm.test("Orders list is an array", () => {
pm.expect(pm.response.json().orders).to.be.an("array");
});
pm.test("Responds within two seconds", () => {
pm.expect(pm.response.responseTime).to.be.below(2000);
});
Switching the environment dropdown from Dev to QA reruns the whole flow against the other server with no edits.
Running the Collection Outside the App
Export the collection and environment as JSON files, then run them from the command line with Newman, which is also how CI pipelines execute them:
npm install -g newman
newman run orders.postman_collection.json -e qa.postman_environment.json
newman run orders.postman_collection.json -e qa.postman_environment.json -d users.csv -r cli,junit
The -d option feeds a data file for multiple iterations, and the junit reporter produces XML that build servers can display.
Organising Collections Well
- Use folders per feature or resource, such as Auth, Orders and Payments, and run folders in a logical order.
- Keep shared scripts at collection or folder level instead of copying them into every request.
- Give requests readable names like "Create order - missing address" so runner results make sense.
- Store exported files in version control, but never with real passwords or tokens in initial values.
- Avoid global variables for anything environment-specific; they leak between unrelated collections.
- Clear or reset tokens at the start of a run so stale values do not hide login failures.
A small sanity check before sharing a collection: select a fresh environment, run it from top to bottom, and confirm nothing depends on a value only your machine holds.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.