Most people assume REST API rules are a strict standard enforced by a governing body. In practice, REST is a set of architectural constraints described by Roy Fielding in his 2000 dissertation, not a formal specification with a compliance test. The rules are conventions: follow them and your API behaves predictably for clients; bend them and you take on the cost of explaining your deviations.

What the core REST API rules actually require

REST stands for Representational State Transfer. The constraints are few, and each one exists to keep clients and servers loosely coupled.

  • Client-server separation. The interface between client and server is uniform, so each side can evolve independently.
  • Statelessness. Each request carries all the information needed to understand it. The server keeps no session state between calls.
  • Cacheability. Responses declare whether they can be cached, and for how long.
  • Uniform interface. Resources are identified by URIs, manipulated through representations, and described by self-descriptive messages.
  • Layered system. A client cannot tell whether it is connected directly to the origin server or through an intermediary.
  • Code on demand (optional). Servers may send executable code to clients, though most APIs skip this.

If an API meets the first five, it is generally called RESTful. The sixth is optional and rarely used.

Why statelessness is the rule people break most

Statelessness is the constraint that surprises newcomers. It does not mean the server stores no data; it means the server stores no client session data between requests. Every request must include the credentials, identifiers or tokens needed to process it.

This is why REST APIs typically use tokens in an Authorization header rather than server-side sessions keyed by a cookie. A stateless server can be restarted, scaled horizontally, or placed behind a load balancer without losing track of who is logged in. The trade-off is that each request carries more overhead, and token revocation becomes harder because the server no longer holds a session record.

If you find yourself adding a server-side session store to make a REST API work, you have stepped outside the constraint. That may be the right engineering choice, but call it what it is: a stateful API with REST-like URLs.

The uniform interface, broken into its four parts

The uniform interface is where most practical REST rules live. It has four sub-constraints, and each one maps to a habit you can check in code review.

Resource identification through URIs

Each resource gets a stable identifier. Use nouns, not verbs: /orders/1042, not /getOrder?id=1042. Collections are plural: /customers. A single item is /customers/57. Nested resources appear under their parent: /customers/57/orders. Keep identifiers opaque to clients so you can change them later.

Manipulation of resources through representations

Clients send a representation (usually JSON) and the server responds with one. The client should not need to know the server's internal storage. If a client must send a SQL query or a database row ID to get work done, the interface is leaking.

Self-descriptive messages

Each message includes enough metadata to be processed. The Content-Type header tells the client how to parse the body. Status codes tell the client what happened. A 201 Created response should include a Location header pointing to the new resource.

Hypermedia as the engine of application state

This is the constraint most APIs ignore. In a fully RESTful API, responses include links that tell the client what it can do next. A response for an order might include a cancel link only when the order is still cancellable. Most public APIs stop short of this and rely on documentation instead. That is a pragmatic choice, but it means the API is REST-style rather than fully RESTful.

How HTTP methods and status codes map to REST API rules

The method tells the server what to do; the status code tells the client what happened. Keep the mapping consistent and clients can predict behaviour.

  • GET retrieves a representation. It must be safe: no side effects. Return 200 OK on success, 404 Not Found if the resource is missing.
  • POST creates a subordinate resource or triggers processing. Return 201 Created with a Location header, or 200 OK if the result is not a new resource.
  • PUT replaces a resource entirely. Return 200 OK or 204 No Content. If the resource did not exist and you created it, 201 Created is correct.
  • PATCH applies a partial update. Return 200 OK with the updated representation, or 204 No Content.
  • DELETE removes a resource. Return 204 No Content on success, 404 if it was already gone.

Use 400 Bad Request for malformed input, 401 Unauthorized when credentials are missing or invalid, 403 Forbidden when the client is authenticated but not allowed, and 409 Conflict when the request clashes with the current state. Reserve 500 for genuine server faults, not for validation errors.

A small orders API

Suppose a shop needs an API for orders. A RESTful design looks like this:

  1. GET /orders returns a list. Respond 200 with a JSON array and a Cache-Control: no-store header if the list changes often.
  2. POST /orders with a JSON body creates an order. Respond 201 and set Location: /orders/1042.
  3. GET /orders/1042 returns one order. Respond 200 or 404.
  4. PATCH /orders/1042 with {"status":"cancelled"} updates one field. Respond 200 with the new representation.
  5. DELETE /orders/1042 removes it. Respond 204.

Notice that no URL contains a verb. The method carries the action, the URL names the resource, and the status code reports the outcome. A client that has never seen this API can guess most of it from the pattern.

When strict REST API rules do not fit

Some operations do not map cleanly onto resources. Search is the common case: GET /search?q=widget is a query, not a resource, and that is acceptable. Batch operations are another: a single POST /batch that accepts an array of sub-requests is a pragmatic extension, not a violation of the spirit.

RPC-style endpoints such as POST /orders/1042/cancel appear in many APIs that call themselves RESTful. They work, but they break the uniform interface because the action is in the URL. If you use them, document them clearly and keep them rare.

Versioning is not a REST constraint, but every public API needs a policy. Putting the version in the path (/v1/orders) is the most common approach because it is visible and easy to route. Header-based versioning keeps URLs clean but is harder to test in a browser.

Checking an API against the rules

Run through this list before shipping. Does every URL name a resource rather than an action? Does every request carry its own credentials? Do responses declare their content type and cacheability? Do status codes match the outcome? Are errors returned in a consistent shape with a machine-readable code and a human-readable message?

If a client can be written against the pattern without reading every endpoint's documentation, the API is following the rules well enough to be useful. REST is a design discipline, not a certificate. The goal is predictability, and the constraints are the shortest path to it.