Testing APIs
Chaining requests and test data
Real flows take several requests — sign in, create, read, delete — each using values from the one before. And one test can run with many inputs. How chaining and data-driven testing work.
Real use of an API is rarely one request. A shopper signs in, finds a product, orders it, checks the order, maybe cancels. Each step needs something from an earlier one: the token from the sign-in, the id of the order just created. Passing values from one request to the next is called chaining.
Taking a value from an answer
The sign-in answers:
{ "data": { "token": "demo-8ed65df1c13…", "user": "ann", "expiresIn": 3600 } }
You tell the tool where the value is — the JSON path data.token (JSON) — and what to call it — token. From then on, {{token}} means that value, in any request after it.
Three ways to point at a value:
- a JSON path —
data.token,items[0].id— for JSON answers (most of the time); - a header — the
Locationheader after a create, for example; - a text pattern (a regular expression) — for anything else, like a value inside HTML or XML:
token=([a-z0-9]+)keeps the part in brackets.
Two directions
There are two ways to wire a chain, and they suit different needs:
- "Before me, run that" — a request says send the sign-in first, and take the token from it. Use it for setup that every test of this request needs. (In APITestCenter: Run first.)
- "After me, keep this" — a request says take the
idfrom my answer and keep it for whoever comes next. Use it for flows: create → read → update → delete. (In APITestCenter: Save values.)
A typical end-to-end flow, run as a collection in order:
| # | Request | Takes | Gives |
|---|---|---|---|
| 1 | POST /login | {{user}}, {{password}} | {{token}} |
| 2 | POST /orders | {{token}} | {{orderId}} |
| 3 | GET /orders/{{orderId}} | {{token}}, {{orderId}} | — checks the order is there |
| 4 | DELETE /orders/{{orderId}} | both | — cleans up |
| 5 | GET /orders/{{orderId}} | both | — checks it's gone (404) |
Step 4 matters: tests should clean up after themselves, or the test environment fills with junk orders and later tests trip over them.
Order matters
A chain only works in order. If Get order runs before Create order, {{orderId}} doesn't exist yet. Good tools tell you which request was supposed to produce it. When a step in the middle fails, the steps after it can't do anything useful — some tools stop the run there ("stop at the first failure").
Test data: one test, many inputs
A test with one input proves the API works for that input. Real APIs get thousands of different ones. Data-driven testing runs the same request with rows from a table — usually a CSV file:
email,password,expected
ann@example.com,correct-1,200
bob@example.com,wrong,401
,missing,400
very.long.name.that.goes.on.and.on@example.com,correct-2,200
Each column becomes a variable ({{email}}, {{password}}), and each row is one run. With an expected column, the same test checks the happy path and the failures. In a load test, test data makes the traffic realistic: a thousand users signing in as a thousand different accounts, not one account a thousand times (which a cache would answer instantly and tell you nothing).
Rows can be used in order, at random, or one per simulated user — the last is what you want for logins, so each user stays one person.
Where test data comes from
- Made up, with variety on purpose: long and short, accents and emoji (
José,Zoë), empty values, the biggest and smallest allowed numbers. - Generated fresh each time:
{{$uuid}},{{$randomEmail}}— so a sign-up test never collides with yesterday's. - Copied from production — careful: real customer data in a test system can be a legal problem. Mask or invent it.
Key takeaways
- Chaining passes values — tokens, ids — from one request's answer to the next, by JSON path, header or pattern.
- Run a flow in order, and clean up what tests create.
- Test data runs one test with many inputs — happy and unhappy — and makes load tests realistic; one row per user for logins.
In the app: Run first · Save values · Test data · A real flow: create, read, delete
Next: Contracts and schemas