Testing a REST API means checking more than "does it return 200". A good test suite confirms that the API returns the right status codes, data shapes and errors, handles bad input gracefully, and enforces authentication. This guide covers what to test, how to structure requests, and a workflow that scales from a quick manual check to automated tests in CI.
201 Created for a successful POST, 404 for a missing resource, 401 when unauthenticated, 422 or 400 for invalid input. A wrong status code breaks clients and monitoring even when the body looks right.null values, and pagination boundaries.429.Don't hard-code base URLs and tokens into every request. Use variables, such as {{base_url}}/api/users, and keep a separate set of values for local, staging and production. Most API tools support environments, and test frameworks read them from configuration or environment variables. This also keeps secrets out of version control.
Each test should create the data it needs and not depend on another test running first. Tests that share state fail in confusing ways when they run in a different order or in parallel.
javascriptexpect(res.status).toBe(201); expect(res.body.id).toMatch(/^[0-9a-f-]{36}$/); expect(res.body.email).toBe('user1@example.com'); expect(res.body).not.toHaveProperty('passwordHash');
Assert on the fields that matter rather than comparing the whole response to a saved copy. Snapshot comparisons break every time a harmless field is added.
X-API-Key. Avoid query parameters, which end up in logs.Authorization: Bearer <token>. Fetch the token once per test run, not per request. To check a JWT's claims, decode it; see JWTs explained.curl or an API client and read the responses in the JSON Formatter to understand the shape.fetch or Supertest, pytest with requests, or REST Assured. Run them in CI on every pull request.WWW-Authenticate or in a JSON body you didn't print.Content-Type: application/json is set. A surprising number of 400 errors come from a missing header or a trailing comma; see Validate JSON.curl but fails in the browser, see CORS errors explained.