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

API › Postman: structure and variables

Collection structure: folders, inheritance, documentation

A collection is a tree: authorization and shared scripts are set at collection or folder level, and requests inherit them from the parent. Folders have no variables. The authorization scheme is then changed in one place, and an exception such as GET /health gets No Auth on the request. Requests are named as action, resource and expectation, and saved examples and Markdown descriptions become documentation.

API · Postman: structure and variables

A collection of 80 requests with the token set in each one. How do you do it once?

A collection of 80 requests laid out "as it happened" becomes unmaintainable within a month. The token is typed into every request, so when the authorization scheme changes there are eighty places to edit. Names like "Request 37" say nothing in a Runner report. Collection structure is not aesthetics: it decides how many times you must configure authorization, variables and scripts and how easy the result is to read.

How it works and why it is designed so

The collection, a folder and a request form a tree, and settings travel downwards. Authorization and scripts are set at the collection or folder level, variables at the collection level, and a request takes them from its "parent". For authorization this is the Inherit auth from parent mode, the default for a new request. To make one request (say GET /health) go without a token, choose No Auth in it and the rest does not change. Scripts run in the order collection, folder, request, so shared logic (an environment check, a token refresh, a general response-time check) is written once at the top.

Inheritance in a collection

Collection authorization, variables, scripts inherits Transfers folder its own auth and scripts if needed inherits POST /transfers Inherit auth from parent
Figure 1. A setting is made once at the top and the request takes it from its parent.

Two kinds of folders. There is no single right scheme: these are two tools for different jobs, and a mature collection has both.

A resource folder or a scenario folder

By resource

Accounts, Transfers

CRUD and negative checks of one resource. Handy for exploring the API and for documentation.

By scenario

Transfer: start to finish

A chain with data, an action, a check and cleanup. Runs as a whole folder in the Runner or Newman.

Figure 2. Both are legitimate: the choice depends on what you do, exploring the API or running regression.

Example: a structure that reads well

Request names follow "action + resource + expectation": a Runner report shows at once what failed and you do not need to open the request.

Bank API                     <- auth, variables, shared scripts
  Auth
  Accounts                    <- a resource folder
    GET /accounts - 200 list
    GET /accounts/{id} - 404 foreign id
  Transfers
    POST /transfers - 201 created
    POST /transfers - 422 amount 0
  Scenarios
    Transfer: from creation to debit
      1 create an account
      2 POST /transfers
      3 check the balance
      4 clean up the data
// Pre-request at the COLLECTION level: runs before every request
// (then the folder script, then the request's own)
if (!pm.environment.get('baseUrl')) {
  throw new Error('No environment selected: baseUrl is empty');
}

Response examples. A saved "request and response" pair documents the expected behaviour and is the basis for a mock server. Negative responses (400, 404, 422) are worth saving as examples too. Documentation. A Markdown description exists on the collection, folder and request: purpose, required fields, error codes. Postman builds the Documentation page from it, which can be published for a team or partners. Teamwork. A team works in a shared workspace; changes are proposed through a collection fork and a pull request. For review in git the collection is exported to JSON (format v2.1) and stored in the repository next to the code; the newer Postman format with separate YAML files per request reads better in a diff.

Limits and alternatives

Inheritance eases maintenance but hides where behaviour comes from: a request "just fails with 401" while the cause is a collection script or the folder's authorization mode. When something is unclear, check the Authorization tab and scripts at all three levels. An overly nested structure (a folder in a folder in a folder) hurts too: two or three levels is usually enough. For a large regression set with logic, data factories and review in git, code (Playwright request and similar) is more convenient: see the lesson on Postman's limits.

Common mistakes

  • Authorization in every request. A scheme change means edits in dozens of places. Set it on the collection;
  • Nameless requests. "New Request" and "Copy of …" say nothing in a report;
  • A hidden folder script. A request behaves oddly because a higher-level script ran before it;
  • Negative cases without an example. The documentation shows only success and nobody knows what happens on 422;
  • A scenario without cleanup. The folder creates data and leaves it, so the second run hits a 409.

Check yourself

All requests of the collection go with a token, but GET /health must go without it. How do you set that up without copying the collection?

Answer

Authorization is set on the collection and in the /health request you choose No Auth instead of Inherit auth from parent. The other requests stay on the inherited token.

When do you use a resource folder and when a scenario folder?

Answer

A resource folder for CRUD and negative checks of one resource and for documentation. A scenario folder for an end-to-end chain (create, act, check, clean up) that is run as a whole folder in the Runner or Newman.

In an interview. Junior: I keep authorization and shared scripts at the collection level, make folders by resource, and name requests as "action, resource, expectation". Every request has a response example and a description. Middle/Senior: I move scenarios into separate folders for the Runner and Newman and keep resource folders for exploration and documentation. For odd behaviour I check inheritance and scripts at all three levels, and for review I keep the collection in the repository.

More articles in this chapter