All topics
API error best practices

API error best practices

Communicate problems their resolution when processing API requests.

Problem statement

API errors can occur due to various reasons such as invalid requests, authentication issues, or problems with the server.

Errors are one of the feedback mechanisms developer rely upon during development to ensure their integration fulfills all requirements and edge cases. Poor error responses can contribute to longer development times and a less reliable service in production. This is especially true for developers who learn by doing and approach new APIs with a trial-and-error strategy.

Sometimes a end user needs to be prevented from completing their current task due to legitimate business policies. In these situations, it is important to maintain the end user's trust and provide them with a recourse if possible. Great errors are therefore not just the final state of a user journey, but can be the beginning of a new one.

The status code of a HTTP response usually does not communicate enough information to the developer or end user to be able to understand and resolve the underlying issue. Therefore it is incumbent on API providers to supply clear and helpful information in the response body.

Success criteria

Since errors are a critical source of feedback to developers, their quality directly impacts developer experience.

GoalSignalMetric
Improve developer successRequest error rateReduce the proportion of API requests that are not successful by X%.
Improve developer successNumber of support ticketsThe number of support tickets related to API errors is reduced by X%.
Improve developer activationTime to hello worldThe average time between a developer creating an account to successfully making an API request and obtaining a response is less than X hours/days.
Improve developer experienceDeveloper satisfactionNet promotor score (NPS), customer effort score (CES), or similar.

Product requirements

A good error error response satisfies four use cases:

  1. Provides the developer with a human-readable message for troubleshooting.
  2. Encodes the type of error as a stable identifier that can be read programatically.
  3. Includes all dynamic information relevant to the error instance as machine-readable variables.
  4. Includes a localized message that may be shown verbatim to the end user.

User stories

Most REST APIs build upon the standard HTTP status codes to provide additional semantics relevant to the business domain. This usually takes the form of an additional JSON field in the response body.

Documenting each failure mode upfront allows application developers to craft tailored experiences for each scenario, if they wish. Of course, application developers should still build a generic, catch-all flow.

Using the example of an invalid request error, an error response should describe which fields were invalid and why to the client. If this information is structured in a machine-readable format, the client application can provide an experience that is tailored to the error, for example by highlighting and focussing the inputs for the invalid fields in the interface.

All errors should provide a human readable description of the problem in the response body. This aids developers during the development and testing phases. Logging systems typically also include this message as part of the log record.

Include as much information as practible, without disclosing sensitive information.

Occasionally an end user will need to contact support in order to troubleshoot or resolve an error that occured. Providing a trace identifier can often expediate this process by allowing support agents and engineers to immediately identify the transaction where the error occured.

The primary message included in an error response is usually intended for the developer integrating the API. As such, it is less suited for the end user and may come across as cryptic, confusing or off-putting.

By including a localized error message intended for the end user in error responses, you can make an API simpler to integrate against as the developer no longer has to choose between writing a message for each error code or relying on an unhelpful, generic message.

API errors should provide further detail about the API contract to the client. They should not be used to debug the server implementation. Take care not to include implementation details such as stack traces or pass through error messages from third-party frameworks.

Logging and monitoring API errors allows API owners to track and analyze patterns, enabling them to make data-driven decisions to enhance the API's performance and stability.

Explore design patterns

Problem details

Communicate API errors using a well-defined standard.

Was this page helpful?

Made by Criteria.