Skip to content
TestLine Menu
TestLine
API

How to test a REST API: 15 negative scenarios worth having

A list of 15 negative scenarios with examples and expected response codes, what to check in every error response and how to write such a test in Postman.

author
Artur Machowski, Senior QA Engineer
published
reading
7 min

Short answer

Negative tests check that an API correctly rejects bad requests: missing fields, wrong format, a missing or expired token, access to someone else’s resource, a duplicate, a repeated request and an exceeded rate limit. The 15 scenarios below cover most of the bugs that come up in API testing. In each one you check three things: the response code (400, 401, 403, 404, 409, 422 or 429), a clear error message and that nothing changed in the data.

15 negative scenarios

The examples use a POST /orders endpoint that creates an order.

Data validation

#scenarioexampleexpected result
1missing required fieldrequest without productId400 or 422, field name in the message
2wrong data type"quantity": "two"400 or 422
3boundary valuesquantity equal to 0, -1 and above the limit422 for out-of-range values, 201 for allowed boundaries
4long text and special characters10,000 characters, emoji, non-ASCII letters in the name422 or saved without truncation or changed characters
5wrong formatemail without @, date 2026-02-30422
6broken JSON or wrong headercut-off bracket, Content-Type: text/plain400 or 415

Access

#scenarioexampleexpected result
7no tokenrequest without the Authorization header401
8expired or altered tokenan hour-old token, one character changed401
9someone else’s resourceGET /orders/{id} of another user’s order403 or 404, no order data
10role without permissionsa regular customer calls an admin endpoint403

State and logic

#scenarioexampleexpected result
11non-existent resourceGET /orders/999999404
12duplicatesigning up a second time with the same email409
13repeated requestthe same POST /payments twice with the same idempotency keyone payment, second response the same as the first
14forbidden state changecancelling an order that has already shipped409 or 422
15rate limit100 requests per second from one account429 and a Retry-After header

Scenarios 7-10 are a basic access check as part of functional testing. They don’t replace security testing or penetration testing.

What to check in every error response

  • Response code in line with the API contract.
  • Error structure the same across the whole API, e.g. in the Problem Details format (RFC 9457): type, title, detail, list of invalid fields.
  • No technical details: no stack trace, SQL queries or server names.
  • No data changes: after a rejected POST, a following GET doesn’t show a new order.
  • No side effects: no email went out, no stock was reserved, no card was charged.

The last two points are skipped most often, and they are the ones that protect against the most expensive bugs. I cover retried payment notifications in the guide testing webhooks and payments.

A test example in Postman

A test for scenario 1, in the request’s Tests tab:

pm.test("422 when productId is missing", () => {
  pm.response.to.have.status(422);
  const body = pm.response.json();
  pm.expect(JSON.stringify(body)).to.include("productId");
});

pm.test("no stack trace in the response", () => {
  pm.expect(pm.response.text()).to.not.include("at com.");
});

You can run the collection in CI with Newman. I cover the basics of requests, response codes and Postman in API testing basics. When to choose Postman and when RestAssured is covered in Postman vs RestAssured.

Consistency with the contract

Whether a validation error is 400 or 422 depends on the team’s convention. What matters is that there is one, and that it’s described in the contract, e.g. in OpenAPI. Then negative tests check compliance with the contract, and a change to the error format in one service won’t silently break the mobile app. How to keep the contract between services in check is covered in API contract testing.

How I can help

As part of API testing I prepare a suite of 50-80 cases in Postman or RestAssured, with these negative scenarios for your endpoints and CI runs. It usually costs PLN 10,000-15,000 net and takes 2-3 weeks. Describe your API and the documentation you have in the contact form.

Frequently asked questions

What are negative API tests?

Tests that send bad or forbidden requests to the API: missing fields, wrong format, no token, someone else's resource, a duplicate. They check that the API rejects them with the right code, a clear message and no change to the data.

How many negative scenarios should an endpoint have?

An endpoint that writes data usually needs 5-10: missing fields, wrong type, boundary values, no token, someone else's resource, duplicate. A read-only endpoint needs fewer, usually authorisation and a non-existent resource.

400 or 422 for a validation error?

Both are common. What matters more than the choice is that the whole API uses one convention described in the contract, e.g. in OpenAPI. The test checks compliance with the contract, not the author's taste.

Are API authorisation tests security tests?

A basic check that you can't fetch data without a token or with someone else's ID is part of normal functional testing. It doesn't replace penetration testing done by a security specialist.

What should I write negative API tests in?

To start with, Postman with JavaScript tests run through Newman in CI is enough. In Java projects RestAssured works well, in JavaScript projects API tests in Playwright.

Got a release in a few weeks?

Describe your project in the form. I'll reply with questions about scope or an initial test plan.