REST API codes are three-digit HTTP status codes that tell a client whether a request to a server was successful. The specific code depends on the category of the response, which is indicated by the first digit of the three-digit number.
Standard Response Categories and Meanings
Every status code falls into one of five classes. These classes allow you to quickly identify if the problem lies with the request you sent, a configuration error on the server, or a successful transaction. Understanding these categories helps you debug your code without needing to read the full response body every time.
| Code Range | Category | General Meaning |
|---|---|---|
| 100-199 | Informational | Request received, continuing process |
| 200-299 | Success | The action was successfully received and accepted |
| 300-399 | Redirection | Further action is needed to complete the request |
| 400-499 | Client Error | The request contains bad syntax or cannot be fulfilled |
| 500-599 | Server Error | The server failed to fulfill an apparently valid request |
Success Codes for Confirmed Actions
Success codes confirm that the server understood the request and performed the intended action. While 200 is the most common, using more specific codes helps the client know exactly what happened to the data.
200 OK
This is the standard response for successful HTTP requests. In a GET request, the server sends the requested resource in the response body. In a PUT or PATCH request, it confirms the update was successful. You use this when the operation is complete and there is data to return to the user.
201 Created
This code is used specifically after a POST request that results in the creation of a new resource. It tells the client that the resource now exists. It is standard practice to include a Location header in the response, providing the URL where the new resource can be found.
204 No Content
A 204 response means the server successfully processed the request, but there is no content to send back. This is most common for DELETE requests. Since the resource is gone, there is nothing to show, and a 204 tells the client not to expect a response body.
Client Error Codes for Request Fixes
Client error codes indicate that the sender made a mistake. These codes are your primary tool for telling a user or a developer exactly why their request failed so they can fix it without contacting support.
400 Bad Request
The server cannot process the request due to something perceived to be a client error. This often happens when the JSON body is malformed, a required field is missing, or the data type is incorrect. For example, sending a string where the server expects an integer will trigger a 400.
401 Unauthorized
This code means the request lacks valid authentication credentials. It is not about permissions, but about identity. If the API key is missing or the token has expired, the server returns a 401. The client must provide valid credentials to proceed.
403 Forbidden
Unlike 401, a 403 means the server knows who you are, but you do not have permission to access the specific resource. For instance, a user with a "Guest" role attempting to delete an admin account will receive a 403. No amount of re-authenticating will fix this; the user's permissions must be changed.
404 Not Found
The server cannot find the requested resource. This happens if the URL is mistyped or if the resource (like a specific user ID) has been deleted. It is important to use 404 for missing resources rather than 400, as it specifically points to the URI being the issue.
429 Too Many Requests
This code is used for rate limiting. If a client sends 100 requests per second when the limit is 10, the server returns a 429. This prevents server crashes and abuse. You should include a Retry-After header to tell the client how many seconds to wait before trying again.
Server Error Codes for System Failures
Server errors occur when the request is valid, but the server fails to execute it. These are the most critical codes to monitor because they usually indicate a bug in the backend code or a failure in the infrastructure.
500 Internal Server Error
This is a generic catch-all for when the server encounters an unexpected condition. It often happens due to unhandled exceptions in the code, such as a null pointer reference or a crash in a third-party library. You should log the specific error on the server but return a generic 500 to the client for security reasons.
502 Bad Gateway
A 502 occurs when one server, acting as a proxy or gateway, receives an invalid response from an upstream server. This is common in environments using Nginx or Apache as a reverse proxy in front of a Node.js or Python application. It usually means the backend application has crashed or is restarting.
503 Service Unavailable
This indicates the server is currently unable to handle the request. This is usually temporary, caused by maintenance or a sudden spike in traffic that has overloaded the CPU or memory. Unlike 500, a 503 suggests the server is still healthy but simply too busy.
504 Gateway Timeout
This happens when the proxy server did not receive a timely response from the upstream server. This is different from a 502 because the connection was established, but the backend took too long to process the data. This often points to slow database queries or external API calls that are hanging.
Practical Implementation Example
To see how these codes work in a real scenario, imagine a simple API for managing a list of books. The interaction follows a logical flow of requests and responses.
- Request: POST /books with body { "title": "The Great Gatsby", "author": "F. Scott Fitzgerald" }
Response: 201 Created. The server adds the book to the database and returns the new ID. - Request: GET /books/123 (where 123 does not exist)
Response: 404 Not Found. The server checks the database, finds no book with that ID, and informs the client. - Request: PUT /books/1 with body { "title": 12345 } (sending a number instead of a string)
Response: 400 Bad Request. The server validates the data type and rejects the request before it reaches the database. - Request: DELETE /books/1 (without an API key)
Response: 401 Unauthorized. The server detects the missing authentication header and stops the request. - Request: GET /books/1 (while the database is offline)
Response: 500 Internal Server Error. The code attempts to connect to the database, fails, and throws an exception.
Rules for Choosing the Correct Code
Choosing the right code prevents confusion and reduces the time spent debugging. A good rule of thumb is to ask: "Who is responsible for this error?" If the client can fix it by changing the request, use a 4xx code. If the client did everything right but the system failed, use a 5xx code.
Another helpful guideline is to be as specific as possible. Do not use 400 for everything; use 401 for auth, 403 for permissions, and 404 for missing data. This allows the client application to automate its response—for example, automatically redirecting a user to the login page when a 401 is received, but showing a "Page Not Found" screen for a 404.
When creating resources, always prefer 201 over 200. When deleting, prefer 204 over 200. These distinctions make the API predictable and compliant with REST standards, which makes it easier for other developers to integrate with your system without reading extensive documentation.