Consistent errors, idempotency and REST API versioning: conventions that help an API last

  1. Introduction
  2. Consistent error responses
  3. Idempotency: retrying without fear of duplicates
  4. Versioning
  5. Deprecation notices
  6. Summary
  7. References

Introduction

The two earlier articles in this series covered resource names, methods, status codes, pagination, filtering and sorting.

This one covers three things that decide whether an API is easy to live with over the long term: how errors are reported, how repeated requests are handled, and how to change the API without breaking the people using it.

If you want the foundations first, read the two previous articles and then come back here.

Consistent error responses

A status code tells you the kind of error, but a client usually needs more: what exactly caused it, which field is wrong, how it can be fixed.

If every endpoint returns errors in its own style (sometimes a string, sometimes {"message": ...}, sometimes {"errors": [...]}), the client side fills up with one-off handling code.

The IETF has a standard for this: RFC 9457 (Problem Details for HTTP APIs), which replaces RFC 7807. The error response uses the content type application/problem+json with a few standard fields:

The standard allows extra fields of your own. For example, a validation error:

HTTP/1.1 422 Unprocessable Content
Content-Type: application/problem+json

{
  "type": "https://api.example.com/problems/validation-error",
  "title": "Invalid data",
  "status": 422,
  "detail": "One or more fields are not in the right format.",
  "errors": [
    { "field": "email", "message": "Must be a valid email address" }
  ]
}

Whether you use RFC 9457 or define your own shape, a few principles always hold:

Idempotency: retrying without fear of duplicates

In the first article we saw that GET, PUT and DELETE are idempotent while POST is not.

This causes a real problem: a client sends POST /payments, the server finishes processing but the response is lost because the network drops.

The client does not know the outcome, and if it resends, it may charge twice.

The common solution is an idempotency key: the client generates a unique value (for example a UUID) for each logical operation and sends it in a header, usually named Idempotency-Key.

The server does the following:

  1. Receive the request and check whether it has seen the key.

  2. Not seen: process it, store the result (status code and body) tied to the key, and return it.

  3. Seen with the same content: do not process again, return exactly the stored result.

  1. Seen but with different content: reject with an error (usually 409 or 422), since the client may have reused a key by mistake.

POST https://api.example.com/payments
Idempotency-Key: 6f1c2a3e-1d4b-4f6e-9a77-0c2f5b8d9e11
Content-Type: application/json

{"orderId": 42, "amount": 150000}

Some notes for implementation:

For PUT and DELETE you get idempotency "for free" if designed correctly: PUT sets a fixed final state, and deleting something already deleted is still "gone".

Versioning

Sooner or later you will want to change the API: rename a field, drop an endpoint, restructure a response.

If there are clients running that you do not control (a mobile app installed on users' phones, say), changing things carelessly breaks them. Two approaches work together:

1. Prefer backward-compatible changes. Adding a new field to a response, a new endpoint or an optional parameter usually does not break clients (if they are written correctly and ignore unknown fields).

Renaming, changing a type, removing a field or changing a meaning is a breaking change.

2. When you must break, release a new version. Common ways to carry the version number:

Approach

In the path

Example

/v2/orders

Notes

Easy to see, route and try in a browser

Approach

In a header

Example

X-API-Version: 2 or a custom media type

Notes

Cleaner URLs but harder to see and try

Approach

Query parameter

Example

/orders?version=2

Notes

Less liked because it is easily confused with filters

There is no single "right" way.

Putting the version in the path is popular because it is simple; what matters more is a clear policy: when to bump the version, how long old versions are supported, and how you give notice.

Deprecation notices

Before turning off an old endpoint or version, announce it in advance.

Technically, the Sunset header (RFC 8594) tells when a resource will stop working, and you can add a Link pointing to a migration guide.

Combined with an email or a developer news page, and with watching the logs to see how many clients still call the old version, you can switch it off without surprising anyone.

This is also where your delivery process matters: when changing an API, deploy in a way that lets two versions run side by side and can be rolled back, as described in Zero-downtime deployment and rollback in a pipeline: rolling, blue/green and canary.

Summary

References

Consistent errors, idempotency and REST API versioning: conventions that help an API last