Knowledge
Consistent errors, idempotency and REST API versioning: conventions that help an API last
- Every API error should share one shape, for example the "problem details" of RFC 9457, so clients can handle them with one piece of common code.
- For non-idempotent operations such as POST, an idempotency key sent by the client makes retries safe without creating duplicates.
- Versioning and deprecation notices let you change an API without breaking the clients that are already running.
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:
type: a URI identifying the kind of error (it can point to a documentation page).title: a short, human-readable summary, stable for the same kind of error.status: the HTTP status code, repeated for the client's convenience.detail: a description specific to this occurrence.instance: an identifier for this occurrence, handy when searching logs.
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:
- One shape for every error of every endpoint, including the
404,405and500errors generated by the framework. - A machine-readable code (such as
typeor a stablecodefield) separate from the message for people. Clients should rely on the code, not the description string, because strings can change. - Do not leak internals such as stack traces, table names or file paths. Put those details in the log, and let the response carry only a reference code to look up later.
- With
429, include aRetry-Afterheader so the client knows how long to wait.
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:
- Receive the request and check whether it has seen the key.
- Not seen: process it, store the result (status code and body) tied to the key, and return it.
- Seen with the same content: do not process again, return exactly the stored result.
- Seen but with different content: reject with an error (usually
409or422), 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:
- A key is valid only for a period of time (for example a few dozen hours); after that it can be deleted so the store does not bloat. State that period in the documentation.
- Checking the key and recording the result must be atomic (for example with a unique constraint in the database), otherwise two requests arriving at the same time can both slip through.
Idempotency-Keyis a widespread convention used by many services and is being drafted by the IETF as a draft, not yet a complete RFC at the time of writing; so describe your exact behavior in the documentation instead of assuming every client already knows it.
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 | Example | Notes |
|---|---|---|
| In the path | /v2/orders |
Easy to see, route and try in a browser |
| In a header | X-API-Version: 2 or a custom media type |
Cleaner URLs but harder to see and try |
| Query parameter | /orders?version=2 |
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
- One error shape with a machine-readable code; RFC 9457 is a ready-made standard option.
- Use idempotency keys so important POST operations can be retried safely.
- Prefer backward-compatible changes, number versions when you must break, and announce deprecation with a clear deadline.