Collections, environments and variables
A collection says what to request and an environment says where and with what values, so one collection runs on dev, stage and prod. The host and data are written as {{baseUrl}}, with values kept in the environment, switched from a list. Dynamic variables such as {{$guid}} give a new value on every substitution, so a value also needed in an assertion is fixed with a script.
API · Postman: structure and variables
Same requests, different environments. How do you avoid a collection copy for dev, stage and prod?
The same API lives on dev, stage and prod. The simplest move is to copy the collection three times and replace the host in every request. Within a month the copies drift apart: one got a fixed request body, the second did not, the third still has a stage account. Postman solves it by splitting the roles: the collection describes what to ask and the environment says where and with which values. This lesson covers how they combine and where a variable can let you down.
How it works and why it is designed so
A collection is a set of requests for one API, laid out in folders (Auth, Accounts, Transfers). It also carries shared settings: authorization, scripts, variables, a description. Folders and requests inherit them, so the token and baseUrl are configured once and not in every request (more in the lesson on collection structure).
An environment is a named set of variables for a specific stand. One collection, three environments, and the stand is switched from the list in the top right corner. Before sending, Postman substitutes variable values into the URL, params, headers, body and authorization: everything written as {{name}}. Substitution happens at send time, so changing the environment affects the next request without editing the collection.
One collection, three environments
Example: before and after
Before: three sets of requests with hard-coded hosts.
# Three copies of the collection, a different host in every request
GET https://dev.example.test/accounts/acc-1001
GET https://stage.example.test/accounts/acc-1001
GET https://example.test/accounts/acc-1001
After: the host and the identifier are variables and the values live in the environment.
# One collection, only the selected environment changes
GET {{baseUrl}}/accounts/{{accountId}}
An environment is stored as JSON and can be exported. Here is a simplified stage example (a real file has more service fields and identifiers):
{
"name": "stage",
"values": [
{ "key": "baseUrl", "value": "https://stage.example.test",
"type": "default", "enabled": true },
{ "key": "accountId", "value": "acc-1001",
"type": "default", "enabled": true }
],
"_postman_variable_scope": "environment"
}
Dynamic variables generate data at run time: {{$guid}}, {{$timestamp}}, {{$randomInt}}, {{$randomEmail}}. They are needed when every run must create a unique user or key, so it does not hit a 409 on a duplicate. The trap: the value is generated during substitution, so you cannot read it back later. If the email is needed both in the body and in a check, fix it with a script through pm.variables.replaceIn:
// Pre-request: one email for the request body and for the check
const email = pm.variables.replaceIn('{{$randomEmail}}');
pm.variables.set('email', email);
// request body: { "email": "{{email}}" }
// Post-response of the same request
pm.test('The email in the response is the one we sent', () => {
pm.expect(pm.response.json().email).to.eql(pm.variables.get('email'));
});
Limits and alternatives
An environment answers "where", but not "who": test logins and tokens are often put into variables too, and that is where the topic of secrets starts (see the lesson on variable scopes and secrets). To change behaviour inside one stand (different roles, different plans) environments are not enough: use data in the Runner or separate folders. Do not confuse an environment with collection copies on branches: one collection with several environments keeps one version of the logic.
Common mistakes
- No environment selected. Variables are not substituted, a literal
{{baseUrl}}stays in the URL and the request never reaches the server; - A host typed into a request. On another stand the test quietly hits the old host;
- Prod data in the dev environment and vice versa. Name the variables the same and keep the values apart, and check which environment is active before a run;
- A dynamic variable instead of stable data. A "find the created one" test cannot find it because each substitution gives a new value;
- Exporting an environment into the repository with real keys. Export only non-secret values.
Check yourself
A request goes to an address with a literal {{baseUrl}} and fails. Which two causes should you check first?
Answer
First, whether an environment is selected (with "No environment" the variable does not exist). Second, whether baseUrl exists in the active environment and the name has no typo (case matters).
Why is one collection and three environments better than three collections?
Answer
The request and check logic is the same, so it is edited in one place. Three copies drift apart and a bug fixed in one stays in the others. Only values differ (host, account) and they live in the environment.
{{baseUrl}} and switch dev, stage, prod from the list without copying requests. Middle/Senior: One collection is one version of the logic; environments carry only the values that differ between stands. I fix dynamic variables with a script when the value is needed in a check, and keep secrets out of shared values and exports.