Designing a REST API: naming resources, choosing HTTP methods and picking the right status codes

  1. Introduction
  2. Naming resources
  3. Choosing HTTP methods
  4. Choosing status codes
  5. A small example
  6. Summary
  7. References

Introduction

When you build an API for a mobile app, a website or another service to call, the hardest part is usually not the code but setting conventions so that other people can guess how to use it without asking you.

This article covers three foundational conventions of a REST API running over HTTP: how to name resources in the URL, how to choose an HTTP method, and how to choose the status code you return.

A small caveat: "REST" is an architectural style, not a certified standard. Most APIs called REST in the real world follow only part of that style.

So the practical goal here is consistency and predictability, while the behavior of each method and status code is already well described by the HTTP standard (RFC 9110), so you should follow it.

Naming resources

A resource is a "thing" the API manages: a user, an order, an invoice, an uploaded file. Each resource has an address (a URL). A few common conventions keep addresses readable:

Some operations do not look like "create, update, delete a record", for example "cancel an order" or "resend the confirmation email". There are two common approaches.

The first treats the result as a new resource: POST /orders/42/cancellations. The second accepts a "verb" at the end of the path: POST /orders/42/cancel.

The latter is shorter but departs from the noun convention; either is fine, so document it and apply it consistently.

Choosing HTTP methods

The method says what you want to do with the resource. The HTTP standard (RFC 9110) defines two properties worth remembering:

Method

GET

Used to

Read a resource or a list

Example

GET /orders/42

Method

POST

Used to

Create, or trigger an operation

Example

POST /orders

Method

PUT

Used to

Replace a whole resource

Example

PUT /orders/42

Method

PATCH

Used to

Partially modify a resource

Example

PATCH /orders/42

Method

DELETE

Used to

Delete a resource

Example

DELETE /orders/42

A few points that are often confused:

Choosing status codes

A status code is the three-digit number on the first line of the response. Remember the class first:

The codes used most in APIs:

Code

200 OK

Meaning

Success, with a body

When to use

Successful GET, PUT, PATCH

Code

201 Created

Meaning

A new resource was created

When to use

POST that creates; should include a Location header pointing at the new resource

Code

204 No Content

Meaning

Success, no body

When to use

Successful DELETE

Code

400 Bad Request

Meaning

Syntactically wrong request

When to use

Broken JSON, missing required parameter

Code

401 Unauthorized

Meaning

Not authenticated or credentials wrong

When to use

Token missing or expired

Code

403 Forbidden

Meaning

Authenticated but not allowed

When to use

The user may not view this resource

Code

404 Not Found

Meaning

Not found

When to use

Address or id does not exist

Code

405 Method Not Allowed

Meaning

Method not supported at this address

When to use

DELETE /orders when the API disallows it; should include an Allow header

Code

409 Conflict

Meaning

Conflicts with current state

When to use

Creating a record that violates a unique key

Code

422 Unprocessable Content

Meaning

Syntax is fine but the content is invalid

When to use

Malformed email, negative quantity

Code

429 Too Many Requests

Meaning

Too many calls

When to use

Rate limit exceeded (defined in RFC 6585)

Code

500 Internal Server Error

Meaning

Unexpected server failure

When to use

Unhandled exception

Code

503 Service Unavailable

Meaning

Temporarily cannot serve

When to use

Maintenance, overload

The two most confused pairs are 401 and 403 (401: "I do not know who you are"; 403: "I know you, but you are not allowed") and 400 and 422 (400: the request cannot be read; 422: it can be read but a value is invalid).

Many APIs merge 400 and 422 into one, which is acceptable as long as you are consistent.

Do not return 200 OK with a body like {"error": "..."} for a failure.

Monitoring tools, caches and client libraries all rely on the status code to guess what happened; the wrong code makes everything downstream misread it.

A small example

Suppose you create an order and read it back (addresses use the example domain):

POST https://api.example.com/orders
Content-Type: application/json

{"customerId": 7, "items": [{"sku": "A-100", "quantity": 2}]}

A sensible response:

HTTP/1.1 201 Created
Location: https://api.example.com/orders/42
Content-Type: application/json

{"id": 42, "status": "pending"}

After that GET /orders/42 returns 200, DELETE /orders/42 returns 204, and calling GET /orders/42 again returns 404.

This sequence of codes is enough for a client to understand the whole lifecycle without reading anything else.

Summary

Every convention is a commitment to your API's users. Choose few, choose them clearly, write them down and keep them.

References

Designing a REST API: naming resources, choosing HTTP methods and picking the right status codes