HTTP Methods & Status Codes

HTTP is the language of the web. To build a great API, you must use the protocol's built-in features correctly rather than reinventing them.

1. HTTP Methods (Verbs)

MethodStandard Use CaseIdempotent?
GETRetrieve a resourceYes
POSTCreate a new resourceNo
PUTUpdate/Replace a resourceYes
PATCHPartial update to a resourceNo
DELETERemove a resourceYes

2. Semantic Status Codes

Never return 200 OK for everything. Use specific codes so clients know what happened.

  • 201 Created: After a successful POST.
  • 204 No Content: After a successful DELETE.
  • 400 Bad Request: Validation failed.
  • 401 Unauthorized: No valid login found.
  • 403 Forbidden: Logic found, but user doesn't have permission.
  • 404 Not Found: Resource doesn't exist.
  • 429 Too Many Requests: Rate limit exceeded.

3. Idempotency

An operation is idempotent if performing it multiple times has the same effect as performing it once. GET and DELETEare idempotent. POST is not (as it creates a new record each time).

Pro Tip: Use the Location header in a 201 Createdresponse to tell the client exactly where the new resource can be found.