skip to content

Auth Helpers

A declared block that makes the sending program build the credential itself, instead of a header you typed or a script that sets one. Interviewers probe where it is declared and which one wins.

part ofAPI & DB clientsoverview, primer and where to startread it →
on this pageshow

explore

questions

12

In a saved Postman collection, where may an `auth` block be declared, and which declaration signs a request?

level: juniorimportance: must knowfreq 70%

answer

  1. Count the places a block may sit
  2. Root, folder, request, and nothing else
  3. The declaration nearest the request wins
  4. getAuth checks the request before any parent

basics

~20 s

The collection format allows an auth block in three places: the collection document itself, a folder, and a single request. Resolution is nearest-wins, so a request's own block beats a folder's, and a folder's beats the collection's.

solid answer

~40 s

The collection **format** declares `auth` in exactly three places: on the collection document, on a folder (an `item-group`), and on a `request`. Nothing else in the file may carry one. When a request is sent, the SDK's `Item.getAuth()` looks at the request's own `auth` first and, failing that, calls `findInParents('auth', ...)` — a walk up the parent chain that returns the first ancestor declaring a usable block. The result is nearest-wins: request, then the innermost enclosing folder, then each outer folder, then the collection. If no site in the chain declares one, `getAuth()` returns `undefined` and the runtime skips its authorization step. The SDK deliberately refuses to build an empty auth object for a falsy value — the source comment says it does that "to allow inheritance from parent".

code

json · 18 lines
json
{
  "auth": { "type": "basic", "basic": [{ "key": "username", "value": "svc" }] },
  "item": [
    {
      "name": "public",
      "auth": { "type": "noauth" },
      "item": [
        { "name": "health", "request": { "method": "GET", "url": "https://example.com/health" } }
      ]
    },
    {
      "name": "invoices",
      "item": [
        { "name": "list", "request": { "method": "GET", "url": "https://example.com/invoices" } }
      ]
    }
  ]
}

go deeper

for a junior

Be ready to name the three declaration sites without hesitating: the collection document, a folder, and a single request. Then state the selection rule in one line — the declaration nearest the request wins.

for a middle

Be ready to explain the mechanics: the request's own block is extracted first, and only a miss triggers the walk up the parent chain, which returns the first ancestor with a usable block and stops there.

for a senior

Be ready to reason about a real file. Explain why a request that authenticates correctly can break when it is dragged into a different folder, and how you would confirm which site is actually supplying credentials.

for a principal

Be ready to argue where credentials should live in a collection many people edit. Weigh one root declaration against per-folder blocks, and say which arrangement makes an accidental change visible in review.

## The three places a credential block may sit A Postman collection is a JSON document. The **collection format** — the published schema, not the desktop app — decides which objects in that document may carry an `auth` member, and it allows exactly three: | Declaration site | The object it sits on | Typical use | | --- | --- | --- | | The collection | the top-level document | one scheme for every request in the file | | A folder | an `item-group` object | a subtree that talks to a different service | | A request | the `request` object | one endpoint that signs differently | At each of those sites the format writes `auth` as `oneOf` a `null` value or an auth object, and an auth object's only required member is `type`. Nothing else in the document may declare one: not a saved example, not a variable entry, not an event, not a header. That closed list is what makes the file tractable — when you want to know how a request signs, there are three kinds of place to look and no fourth. ## How a request finds the block that applies to it The **SDK** resolves this, not the format. `Item.getAuth()` runs a two-step lookup: 1. It extracts the `auth` from the item's own `request`. A block counts only if its `type` is a usable name; an absent block, or one whose `type` is `null`, does not count. 2. If the first step yields nothing, it calls `findInParents('auth', ...)`, which walks the parent chain outward and returns the `auth` of the **first** ancestor whose block passes the same test. The chain runs request, then the innermost enclosing folder, then each outer folder in turn, then the collection document. The walk stops the moment it finds a usable block, so the result is **nearest-wins**: - a request's own block beats every folder and the root; - an inner folder's block beats an outer folder's; - any folder's block beats the collection's; - if nothing in the chain declares one, `getAuth()` returns `undefined`. Two things the lookup does **not** do deserve stating plainly, because both are common assumptions. It does not **merge**: attributes are never combined across two levels, one block is chosen whole and the rest are ignored. And it does not apply a **default**: there is no implicit scheme filling the gap when every site is silent. An unresolved lookup simply means the runtime skips its authorization step and the request goes out as written. ## The empty-block rule The SDK deliberately refuses to build an auth object for a falsy value. Both the request and the folder constructors guard that assignment with the same comment — an empty auth "should not be created for falsy values **to allow inheritance from parent**". That one line is the whole design. If a falsy `auth` produced an empty object, the object would satisfy the lookup and silently sever the request from its ancestors' credentials. Because it produces nothing at all, a request that writes `"auth": null` stays transparent and the parent's block still applies. ## Why files that lean on inheritance read oddly The practical consequence is that **a request inheriting credentials and a request sending none look identical at the request**. Both carry no usable `auth` member. The difference lives entirely in the enclosing folders and the root, about which the request itself gives no hint. It is also why moving a request between folders can change how it authenticates with no edit to the request at all: position in the tree *is* the binding. When you inherit a collection you did not write, read outward rather than inward: 1. Look at the request's `auth`. If its `type` is a real name, you are done. 2. Look at the enclosing folder, then each folder above it, in order. 3. Look at the collection's top-level `auth`. 4. The first usable block on that path is the one that signs. Reach the top without one and nothing signs. ## Setting the block from code The SDK exposes the same three sites as methods. `Request.authorizeUsing(type, options)` installs a block on a single request, and `ItemGroup.authorizeRequestsUsing` is literally that same function bound onto a folder or a collection — so the identical call sets the block at whichever level you happen to hold. Passing `null` as the type is a special case: it deletes the `auth` property outright rather than storing an empty one, which restores inheritance for everything underneath. That symmetry is a useful mental model for the whole leaf — three sites, one selection rule, and a deliberate way to say "declare nothing here".

  • If a folder and the collection both declare a block, are their attributes combined for a request in that folder?
    No. The lookup selects one block whole and ignores the others; there is no merging of attributes across levels. The folder sits nearer the request, so the folder's block is returned and the collection's is never consulted at all. A folder declaring only a username therefore does not borrow a password from the root.
  • Can a saved example or an environment file carry the auth block instead?
    No. The format defines `auth` only on the collection document, on an `item-group` and on a `request`. A saved example records a past reply and carries no auth member, and an environment file is a separate document of variable values that resolution never reads. Neither participates in the walk.

saying these in an interview costs you the question

  • Says auth can only be set per request, never on a folder
  • Thinks a folder's block is merged with the collection's rather than replacing it
  • Claims the outermost declaration wins because it is the default
  • Believes a request with no auth block is rejected before it is sent
  • Assumes a saved example or a variable entry can carry the credential block
open as a page

In a saved Postman collection, how does the `auth` object name a scheme and where do its settings live?

level: juniorimportance: must knowfreq 68%

basics

~10 s

The auth object's required type field names the scheme, and that scheme's settings sit in a sibling array with the same name, holding auth attributes. Only key is required on each attribute.

open as a page

How does the Postman SDK's `Item.getAuth()` choose a credential block when several ancestors declare one?

level: middleimportance: must knowfreq 52%

basics

~20 s

Item.getAuth() extracts the request's own auth first, then calls findInParents to walk the parent chain outward. It returns the first ancestor declaring a block whose type is a usable name, and undefined if none does.

open as a page

In a Postman collection, how does a token a pre-request script just fetched reach the oauth2 accessToken attribute?

level: middleimportance: must knowfreq 62%

basics

~20 s

A pre-request script stores the fetched token in a variable scope; the collection's oauth2 accessToken attribute holds a brace token naming it. The runtime resolves auth variables before the handler signs, so signing sees the real value.

open as a page

What happens when a Postman collection's `auth.type` names a scheme no signing handler is registered for?

level: seniorimportance: must knowfreq 52%

basics

~20 s

The request goes out unsigned. The SDK stores almost any scheme name, the runtime's handler lookup misses, a console warning naming the type is triggered, and the run continues as if no credential had been configured.

open as a page

In a Postman collection, why put a brace token in the oauth2 accessToken attribute instead of pasting a credential?

level: juniorimportance: should knowfreq 50%

basics

~20 s

A brace token binds late: the exported file carries only a variable name, and whatever a pre-request script most recently stored under it signs the next request. A pasted credential is frozen into the file and goes stale.

open as a page

In a Postman collection, how does an `auth` type of `noauth` differ from a `null` type during inheritance?

level: middleimportance: should knowfreq 42%

basics

~20 s

The noauth type is a real string, so it satisfies the lookup, stops the parent walk, and leaves the request unsigned. A null type fails the validity check, so the walk continues and an ancestor's credentials still apply.

open as a page

In a Postman collection, what does hanging the token-fetch pre-request script on the collection cost?

level: middleimportance: should knowfreq 55%

basics

~20 s

A prerequest event declared on the collection runs before every request in the run, so a token-fetch script there makes one extra call per request. Moving it to a folder, or caching the token in a variable, removes that.

open as a page

Which authentication schemes does the `auth.type` enum in the Postman collection format declare?

level: middleimportance: should knowfreq 45%

basics

~10 s

The collection format's auth.type enum declares eleven values: apikey, awsv4, basic, bearer, digest, edgegrid, hawk, noauth, oauth1, oauth2 and ntlm. Every one except noauth also has a matching attribute array.

open as a page

A request under an authenticated Postman collection is going out unsigned — how would you trace the cause?

level: seniorimportance: should knowfreq 36%

basics

~20 s

Reconstruct the lookup by hand: read the request's auth, then each enclosing folder outward, then the collection root. The first block whose type is a real string wins, and a noauth block on that path stops the search.

open as a page

Your Postman collection calls the token endpoint once per request in a run. How do you fix that?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Find the prerequest event that fetches the token; on the collection it is gathered for every item. Move it down to the folder that holds the authenticated requests, or guard it with a cached token and a stored expiry.

open as a page

Why does a Postman collection with an `auth.type` of `jwt` run fine yet fail schema validation?

level: seniorimportance: should knowfreq 34%

basics

~10 s

Declaration and implementation are separate authorities. The collection format's auth.type enum declares eleven schemes; a runner's authorizer registers thirteen signing handlers, including jwt and asap, which the format never names.

open as a page