QA Interview Hub · Knowledge base · Quizzes · Українська

API › Scripts and checks in Postman

Postman: variables, scripts, chaining

A Postman variable comes from the narrowest scope (local, data, environment, collection, global); chained requests pass values through variables. Values for later requests are stored in pm.collectionVariables or pm.environment, because pm.variables lives only until the end of the request or run. A chain makes requests order-dependent, so each creates its own data or explicitly requires its predecessor.

API · Scripts and checks in Postman

A variable of the same name exists in the collection and the environment. Which value goes into the request?

A collection works on dev, and on stage the same baseUrl variable gives another host. The second request reads {{userId}}, which the first one should have stored, and a literal {{userId}} ends up in the URL. Both stories are about the same thing: a Postman variable lives in a particular scope, and scripts decide where a value goes and who can see it. This lesson covers how scopes override one another, how scripts write and read values, and how a chain of requests follows from that.

How it works and why it is designed so

There are five scopes: global, collection, environment, data and local. Postman substitutes the value from the narrowest scope where the variable is found. So an environment variable overrides a collection one of the same name, and a value from a CSV in the Runner overrides both. The idea is that shared values are set once and a specific run can change them without touching the shared settings. Environments (dev, stage, prod) exist so the same collection runs on different stands just by switching the selected environment.

Which value wins

1. Localpm.variables.set, until the request or run ends2. Dataa CSV/JSON row in the Runner3. Environmentdev, stage, prod4. Collectiona value for the whole collection5. Globalevery collection in the workspace
Figure 1. Reading goes top to bottom: the first scope that has the variable wins.

Scripts add writing to this. In the Pre-request and Post-response tabs (the names current Postman uses; the second used to be called Tests) the pm object has one object per scope: pm.environment, pm.collectionVariables, pm.globals, pm.iterationData (no set) and pm.variables. The last one reads from all scopes but writes to local. Scripts can be set on a collection, a folder and a request: they run in the order collection, folder, request.

Example: a "create, then read" chain

The first request creates a user, the second reads it. A common attempt to store the id with pm.variables.set works only inside a collection run, and a request sent alone does not see the id:

// trap: a local variable lives only until the run ends
pm.variables.set('userId', pm.response.json().id);
// GET {{baseUrl}}/users/{{userId}} sent alone with Send goes out with a literal {{userId}}

A local variable lives until its request ends, or until the run ends in the Collection Runner or Newman. In a run the next request sees it, but a request sent alone with Send gets a literal {{userId}}, and the value is gone after the run. A value that must outlive requests needs a longer-lived scope:

// Post-response of POST {{baseUrl}}/users
const user = pm.response.json();
pm.test('User is created', () => {
  pm.response.to.have.status(201);
  pm.expect(user.id).to.be.a('string');
});
// outlives this request: the next ones read {{userId}}
pm.collectionVariables.set('userId', user.id);

The next request uses {{baseUrl}}/users/{{userId}}. The choice between pm.collectionVariables and pm.environment is meaningful: the id of a created user does not depend on the stand, so the collection fits. Values that differ between stands (host, keys) stay in the environment.

Limits and alternatives

A chain through variables has a price: requests become order dependent. Run the second request alone and {{userId}} is empty or stale. So such a request either creates its own data or says in its description that it needs the previous one. For many values per run a data file in the Runner is better. Secrets have their own practices: see the lesson on variable scopes and secrets.

Common mistakes

  • pm.variables.set for data between requests. It works in the Runner, but a request sent alone gets a literal {{userId}}, and the value is gone after the run;
  • Same-named variables in several scopes. Values come from not where you expect, and a test on stage takes a dev value. Give variables unique names or check which scope wins;
  • No environment selected. Environment variables are empty and the request goes to a wrong or empty host;
  • Stale values. A variable left over from the last run masks a failure: clear it with unset when the data is no longer needed.

Check yourself

A variable host exists in the collection with the value a.test and in the active environment with b.test. Which host does the request use?

Answer

b.test. Environment is narrower than collection and overrides it. If the Runner data row also had host, that one would win.

The first request's post-response does pm.variables.set('id', 5), the second, sent separately with Send, reads {{id}} and gets nothing. Why, and what to change?

Answer

pm.variables writes to the local scope, which outside a collection run lives only until the request ends (in a run, until the run ends). To pass data between requests use pm.collectionVariables.set or pm.environment.set.

In an interview. Junior: Variables have scopes: a narrower one overrides a wider one. A pre-request script prepares data, a post-response script checks the response and stores values for the next requests. Middle/Senior: The override order is local, data, environment, collection, global. Data between requests goes through collection or environment, because local lives only until the request or run ends and is not kept between runs. A chain makes requests order dependent, so each one either creates its own data or explicitly requires the previous one, and environment specific values (host, keys) stay in the environment.

More articles in this chapter