REST API return codes—also called HTTP status codes—are three-digit numbers that tell a client whether a request succeeded, failed, or needs further action. The most common codes—200, 201, 400, 401, 403, 404, and 500—cover most real-world scenarios, but many developers misuse 500 for client errors or use 200 even when data isn’t returned. The correct practice is to match the status code precisely to the request’s outcome, not to the presence of data.

These codes originate from the HTTP/1.1 standard (RFC 2616, 1999) and are maintained in RFC 7231 and later updates. They are not invented per API; they are standardized across the web. Every HTTP response must include one, and clients (browsers, mobile apps, scripts) rely on them to decide what to do next—like retrying, prompting for login, or showing an error message.

Group Codes by Their Meaning, Not by Number

HTTP status codes fall into five categories, each starting with a digit that tells you the outcome type. Use this grouping to decide quickly which code fits your situation.

1xx: Informational (Rarely Used in Practice)

These indicate the request was received and the server is continuing to process it. You’ll see 100 Continue in large uploads where the client waits for permission before sending the body, but most APIs skip 1xx codes entirely. Mobile and web clients often ignore them, so you can safely ignore them unless you’re building a very large file upload endpoint.

2xx: Success—Data Was Handled Correctly

Use these when the server processed the request as intended. 200 OK is for GET, PUT, or PATCH requests where the response body contains the requested or updated data. 201 Created is for POST requests that result in a new resource; it must include a Location header pointing to the new resource’s URL. 204 No Content means the action succeeded but there’s no data to return—common for DELETE requests or idempotent updates. Do not use 200 when the resource wasn’t found or modified; that misleads clients into thinking they have valid data.

3xx: Redirection (Use Sparingly)

These tell the client to look elsewhere. 301 Moved Permanently indicates a permanent move and prompts clients to update saved URLs. 302 Found (or 307 Temporary Redirect) is for temporary moves. Most APIs avoid redirection because clients may not follow it automatically—especially in server-to-server calls. If you need to version your API, use a header or path segment instead of 301/302 redirects.

4xx: Client Errors—The Problem Is on the Request Side

These mean the request was invalid or unauthorized. 400 Bad Request covers malformed JSON, missing required fields, or out-of-range values. 401 Unauthorized means no valid credentials were sent; the client must log in or send an access token. 403 Forbidden means credentials were valid, but the user lacks permission for that action—like a regular user trying to delete another user’s account. 404 Not Found is for resources that don’t exist, such as a user ID that’s never been created. 422 Unprocessable Entity is for syntax-correct but semantically invalid requests—like sending a negative amount in a payment API. Avoid 500 for any of these; it makes debugging harder and triggers false alerts on the server side.

5xx: Server Errors—The Problem Is on Your Side

These mean something went wrong in your code or infrastructure. 500 Internal Server Error is the catch-all for unhandled exceptions or logic failures. 502 Bad Gateway means your server received an invalid response from an upstream service (like a database or third-party API). 503 Service Unavailable means your server is temporarily overloaded or down for maintenance. 504 Gateway Timeout means the upstream service didn’t respond in time. Log the full error on the server, but never return the error’s stack trace or internal message in the response—only use generic text like "Internal server error" to avoid leaking sensitive details.

Choose the Right Code by Following This Decision Tree

When writing a new endpoint, walk through these questions in order to pick the correct status code. This avoids guesswork and keeps your API predictable.

  1. Did the request reach the server and parse correctly? If not, use 400 Bad Request.
  2. Does the user need to authenticate? If credentials are missing or invalid, use 401 Unauthorized.
  3. Are credentials valid but the user lacks permission? Use 403 Forbidden.
  4. Is the requested resource missing? Use 404 Not Found.
  5. Was the data syntactically correct but semantically wrong (e.g., invalid enum value)? Use 422 Unprocessable Entity.
  6. Did the server fail to process the request due to its own fault? Use 500, 502, 503, or 504 as appropriate.
  7. Did the request succeed and return data? Use 200 OK.
  8. Did the request create a new resource? Use 201 Created with a Location header.
  9. Did the request succeed but return no content? Use 204 No Content.

Common Misuses—and How to Fix Them

These mistakes appear in many public APIs, even from experienced teams. Fixing them improves reliability and reduces support tickets.

Misusing 200 for Empty Responses

Returning 200 OK with an empty body or {"data": null} for missing resources confuses clients. They assume data is valid and try to use it, leading to runtime errors. Instead, use 404 Not Found when the resource doesn’t exist. If the request succeeded but there’s nothing to return (like after a successful DELETE), use 204 No Content and omit the body entirely.

Using 500 for Client Mistakes

Throwing a 500 Internal Server Error when a user submits invalid data makes the client think it’s a server problem, not their own. This triggers unnecessary alerting in monitoring systems and delays fixes. Replace it with the correct 4xx code: 400 for bad syntax, 422 for bad semantics, 401 for missing auth, and 403 for access denial.

Omitting Location Headers on 201

After creating a resource with POST, always include the Location header pointing to the new resource’s URL. Clients use this to fetch or update the resource immediately. Without it, they must reconstruct the URL or make a second request, which is inefficient and error-prone. For example, POST /users returns 201 with Location: /users/12345.

Testing Your Status Codes

Before launching an API, verify each endpoint returns the correct status codes under different conditions. Use tools like curl or Postman to send requests and inspect responses.

  1. Send a GET for an existing ID—expect 200 OK.
  2. Send a GET for a nonexistent ID—expect 404 Not Found.
  3. Send a POST with missing required fields—expect 400 Bad Request or 422 Unprocessable Entity.
  4. Send a POST without an authorization header—expect 401 Unauthorized.
  5. Send a POST with valid auth but insufficient permissions—expect 403 Forbidden.
  6. Send a DELETE for an existing resource—expect 204 No Content.
  7. Trigger a server-side error (e.g., database unreachable)—expect 500 or 503 depending on the cause.

Automate these checks with unit or integration tests. In Node.js, use Jest to assert status codes; in Python, use pytest with requests. A test that checks "status_code == 200" for every scenario is incomplete—add specific assertions for 4xx and 5xx cases too.

Handling Errors in Your Client Code

When consuming a REST API, always check the status code before parsing the response body. Many libraries (like Axios or fetch) throw errors only for 4xx/5xx codes, but you must handle them explicitly.

  1. Check if status is 200, 201, or 204 before assuming success.
  2. For 401, redirect the user to log in or refresh the token.
  3. For 403, show a permission-restricted message—do not retry with the same credentials.
  4. For 404, display a "not found" message or fallback UI.
  5. For 5xx, retry with exponential backoff for transient errors (like 503), but fail fast for 500 if logs show no recovery.

Never assume a response is JSON if the status is not 2xx. A 503 might return HTML from a load balancer, and a 401 might return a login form. Always validate content-type and status together before parsing.

REST API Return Codes for Large Batches and Kids

What if your API handles batch operations—like importing 1,000 records—or is used by junior developers or non-technical users? These cases need special attention to status codes.

Batch Processing

For bulk operations, return 202 Accepted to indicate the request is queued for processing. Include a job ID and a status endpoint URL in the response. When the job finishes, clients check the status endpoint and see 200 OK with partial results, or 207 Multi-Status if individual items succeeded or failed differently. Do not return 200 OK immediately with partial data—that hides failures and makes rollback impossible.

Beginner-Friendly APIs

When non-experts use your API, add more specific 4xx codes. For example, 409 Conflict for duplicate entries (like trying to register an email already in use), or 429 Too Many Requests when rate limits are exceeded. Include human-readable error messages in JSON (like {"error": "Email already exists"}) to guide them. Avoid 500 unless a true server failure occurred—most mistakes are client-side and deserve a 4xx code.

Idempotency and Retries

Use 409 Conflict for idempotent requests where the same action is sent twice (e.g., submit the same payment twice). This lets clients know the first request already succeeded and no action is needed. For retries, 425 Too Early signals the request was sent before the server was ready, so clients can wait and retry. These help prevent duplicate charges or side effects during network hiccups.

Finally, remember: status codes are part of your API’s contract. Changing them after launch breaks clients. Document each endpoint’s possible codes in your OpenAPI spec, and test them rigorously before merging changes.

REST API Return Codes for Large Batches and Kids

Batch Processing

Beginner-Friendly APIs

Idempotency and Retries