Secrets, mock servers and monitors in Postman
Secrets do not belong in a Shared value, because it is synced to the team and exported: secure variables, Local values and Postman Vault exist for them. A mock server answers with saved examples but does not prove the real server works. A cloud monitor sees only Shared values and, without Private API Monitoring, cannot reach localhost or a VPN; its requests must be safe and repeatable.
API · Postman: structure and variables
The collection is green locally but the cloud monitor gets 401. What can it not see?
A collection passed locally and gets switched on as a monitor. The very first run sends an email that everything failed with 401. Nothing changed in the backend; one thing did: the monitor runs in the Postman cloud and does not see what lies on your laptop. This lesson is about three tools around a collection: secrets, a mock server and a monitor. Each has its limits, and the limits are what gets asked in interviews.
How it works and why it is designed so
Secrets. A password, API key or token is not for a Shared value: it is synced to the team, goes into the environment export and can end up in git. There are several levels. A secure variable is stored encrypted and masked in the interface. A value can be kept in the Local value, which stays on the machine. The Postman Vault stores secrets encrypted, they are referenced as {{vault:name}} and read in scripts through await pm.vault.get('name') (the method is asynchronous; the pm.vault methods do not work in Newman, the Postman CLI or monitors). In CI secrets come from the pipeline store and are passed to Newman through --env-var. If a secret reached a Shared value or a repository, it is treated as compromised: the key is reissued, not just deleted from the variable.
A mock server is created from a collection and answers with saved examples: Postman picks an example by method and path (the body only raises the match score; strict body and header matching is turned on in the mock settings or with the x-mock-match-request-body and x-mock-match-request-headers headers). It is needed when the backend is not ready: the frontend, a mobile client or consumer tests work against the mock while the contract is already fixed in OpenAPI or the collection.
A monitor runs a collection on a schedule in the Postman cloud from selected regions and sends a notification when tests fail. It watches availability and the contract of staging or production, and does not check every commit.
The same request locally and in a monitor
Local run
Shared and Local values exist: the token is substituted, the response is 200.
Cloud monitor
Only Shared values exist: there is no token, the response is 401.
Example: a mock and a monitor in practice
A specific mock example can be chosen with the x-mock-response-name or x-mock-response-code header. That is handy for negative scenarios that are hard to reproduce on a real backend:
POST {{mockUrl}}/transfers
Content-Type: application/json
# pick a saved example by its name
x-mock-response-name: Insufficient funds
{ "amount": 999999, "currency": "UAH" }
A request in a monitor must be safe and repeatable: it runs constantly, so it must not create garbage or move money. A typical health check:
// Post-response for a request in a monitor: a safe, repeatable check
pm.test('Service health', () => {
pm.response.to.have.status(200);
pm.expect(pm.response.responseTime).to.be.below(1500);
});
Limits and alternatives
- A mock from a collection has no logic: it returns only what is saved (code-based mocks in JavaScript can have logic and state). No mock proves the real server works, so the contract should be checked separately;
- Only Shared values apply in the cloud; Local values and the Vault from your computer do not get there. That is why everything is green locally and the monitor gets 401;
- A cloud monitor does not see
localhostor a network behind a VPN. For internal APIs the collection is run on a schedule through Newman or the Postman CLI in your own CI (check what your plan allows); - The per-commit pipeline runs through Newman in CI and the monitor complements it: it notices that an endpoint broke without any deploy (a certificate, a dependent service, configuration).
Common mistakes
- A secret in a Shared value. Everyone sees it and it goes into the export;
- A monitor with a request that creates data. Garbage piles up each night or it fails with 409;
- Trusting a mock as proof. The frontend is green on the mock while the real backend returns another field;
- A monitor for an internal API. The cloud cannot see it and the failure alerts are false;
- A mock without negative examples. The frontend cannot check 4xx and 5xx errors.
Check yourself
The collection is green locally while the cloud monitor gets 401. What is the most likely cause?
Answer
The token lives in a Local value or the local Vault while the cloud sees only Shared values (and, on Enterprise, Shared Vault secrets linked to an external vault). The fix: obtain the token with a separate auth request in a pre-request, using a monitor-only test account whose credentials come through a protected mechanism the monitor supports, and do not put the real key into a Shared value.
When is a mock server useful and what does it not prove?
Answer
When the backend is not ready and the frontend or consumer tests must work against an already fixed contract. It does not prove the real server works and returns the same: that needs contract checks against the real API.