Skip to content

Menu

REST API Design Cheat Sheet: Methods, Status Codes and Idempotency

Checked against HTTP (RFC 9110) RFC 9110 (HTTP Semantics), RFC 5789 (PATCH), draft-ietf-httpapi-idempotency-key-header-09 on 28 November 2026. Each section links to the official docs it was checked against.

This is for backend developers designing JSON-over-HTTP APIs who want answers grounded in the HTTP specs rather than in blog folklore. The method and status code rows come from RFC 9110 and RFC 5789. Anything the RFCs don't define, such as pagination parameters or URL versioning, is labelled as a convention.

When to use what

Choose the method by what the request promises, not by what the handler does. GET, HEAD, OPTIONS and TRACE are safe: the client asks for no state change. PUT and DELETE are idempotent as well, which means a client or proxy may resend them after a timeout without asking you. POST and PATCH carry neither promise, so a retried POST can create two orders. That is the gap idempotency keys fill.

Use PUT when the client sends the whole resource and PATCH when it sends a set of changes. If your PUT handler quietly keeps fields the client left out, you have built a PATCH with the wrong name, and clients will rely on it.

For errors, the honest test is "what can the client do next?" 400 says fix the request, 401 says authenticate (and you must send WWW-Authenticate), 403 says don't bother retrying with the same credentials, 409 says the current state blocks this, and 422 says the syntax was fine but the content wasn't. Our HTTP status codes tool and method reference are quick lookups, and the status codes guide goes deeper. For versioning there is no standard; the API versioning post compares the options.

Safe means the client doesn't request a state change. Idempotent means sending the same request several times has the same intended effect as sending it once. Both are promises about intent; your server can still log or count requests.

GET

Read a representation; safe and idempotent, so caches and crawlers may call it freely

Try it: http-method-referenceFrom the docs, not run by us
HEAD

Same as GET without the body; safe and idempotent, handy for checking size or ETag

From the docs, not run by us
OPTIONS

Ask which methods and features a resource supports; safe and idempotent

From the docs, not run by us
POST

Process the enclosed content, often to create a subresource; neither safe nor idempotent

From the docs, not run by us
PUT

Create or replace the resource at this URL with the enclosed state; idempotent, not safe

From the docs, not run by us
DELETE

Remove the mapping for this URL; idempotent, not safe

From the docs, not run by us
PATCH

Apply a set of changes described in the body; neither safe nor idempotent (RFC 5789)

From the docs, not run by us
POST -> 201 Created + Location

Return 201 with a Location header naming the new resource when POST creates one

Try it: HTTP Status CodesFrom the docs, not run by us
PUT -> 201 or 200/204

Return 201 if PUT created the resource and 200 or 204 if it replaced an existing one

From the docs, not run by us
DELETE -> 202 / 204 / 200

202 if deletion is queued, 204 if done with nothing to say, 200 if done and you return a status body

From the docs, not run by us
405 + Allow

Reject an unsupported method; RFC 9110 requires an Allow header listing the methods you do support

From the docs, not run by us
401 + WWW-Authenticate

Signal missing or invalid credentials; the challenge header is mandatory

From the docs, not run by us
409 Conflict

Refuse because the resource's current state conflicts, such as an edit on a stale version

From the docs, not run by us
412 Precondition Failed

Reject a conditional request whose If-Match or similar header evaluated to false

From the docs, not run by us
422 Unprocessable Content

Reject well-formed content that fails validation or business rules

From the docs, not run by us

A PATCH must apply all changes or none. Advertise accepted patch formats with Accept-Patch.

PUT /users/42 {full body}

Replace the whole resource; fields you omit are expected to be reset, not kept

From the docs, not run by us
PATCH /users/42 + Content-Type: <patch format>

Send a description of the changes in a declared patch media type, not a full resource

Try it: json-patch-builderFrom the docs, not run by us
If-Match: "e0023aa4e"

Make a PATCH or PUT conditional on the ETag so two clients can't overwrite each other

From the docs, not run by us
415 + Accept-Patch

Reject a patch format you don't support and list the formats you do

From the docs, not run by us
422 on PATCH

Reject a patch that is valid but would leave the resource in an invalid state

From the docs, not run by us

Draft, not a standard: draft-ietf-httpapi-idempotency-key-header-09. Expiry is left to each API, which should document it. See our idempotency keys post.

Idempotency-Key: "8e03978e-40d5-43e8-bc93-6894a57f9324"

Mark a POST or PATCH so the server can return the first result for retries; the value is a quoted structured-field string

Try it: UUID GeneratorFrom the docs, not run by us
400 when the key is missing

Tell the client this endpoint requires a key, ideally with a problem-details body linking your docs

From the docs, not run by us
422 when a key is reused with a different body

Refuse a retry that isn't really a retry

From the docs, not run by us
409 while the first request is still running

Ask the client to retry later without changing anything

From the docs, not run by us

None of these are defined by an RFC. They are common conventions; pick one and use it everywhere in your API.

GET /notes?limit=50&cursor=abc

Cursor pagination (convention); stable when rows are inserted while a client pages

From the docs, not run by us
GET /notes?page=3&per_page=50

Offset pagination (convention); easy to jump to a page, but rows shift under concurrent writes

From the docs, not run by us
GET /notes?status=open&sort=-created_at

Filter and sort with query parameters (convention); document the allowed fields

From the docs, not run by us
/v1/notes

Version in the path (convention); visible and cache-friendly, but every version is a new URL

Try it: api-versioning-helperFrom the docs, not run by us
Accept: application/vnd.example.v2+json

Version by media type (convention); keeps URLs stable but is harder to try in a browser

From the docs, not run by us

Worked example: Make a payment endpoint safe to retry

A mobile client posts a payment, the network drops, and the app retries. Without a key, that's two charges. With one, the flow looks like this:

POST /payments HTTP/1.1
Content-Type: application/json
Idempotency-Key: "8e03978e-40d5-43e8-bc93-6894a57f9324"

{"amount": 1200, "currency": "eur"}

HTTP/1.1 201 Created
Location: /payments/pay_81

The server stores the key with the request fingerprint and the response. When the identical retry arrives, it returns the stored 201 and the same Location instead of charging again. If the retry arrives while the first request is still running, the draft says to answer 409, which tells the client to wait and try again unchanged. If a buggy client reuses the key with a different amount, the server answers 422, because that's a new request wearing an old key. Notice what didn't need a key: a PUT /payments/pay_81/note that sets a note is already idempotent under RFC 9110, so resending it is harmless. Keys earn their place on POST and PATCH, which carry no such promise. Write down how long you keep keys; the draft leaves that to you.

Common mistakes

  • Using GET for actions like /users/42/delete because it is easy to call from a link.

    Crawlers, prefetchers and caches assume GET is safe. Use DELETE or POST so a link preview can't delete data.

  • Returning 200 with {"error": ...} in the body for failures.

    Use a 4xx or 5xx code so clients, proxies and monitoring see the failure; put details in the body as well.

  • Sending 401 without a WWW-Authenticate header, or using 401 when the user is known but lacks rights.

    Send the challenge header with every 401, and use 403 for an authenticated caller who isn't allowed.

  • Retrying POST automatically in a client library because the request timed out.

    Retry only idempotent methods automatically, or send an Idempotency-Key the server honours.

  • Answering 405 without listing what is allowed.

    RFC 9110 requires an Allow header on 405, such as Allow: GET, HEAD.