Skip to main content

Web and APIs

What is a REST API?

Definition

A REST API is a web API that exposes data as URL-addressed resources and uses standard HTTP methods such as GET, POST, PUT and DELETE to act on them.

Also known as: RESTful API, REST

REST API in more detail

REST — representational state transfer — was described by Roy Fielding in 2000 as an architectural style, not a specification. Its constraints are that every resource has its own identifier (a URL), that clients and servers are decoupled, that each request carries everything needed to serve it (statelessness), that responses say whether they can be cached, and that the interface is uniform across the whole API rather than bespoke per endpoint.

In practice that produces a very recognisable shape. GET /articles lists articles, GET /articles/12 returns one, POST /articles creates one, PUT /articles/12 replaces it, PATCH /articles/12 edits part of it, DELETE /articles/12 removes it. The HTTP status code carries the outcome — 200 for success, 201 with a Location header for a creation, 404 for a resource that does not exist, 422 for a body that failed validation.

It is worth being honest about the gap between the style and the label. Fielding's model also requires hypermedia controls, where responses link to the actions available next, and almost no API described as REST implements that. What most teams ship is a JSON-over-HTTP API with resource-shaped URLs, which is a perfectly good design — it is just not the full set of constraints the acronym originally named.

REST API example

POST /v1/articles          201 Created
GET  /v1/articles          200 OK   (a list)
GET  /v1/articles/12       200 OK   (one article)
PATCH /v1/articles/12      200 OK   (partial update)
DELETE /v1/articles/12     204 No Content
GET  /v1/articles/999      404 Not Found

# Filtering, sorting and paging live in the query string,
# never in the path:
GET /v1/articles?author=12&sort=-published_at&page=2

REST API — common questions

What is the difference between REST and HTTP?
HTTP is the protocol; REST is a way of using it. REST says to model your data as resources with URLs and to use HTTP's existing methods and status codes to act on them, instead of inventing your own verbs inside the request body.
Is REST better than GraphQL?
Neither is better in general. REST is simpler to cache, to debug and to operate, and it fits well when clients want whole resources. GraphQL wins when many different clients need different subsets of a large, highly connected graph of data.
What does 'stateless' mean in REST?
It means the server keeps no per-client conversation state between requests, so each request must carry its own authentication and context. Any two identical requests can be handled by any two servers, which is what makes horizontal scaling straightforward.

Keep reading

Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.

Browse the A-Z glossary