REST APIs
A bookshop API in seven steps: every book gets a URL, HTTP methods say what to do with it, JSON carries its state, and a status code reports how it went.
In 30 seconds
REST is a common style for web APIs. Everything the API offers is a resource with its own URL, such as /books/42.
The HTTP method is the verb: GET reads, POST creates, PUT replaces, DELETE removes. The data travels as a representation of the resource, usually JSON, and a status code such as 200 or 404 reports the result.
Requests are stateless: each one carries everything the server needs, so the server keeps no memory of earlier requests.
REST APIs, step by step
REST was defined by Roy Fielding in 2000. His full definition also asks for cacheable responses and for links between resources (hypermedia), which many APIs that are called REST leave out.
- Step 1: Resources and URLs. A REST API is organised around things, called resources: a book, a customer, an order. Every resource gets its own address, a URL. Here /books names the whole collection and /books/42 names one book in it. The URL says which thing you mean, and nothing about what to do with it.
- Step 2: Methods are the verbs. The URL is the noun; the HTTP method is the verb. A small fixed set of methods covers almost everything: GET reads a resource, POST adds a new one to a collection, PUT replaces one, DELETE removes one. (PATCH changes only part of one.) The same few verbs work on every resource, so a new API feels familiar.
- Step 3: A representation in JSON. The book itself, however the server stores it, never leaves the server. What travels is a representation: a description of the resource’s current state, usually written as JSON, a simple text format of names and values. The client reads it, and can send a changed representation back to update the book.
- Step 4: Creating with POST. To add a book, the client POSTs a representation of it to the collection. The server stores it, chooses the new id, and answers 201 Created with a Location header holding the new book’s URL. The collection now has one more resource, with an address of its own.
- Step 5: Status codes. Every response starts with a three-digit status code, so the client knows what happened without reading the body. The first digit gives the family: 2xx success, 3xx look elsewhere, 4xx the request was at fault, 5xx the server was. Asking for a book that does not exist gets 404 Not Found.
- Step 6: Stateless requests. The server stores the books, but it keeps no memory of the conversation: nothing like “this client logged in a minute ago”. Every request carries all that is needed to understand it, including proof of who is asking. That is why the work can be spread over many identical servers, and any one of them can answer.
- Step 7: What makes it RESTful. REST is a style, not a standard, and APIs follow it to different degrees. An API is called RESTful when it names resources with URLs, uses the HTTP methods for what they mean, exchanges representations, keeps requests stateless and reports results with status codes. Putting the action into the URL instead is a different style.
Try it yourself
Learn this first
Spotted a mistake on this page?