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

API › Postman: structure and variables

Variable scopes and secrets

A Postman variable lives in a scope (Global, Collection, Environment, Data, Local), and with equal names the narrowest wins. Secrets are not put in a Shared value, because it syncs with the team and reaches exports and git: they are kept in a Local value or Postman Vault, and in CI come from the pipeline's secret store. A secret that reached a Shared value or a repository is treated as compromised.

API · Postman: structure and variables

A token sits in an environment variable and a teammate sees it in their Postman. Where is it stored?

There are two stories about variables. In the first, a same-named baseUrl exists in both the collection and the environment, and the request goes not where it was expected. In the second, a tester pasted a token into an environment variable, shared the environment with the team, and the token landed with everyone in the workspace and then in an export. Both stories are about the same thing: a variable lives in a particular scope with a particular visibility. This lesson lays out the scopes by priority and by who sees the value.

How it works and why it is designed so

Scopes go from wide to narrow. If names match, the narrow one wins: a shared value can be overridden precisely without touching the rest.

ScopeWhere it appliesTypical useIn a script
GlobalThe whole workspaceRarely; better avoidedpm.globals
CollectionOne collectionAPI version, constantspm.collectionVariables
EnvironmentThe active environmentbaseUrl, test loginspm.environment
DataOne Runner iterationA CSV or JSON rowpm.iterationData
LocalOne request run or a collection runTemporary valuespm.variables

Scripts read a variable in two ways: pm.variables.get returns the value from the scope that won, while pm.environment.get or pm.collectionVariables.get look only in their own scope. So the same code in two places can give different values if the variable is also set in a narrower scope.

Shared and Local value. A variable in an environment has two values. The Local value lives only on your machine: it is the one substituted into requests and it is not synced to the Postman Cloud. The Shared value is synced with the team when you share it. In older Postman versions these were called initial and current value, so you meet those names in older materials.

Where the token lives

Risky

Token in the Shared value

It is synced to the team, goes into the environment export and can end up in git.

Better

Token in the Local value or Vault

It stays on your machine: it does not reach the shared workspace or an export.

Figure 1. The same value in different places: the difference is who can see it.

Example: who wins, and where the secret lives

One name in three scopes. The request takes the value from the narrowest one present:

collection   baseUrl = https://a.example.test
environment  baseUrl = https://b.example.test
data row     baseUrl = https://c.example.test

with a data file    -> https://c.example.test
without a data file -> https://b.example.test
without environment -> https://a.example.test
pm.variables.get('baseUrl');            // the winner of all scopes
pm.environment.get('baseUrl');          // the environment only
pm.collectionVariables.get('baseUrl');  // the collection only

Secrets have three levels of protection, from simple to solid. A secure variable is stored encrypted and masked in the interface. Postman Vault keeps secrets encrypted (the local vault stays on your machine, the shared vault lives in the Postman Cloud), and they are referenced as {{vault:name}} or read in a script asynchronously through await pm.vault.get('name'). In CI the secret comes from the pipeline store and is passed on the command line:

# In CI the secret comes from the pipeline store, not from a file
newman run api.postman_collection.json \
  -e stage.postman_environment.json \
  --env-var "password=$API_PASSWORD"

Limits and alternatives

The Vault and your laptop's Local value are unavailable to CI and to a cloud monitor: only values passed explicitly apply there (details in the lesson on secrets, mock servers and monitors). The Global scope looks convenient, but a variable without a collection is available to every collection in the workspace and quickly becomes a source of odd name clashes: keep values in an environment or a collection. If a secret does reach a Shared value or a repository, treat it as compromised and reissue it.

Common mistakes

  • A token or password in a Shared value. Everyone with access to the workspace sees it, it goes into an export and can reach git;
  • Same-named variables in several scopes. Someone edits the collection while the request takes the value from the environment. Give variables unique names or check which scope wins;
  • Global for everything. A value becomes shared by all collections and lingers from old projects;
  • Deleted a secret from the variable and relaxed. It stayed in history or an export: the key must be reissued.

Check yourself

baseUrl exists in the collection, in the active environment and in a CSV row in the Runner. Which value goes into the request and why?

Answer

The CSV row one: the data scope is narrower than environment, and environment is narrower than collection. Without a data file the environment would win.

Why is a token not put into the Shared value?

Answer

The Shared value is synced to the team, goes into the environment export and can end up in git. A token is kept in the Local value or the Vault, and in CI it comes from the secret store.

In an interview. Junior: Variable scopes are Global, Collection, Environment, Data and Local; for the same names the narrower wins. I keep secrets only in a Local value or the Postman Vault so they do not reach a shared workspace or an export. Middle/Senior: For predictable behaviour I give variables unique names and choose the scope by lifecycle: the stand in the environment, a constant in the collection, a case row in data. A secret that reached a Shared value or git I treat as compromised and reissue, and in CI I pass it from the pipeline store.

More articles in this chapter