Skip to main content
paulund

3 min read

#api#rest#http

4. Status Codes

HTTP status codes are the primary way your API communicates the outcome of a request. Use the standard codes consistently and resist the temptation to invent your own.

Success Codes (2xx)

CodeMeaningWhen to Use
200OKThe request succeeded and the response body contains the requested data.
201CreatedA new resource was created. Include a Location header pointing to the new resource.
204No ContentThe request succeeded but there is no body to return — common for DELETE and some PUT responses.

Client Error Codes (4xx)

CodeMeaningWhen to Use
400Bad RequestThe request is malformed or contains invalid syntax.
401UnauthorisedThe request lacks valid authentication credentials.
403ForbiddenThe credentials are valid, but the client does not have permission to perform the action.
404Not FoundThe requested resource does not exist.
409ConflictThe request conflicts with the current state of the resource (e.g. a duplicate unique key).
422Unprocessable EntityThe request is syntactically valid but fails semantic validation (e.g. business rule violations).
429Too Many RequestsThe client has exceeded the rate limit.

Server Error Codes (5xx)

CodeMeaningWhen to Use
500Internal Server ErrorSomething went wrong on the server. Log the full details internally; return only a generic message to the client.
503Service UnavailableThe server is temporarily unable to handle requests (e.g. during maintenance or high load).

Guidelines

  • Be precise. A 404 means the resource does not exist; a 403 means the client is not allowed to access it. Do not return 404 to hide the existence of a resource unless that is a deliberate security decision.
  • Stay consistent. Pick a small set of status codes and use them uniformly. Clients build logic around these codes; surprising them with unusual codes increases integration complexity.
  • Never invent codes. Stick to the codes defined in the HTTP specification. Custom codes outside the standard ranges cause widespread compatibility issues.

Related notes

  • API Caching

    How to use Cache-Control headers and ETags in your REST API to reduce bandwidth, lower server load, ...

  • API Consistency Rules

    Why a consistent API style matters and what your style guide must cover — from field casing and pagi...

  • API Deprecation Process

    A five-step process for deprecating REST API endpoints gracefully — from signalling deprecation in t...


Newsletter

A weekly newsletter on React, Next.js, AI-assisted development, and engineering. No spam, unsubscribe any time.