Testing APIs
Environments and variables
The same API usually runs in several places — development, test, staging, production. Variables and environments let one set of tests run against all of them, with secrets kept apart.
A company rarely has one copy of an API. It has several environments:
| Environment | Also called | Used for |
|---|---|---|
| Local | dev machine | A developer running it on their own computer |
| Development | DEV | The newest code, often broken |
| Test / QA | SIT, UAT | Testers and business users try it before release |
| Staging | pre-prod | A copy of production, for the last checks |
| Production | PROD, live | Real users and real data |
Each has its own address — http://localhost:3000, https://dev.api.example.com, https://uat.api.example.com, https://api.example.com — and often its own accounts, tokens and test data.
Writing every test four times, once per address, would be madness. Variables solve it.
Variables: names for values
A variable is a name that stands for a value. You write {{baseUrl}}/orders once; the tool fills in {{baseUrl}} when it sends the request.
An environment is a set of values for those names:
| Variable | Local | UAT | Production |
|---|---|---|---|
baseUrl | http://localhost:3000 | https://uat.api.example.com | https://api.example.com |
user | dev@example.com | tester@example.com | — |
token (secret) | dev-token | ••••••• | ••••••• |
Pick UAT, and every request goes to UAT with UAT's values. Pick Local, and the same tests run on your own computer. That's the whole idea: tests describe what to check; environments describe where.
Variables aren't only for addresses. Use them for anything that differs between environments or that you'd otherwise repeat: user names, ids of test records, API versions, page sizes.
Secrets belong in variables
Tokens, passwords and API keys should never be typed into the request itself. Put them in a variable marked secret:
- the request then says
Authorization: Bearer {{token}}— safe to save, share and commit to git; - the secret value stays on your computer (APITestCenter encrypts secret values and never writes them into your request files);
- in a pipeline, the secret comes from the pipeline's own secret store (
--var token=…).
Where values come from
A value can come from several places. When two give the same name a value, a rule decides which wins. In APITestCenter, from weakest to strongest: folder variables (defaults for the requests inside a folder) → the environment → values saved from answers (like a token from a login a moment ago) → values filled while a test runs (from a run-first request or a row of test data). On the command line, --var name=value sets or overrides any of them. The general idea, in any tool: the more specific and recent, the stronger.
Values that make themselves
Some values should be different every time — a new order reference, a unique email for a sign-up test, today's date. Built-in values do that: {{$uuid}} (a random unique id), {{$timestamp}}, {{$randomInt(1,100)}}, {{$randomEmail}}. Each request gets a fresh one — which matters in load tests, where a thousand sign-ups with the same email would mostly fail as duplicates.
Key takeaways
- An API runs in several environments (Local, DEV, UAT, staging, production), each with its own address and accounts.
- Variables (
{{baseUrl}}) let one test run anywhere; an environment is a set of values for them. - Keep secrets in secret variables, never in the request; use built-in values for values that must be new every time.
In the app: Environments · Which value wins · Built-in values