Lonti Blog

Using error codes to improve API user experience: best practices and advanced techniques

Written by John Lanctot | March 15, 2023

The success of an API is not just determined by its functionality, but also by its user experience. A well-designed API that is easy to use and understand can make a significant difference in the success of an application.

One key aspect of API user experience is the handling of errors. When something goes wrong with an API call, developers need to know what went wrong and how to fix it. This is where error codes can be extremely useful. Error codes provide a standardized way to communicate errors to developers, making it easier for them to troubleshoot issues and improve their applications.

Common API error codes

When a developer makes an API call, there are many things that can go wrong. The server might be down, the request might be malformed, or the user might not have permission to perform the requested action. In order to help developers troubleshoot these issues, APIs often use error codes to indicate what went wrong.

Here are some common API error codes and their meanings:

400 Bad Request:

This error code indicates that the server cannot process the request because the client has sent a malformed request, such as missing or invalid parameters.

Example: A developer might receive a 400 Bad Request error if they forget to include a required parameter when making an API call, or if they provide a parameter with an invalid value.

401 Unauthorized: 

This error code indicates that the client is not authorized to access the requested resource. This might occur if the user does not have the necessary credentials or if the request is missing an authentication token.

Example: A developer might receive a 401 Unauthorized error if they attempt to access an API resource without providing the correct login credentials, or if they provide an expired authentication token.

403 Forbidden: 

This error code indicates that the client is not allowed to access the requested resource, even if they are authenticated. This might occur if the user does not have the necessary permissions to access the resource, or if the resource is not available to the public.

Example: A developer might receive a 403 Forbidden error if they attempt to access an API endpoint that is not available to their user role, or if they try to access an endpoint that has been restricted by the API provider.

404 Not Found: 

This error code indicates that the requested resource could not be found on the server. This might occur if the URL is incorrect, if the resource has been moved or deleted, or if the server is temporarily unavailable.

Example: A developer might receive a 404 Not Found error if they attempt to access an API endpoint that does not exist, or if they provide an incorrect resource identifier.

500 Internal Server Error: 

This error code indicates that an unexpected error occurred on the server while processing the request. This might occur due to a bug in the API, a problem with the server infrastructure, or an issue with the database.

Example: A developer might receive a 500 Internal Server Error if the API encounters an unexpected error while processing a request, such as a missing dependency or a syntax error.

Best practices for using error codes

Choosing the right error codes and designing helpful error messages is essential for creating a positive user experience with your API. Here are some best practices to keep in mind:

Guidelines for choosing error codes:

  • Use HTTP status codes: 
    Whenever possible, use the standard HTTP status codes to indicate errors. This helps to create a consistent experience for developers and makes it easier to understand what went wrong.

  • Be specific: 
    Choose error codes that accurately reflect the problem that occurred. Vague error messages like "An error has occurred" are not helpful and can make it difficult for developers to diagnose issues.

  • Use consistent error codes: 
    Use the same error code for a specific error across all API endpoints. This helps to create a consistent experience and makes it easier for developers to understand the meaning of each error code.

Tips for designing error messages that are helpful for users:

  • Be clear and concise: 
    Use plain language to explain the error in as few words as possible. Avoid technical jargon that may not be familiar to all developers.

  • Provide context: 
    Include information about the specific request that generated the error, such as the request URL or parameters. This can help developers diagnose and fix the issue more quickly.

  • Suggest next steps: 
    Provide suggestions for how the developer can fix the error, such as correcting a parameter value or contacting support. This can help to reduce frustration and make the error feel more manageable.

Recommendations for formatting error codes in a way that is easy to understand:

By following these best practices, you can create a more user-friendly API that is easier to use and troubleshoot.

  • Use consistent formatting: 
    Use a consistent format for all error codes to make them easier to read and understand. For example, you might use a three-digit number followed by a brief message explaining the error.

  • Group related errors together:
    Group related errors together under a common error code or error category. This can help developers quickly understand the cause of the error and identify solutions.

  • Provide documentation: 
    Include documentation that explains each error code and provides guidance on how to fix the issue. This can help to reduce the number of support requests and make it easier for developers to troubleshoot issues on their own.

Advanced techniques for improving API user experience with error codes

While choosing the right error codes and designing helpful error messages is essential for creating a positive user experience with your API, there are also more advanced techniques that you can use to provide even more specific information and help users troubleshoot issues more effectively.

How to use error codes to provide more specific information about errors:

  • Use error subcodes: 
    In addition to using standard HTTP status codes, you can use subcodes to provide more specific information about the error. For example, you might use a 404 error code to indicate that a resource was not found, but use a subcode to indicate whether the issue was caused by a missing parameter, an invalid parameter value, or another issue.

  • Include error details: 
    When an error occurs, provide detailed information about the cause of the error, such as the specific parameter that was invalid or the location of the resource that was not found. This can help developers diagnose and fix the issue more quickly.

How to use error codes to help users troubleshoot issues:

  • Provide error recovery suggestions: 
    Provide suggestions for how the user can recover from the error, such as retrying the request with different parameters or contacting support. This can help to reduce frustration and make the error feel more manageable.

  • Provide diagnostic information: 
    In addition to providing suggestions for how to recover from the error, provide diagnostic information that can help the user identify the cause of the error, such as detailed error logs or a list of recent requests.

Examples of how other companies have successfully used error codes to improve API user experience:

Stripe:
Stripe's API provides detailed error messages that include the specific parameter that caused the error and suggestions for how to fix the issue. The error messages also include a unique request ID that can be used to look up additional diagnostic information.

Twilio: 
Twilio's API uses subcodes to provide more specific information about errors, such as whether the issue was caused by a malformed request or an authentication issue. The error messages also include a link to documentation that provides guidance on how to fix the issue.

GitHub: 
GitHub's API provides detailed error messages that include the specific resource that was not found and suggestions for how to fix the issue. The error messages also include a link to documentation that provides additional information about the error.

By using these advanced techniques, you can create an even more user-friendly API that provides specific information and helps users troubleshoot issues more effectively. In the next section, we'll provide some tips for testing your error codes to ensure that they are providing the best possible user experience.

Don't overlook the user experience of your API: how error codes can help

We've explored how error codes can play a crucial role in improving the user experience of APIs. By choosing the right error codes, designing helpful error messages, and using advanced techniques to provide even more specific information and help users troubleshoot issues, you can create an API that is more user-friendly and easier to work with.

It's important to prioritize the user experience of your API because it can have a significant impact on the success of your product or service. A poorly designed API can lead to frustration, wasted time and resources, and even lost business. By using error codes effectively, you can help users quickly diagnose and fix issues, reducing the likelihood of these negative outcomes.

We encourage all developers and API designers to consider the importance of using error codes to improve the user experience of their APIs. By taking the time to choose the right error codes, design helpful error messages, and use advanced techniques to provide even more specific information, you can create an API that is more intuitive, efficient, and satisfying for your users.