Key takeaways
- An API is how programs talk to each other. Testing it directly checks the business logic without the UI, so tests are faster and break less often.
- Check more than the "happy path": status codes, response structure, validation, errors, authentication and authorisation, idempotency and performance.
- The most common serious API flaw is broken authorisation: one user can read or change another user's data by changing an ID. Always test it.
Where this picks up
You know integration testing and why the test pyramid puts most tests below the UI. API tests are a large part of that middle layer. They're also where a tester can find many bugs the screens hide.
How a web API call looks
Most web APIs today use HTTP and JSON, often in a style called REST. A call has:
| Part | Example |
|---|---|
| Method | GET (read), POST (create), PUT/PATCH (update), DELETE |
| URL | https://api.shop.example/orders/1001 |
| Headers | Authorization: Bearer <token>, Content-Type: application/json |
| Body (for POST/PUT) | {"productId": 42, "quantity": 2} |
The response has a status code, headers and usually a JSON body:
| Code | Meaning |
|---|---|
| 200 / 201 / 204 | OK / Created / OK with no content |
| 400 | Bad request: invalid input |
| 401 | Not authenticated: no or invalid login |
| 403 | Authenticated, but not allowed |
| 404 | Not found |
| 409 | Conflict, for example a duplicate |
| 422 | Valid format, but breaks a business rule (used by many APIs) |
| 429 | Too many requests |
| 500 | Server error, almost always a bug |
What to test
1. Functional correctness
- Does the response contain the right data, with the right values and types?
- Does a
POSTreally create the resource? Read it back with aGETto check. - Do filters, sorting and pagination work, including the last page and an empty result?
2. Contract and schema
Does the response match the documented structure (for example an OpenAPI specification)? Are required fields always present, and are types stable? A field that's sometimes a number and sometimes a string breaks clients.
3. Validation and errors
Apply the techniques you know to every field: partitions, boundaries, missing fields, wrong types, extra fields, very long strings, special characters.
Check that errors are useful and safe: the right status code (400 or 422, not 500), a clear message and no internal details like stack traces or SQL queries.
4. Authentication and authorisation
- Authentication: no token, an expired token, a malformed token →
401. - Authorisation: a valid token for user B, asking for user A's data →
403or404, never200. - Roles: a customer calling an admin-only endpoint → refused.
5. Behaviour under repetition and load
- Idempotency: sending the same
PUTorDELETEtwice shouldn't cause a different result. Sending the same payment twice, after a network timeout, shouldn't charge twice. - Concurrency: two updates to the same resource at once.
- Rate limits: too many requests →
429, not a crash. - Response times for typical and large requests.
Tools
- Exploring: Postman, Insomnia, Bruno or plain
curl. Good for learning an API and exploratory testing. - Automating: REST Assured (Java), pytest with requests (Python), Playwright's API testing or Supertest (JavaScript/TypeScript).
- Specification: an OpenAPI (Swagger) file describes every endpoint. Use it to generate requests and to check schemas.
The most common serious flaw
The OWASP API Security Top 10, a widely used list of the most critical API risks, puts Broken Object Level Authorization first. In plain words: the API checks that you're logged in, but not that the object you're asking for belongs to you.
In November 2018, security journalist Brian Krebs reported a flaw in a USPS web API used by its "Informed Visibility" service.
Any logged-in usps.com user could query the API for account details of other users: email addresses, usernames, account numbers, street addresses and phone numbers. Krebs reported that this exposed data on around 60 million accounts. The API even accepted wildcard searches.
A researcher said they had reported the problem a year earlier without a response. USPS fixed it after Krebs contacted them.
Lesson: the API checked that the user was logged in. It didn't check whether they were allowed to see the data they asked for. A tester with two test accounts and an API tool can check exactly that.
Structuring API tests
- Create test data through the API at the start of each test. Don't rely on data left by other tests.
- One behaviour per test: "create order with invalid quantity returns 422 and a clear message".
- Assert on status, body and side effects: was the order really saved? Was stock reduced?
- Clean up what the test created, or use isolated test accounts.
- Keep secrets out of the tests: tokens and passwords come from environment variables, never from the code.
Do
Keep two test users in every environment, and for every endpoint that returns user data, check that user B can't read or change user A's.
Don't
Test only with an admin account. Admins can see everything, so you'll never notice when normal users can too.
Check yourself
You log in as user B and call GET /orders/1001, which belongs to user A. The API returns 200 with A's order. What is this, and what should happen instead?
Show answer
It's broken object level authorisation, a serious security bug. The API should return 403 (forbidden) or 404 (to avoid revealing that the order exists). Report it as high severity, and check the same pattern on other endpoints that take IDs, such as invoices, addresses and payment methods.
