GET, POST, PUT, PATCH and DELETE, plus the status codes you will see every day. A plain guide to choosing the right ones when you build or call an API.

Every time your app talks to an API, two small things carry most of the meaning: the method you send (like GET or POST) and the status code you get back (like 200 or 404). Get them right and your API is easy to use. Get them wrong and everyone who calls it has to guess.

By the end you will be able to:

  • Choose the right method for reading, creating, replacing, updating and deleting.
  • Explain what “safe” and “idempotent” mean in plain words.
  • Pick sensible status codes for success and failure.
  • Tell 401 and 403 apart, and 400 and 422 apart.

A request and a response

HTTP is a conversation. The client sends a request made of a method, a path and sometimes a body. The server replies with a response made of a status code and usually a body.

The method says what you want to do. The status code says how it went.

The five methods you need

MethodUse it toTypical pathSuccess code
GETRead data/users/42200
POSTCreate something new/users201
PUTReplace a whole item/users/42200 or 204
PATCHChange part of an item/users/42200
DELETERemove an item/users/42204

Notice the paths are nouns (/users), not verbs (/getUsers). The method already says what to do, so the path only needs to say what it is done to.

Safe and idempotent, in plain words

Two words come up a lot. They sound academic but the ideas are simple.

  • Safe means the request does not change anything on the server. Reading is safe.
  • Idempotent means doing it once or ten times leaves the server in the same state. Deleting user 42 ten times still leaves them deleted.
MethodSafe?Idempotent?
GETYesYes
PUTNoYes
DELETENoYes
PATCHNoNot guaranteed
POSTNoNo

Why care? Networks fail. If a request times out, a client can safely retry an idempotent call. Retrying a POST might create two orders, which is why payment APIs ask for an idempotency key.

Status codes: read the first digit

You do not need to memorize sixty codes. The first digit tells you the story, and a handful of specific codes cover most real work.

Start with the first digit. It tells you who needs to act.
CodeMeaningWhen to use it
200OKRequest worked and there is a body to return
201CreatedA POST made something new. Include its location
204No ContentWorked, nothing to return, like a DELETE
400Bad RequestThe request is malformed, for example broken JSON
401UnauthorizedNot logged in, or the token is missing or expired
403ForbiddenLogged in, but not allowed to do this
404Not FoundThat item does not exist
409ConflictClashes with current state, like a duplicate email
422UnprocessableValid JSON, but the values break a rule
429Too Many RequestsRate limit hit. Say when to retry
500Server ErrorSomething unexpected broke on our side
503UnavailableTemporarily down or overloaded

Two pairs people mix up

401 versus 403

401 is about who you are: “I do not know you, please sign in.” 403 is about what you may do: “I know who you are, and the answer is no.”

400 versus 422

Use 400 when the server cannot even understand the request, such as invalid JSON. Use 422 when it understood fine, but the content breaks a rule, such as an email without an @.

A good error response

A status code alone is not enough. Return a small, consistent body so developers know what to fix:

422 response
{
  "error": {
    "code": "invalid_email",
    "message": "The email address must contain an @ sign.",
    "field": "email"
  }
}