Apidog

All-in-one Collaborative API Development Platform

API Design

API Documentation

API Debugging

API Mock

API Automated Testing

Sign up for free

The Ultimate Guide to Understanding API Status Codes

Start for free
Contents
Home / Basic Knowledge / The Ultimate Guide to Understanding API Status Codes

The Ultimate Guide to Understanding API Status Codes

An API status code is a three-digit number sent from the server in response to a request made to an API. These codes are a standard part of the HTTP protocol and are used to indicate whether the request was successful, encountered an error, or requires further action.

When working with APIs, understanding API status codes is crucial for developers, admins, and testers. These codes, which are issued by a server in response to a client request, tell you if the request was successful, if there were any errors, and much more. In this article, we'll dive deep into the different types of HTTP status codes and what they mean for your API interactions.

What are API Status Codes?

An API status code is a three-digit number sent from the server in response to a request made to an API. These codes are a standard part of the HTTP protocol and are used to indicate whether the request was successful, encountered an error, or requires further action.

Why Do API Status Codes Matter? API status codes provide immediate feedback to developers, admins, and testers about the outcome of their API requests. Knowing how to interpret these codes can save you time in debugging and help ensure your application runs smoothly.

Common API Status Codes You Should Know

Certain API status codes are more common than others. Understanding these can greatly assist in troubleshooting and optimizing your API integrations.

200 OK

The most common status code, 200 OK, indicates that the request was successful. If you see this code, it means your API request went through without any issues.

201 Created

When you send a request to create a resource, a 201 Created status code means your request was successful, and a new resource was created as a result.

400 Bad Request

A 400 Bad Request indicates that the server could not understand the request due to invalid syntax. This often happens when the request body is malformed.

401 Unauthorized

The 401 Unauthorized status code means that the request requires user authentication. Without proper authentication credentials, the request will not be processed.

403 Forbidden

The 403 Forbidden status code tells you that while the server understands the request, it refuses to authorize it. This often happens due to insufficient permissions.

404 Not Found

The 404 Not Found status code is one of the most well-known. It indicates that the server could not find the requested resource, often because the endpoint or URL is incorrect.

500 Internal Server Error

A 500 Internal Server Error is a general error message when the server encounters an unexpected condition. This is usually a problem on the server side, not with the request itself.

5 Types of HTTP Status Codes

HTTP status codes are divided into five categories, each starting with a different digit. Let’s explore what each of these categories represents.

1xx - Informational Responses

The 1xx class includes informational responses, which indicate that the request was received and understood. However, no action is required at this stage.

  • 100 Continue: The server has received the request headers, and the client should proceed to send the request body.
  • 101 Switching Protocols: The requester has asked the server to switch protocols and the server has agreed.

2xx - Successful Responses

The 2xx status codes indicate that the request was successfully received, understood, and accepted.

  • 200 OK: The request was successful, and the server returned the requested resource.
  • 201 Created: The request has been fulfilled, and a new resource was created as a result.
  • 202 Accepted: The request has been accepted for processing, but the processing is not complete.

3xx - Redirection Messages

The 3xx status codes indicate that further action is required to complete the request.

  • 301 Moved Permanently: The requested resource has been permanently moved to a new URL.
  • 302 Found: The resource you requested is temporarily located at a different URL.
  • 304 Not Modified: The requested resource has not been modified since the last request.

4xx - Client Error Responses

The 4xx codes indicate that the client’s request contains incorrect syntax or cannot be fulfilled.

  • 400 Bad Request: The server cannot process the request due to a client error.
  • 401 Unauthorized: Authentication is required, and it has failed or has not yet been provided.
  • 403 Forbidden: The server understands the request but refuses to authorize it.
  • 404 Not Found: The server cannot find the requested resource.

5xx - Server Error Responses

The 5xx status codes signify that the server failed to fulfill a valid request.

  • 500 Internal Server Error: The server encountered an unexpected condition that prevented it from fulfilling the request.
  • 502 Bad Gateway: The server, acting as a gateway or proxy, received an invalid response from the upstream server.
  • 503 Service Unavailable: The server is currently unable to handle the request due to temporary overload or maintenance.

Troubleshooting API Status Codes

Understanding how to troubleshoot API status codes is key to diagnosing issues quickly.

Client-Side Errors (4xx)

  • Check your request syntax: A 400 Bad Request might mean that there's an issue with how you're formatting your API request.
  • Authenticate your request: A 401 Unauthorized usually means you need to include valid authentication credentials.
  • Review your permissions: A 403 Forbidden often requires checking user permissions or access rights.

Server-Side Errors (5xx)

  • 500 Internal Server Error: Check the server logs to determine the root cause.
  • 502 Bad Gateway: This may indicate an issue with the upstream server.
  • 503 Service Unavailable: This often means the server is overloaded. Consider retrying the request after some time.

How to Handle Different API Status Codes

When interacting with an API, how you handle different API status codes can determine the success of your application.

Handling 2xx Success Codes

  • 200 OK: Proceed with the response data as your request was successful.
  • 201 Created: If a resource was created, you might want to follow up by retrieving or confirming the creation.
  • 204 No Content: A 204 indicates that the request was successful, but there's no content to return. No further action is necessary.

Handling 4xx Client Errors

  • 400 Bad Request: Correct the request data or structure and resend.
  • 401 Unauthorized: Ensure the correct authentication details are provided.
  • 404 Not Found: Double-check the endpoint URL or the resource identifier.

Handling 5xx Server Errors

  • 500 Internal Server Error: Retry the request, or contact the API provider if the issue persists.
  • 502 Bad Gateway: Consider retrying after a brief wait time.
  • 503 Service Unavailable: Implement a retry mechanism with exponential backoff.

Effortlessly Manage API Status Codes with Apidog

Apidog is an all-in-one API tool designed to help you easily create, modify, and monitor API response codes for various scenarios. Whether dealing with client-side or server-side issues, Apidog equips you with the tools to handle them effectively.

button

This guide will show you how to view, create, and edit API response codes, enabling you and your team to fully leverage Apidog's powerful capabilities.

Viewing API Response Codes in Apidog

With Apidog, you can easily view potential responses that a server might return.

  1. Select the API: Choose the API whose response you'd like to inspect. In the image above, the selected API is "Find pet by ID."

Edit Header: Click the "Edit" header, as shown in the image. Scroll down to the "Responses" section to view the various API response codes and their descriptions.

  • Example response codes in the image include OK (200), Record Not Found (404), and Invalid Input (400).Response codes may vary depending on the HTTP method used. For more insights, check out our related article on REST API Methods.

Additionally, Apidog allows you to generate corresponding JSON response codes automatically through file imports, speeding up the API development process and increasing success rates.

Creating Custom API Response Codes in Apidog

Apidog also enables you to create additional response codes for different scenarios that might arise during client-server interactions. By clearly identifying whether the issue is on the client or server side, developers can quickly address the problem.

  1. Add Response Code: Locate the "+ Add" button next to the API response code headers. Select "Add Blank Response" to bring up a pop-up.
  2. Enter Details: Provide a name for the response code and the corresponding HTTP status code. Use intuitive names and codes to ensure clarity.

Personalizing API Response Codes with Apidog

Once you've created a new API response code (e.g., Created 201), Apidog offers various ways to enhance it:

  • Code Generation: Trust Apidog to automatically generate code.
  • Schema Reference: Use the schema reference function to build a functional API response.
  • Copy Existing Code: If a similar response code exists, copy and paste it into the "Add Example" section for further refinement later.

With Apidog, you have all the tools to manage API response codes efficiently and effectively