REST API response codes are three-digit numbers that tell you the result of an HTTP request. They follow standard groups: 2xx for success, 4xx for client errors, and 5xx for server errors.
Quick Answer: Core Meaning of Response Codes
The first digit of a REST API response code defines its class: 2 means success, 3 means redirection, 4 means client error, and 5 means server error. This structure lets you quickly understand what happened without reading the body.
Success Codes: 2xx Range
Codes in the 200s indicate the request was received, understood, and accepted. The most common is 200 OK, meaning the request succeeded and the response body contains the expected data. A 201 Created means a new resource was successfully made, often after a POST request, and the response includes a Location header pointing to the new resource. A 204 No Content means the server processed the request but returns no body, useful for DELETE or PUT actions where no data needs to be sent back.
Redirection Codes: 3xx Range
3xx codes mean the client must take additional action to complete the request. A 301 Moved Permanently tells the client the resource has a new permanent URL, and future requests should use that address. A 302 Found indicates a temporary move; the original URL should still be used for future requests. A 304 Not Modified is used when caching is involved—it tells the client the resource hasn’t changed since the last request, so the cached version can be reused, saving bandwidth.
Client Error Codes: 4xx Range
4xx codes point to issues with the request itself, often due to incorrect input or missing authentication. A 400 Bad Request means the server couldn’t understand the request due to malformed syntax. A 401 Unauthorized means authentication is required but missing or invalid; the response usually includes a WWW-Authenticate header. A 403 Forbidden means the server understood the request but refuses to authorize it, often due to insufficient permissions. A 404 Not Found means the requested resource could not be located at the given URL. A 429 Too Many Requests indicates the user has exceeded rate limits and should wait before trying again.
Server Error Codes: 5xx Range
5xx codes mean the server failed to fulfill a valid request. A 500 Internal Server Error is a generic catch-all for unexpected server-side problems. A 502 Bad Gateway occurs when a server acting as a gateway or proxy receives an invalid response from an upstream server. A 503 Service Unavailable means the server is temporarily unable to handle the request, often due to overload or maintenance, and may include a Retry-After header suggesting when to try again. A 504 Gateway Timeout happens when a proxy or gateway didn’t receive a timely response from an upstream server.
How to Use Response Codes in Practice
When building or consuming APIs, always check the status code before parsing the response body. Treat any 2xx code as success and proceed to use the data. For 4xx errors, review the request—check headers, body format, and authentication tokens. For 5xx errors, consider retrying after a delay, especially if the code is 503 or 504, as these are often temporary. Log unexpected 5xx codes for investigation, as they may indicate bugs or infrastructure issues.
Common Misunderstandings
Some developers treat any non-200 code as a failure, but 201, 204, and other 2xx codes are also successful outcomes. Similarly, a 304 is not an error—it’s a successful signal to use cached data. Never assume a 401 means the user doesn’t exist; it only means authentication failed, which could be due to a bad token, not a missing account. Avoid creating custom meanings for standard codes—stick to RFC 7231 definitions to ensure compatibility with tools and clients.
Checking and Testing Response Codes
Use tools like curl, Postman, or browser dev tools to inspect status codes during development. In code, check the status property of the response object before accessing parsed data. Write automated tests that verify both success and error cases—confirm that invalid input returns 400, missing auth returns 401, and server errors trigger appropriate retry logic. Monitor production APIs for spikes in 4xx or 5xx rates, as these can signal client bugs or server problems needing attention.