An API is RESTful when it follows the REST architectural constraints: client–server, statelessness, cacheability, a uniform interface, a layered system, and optionally code on demand.
- Client–server separation: the client (UI) and server (data and logic) are independent and communicate only through requests and responses, so each can evolve separately.
- Stateless: every request carries all the information needed to process it (e.g. an auth token); the server stores no client session between requests.
- Cacheable: responses must say whether they can be cached (e.g.
Cache-Control,ETagheaders) to improve performance. - Uniform interface:
- Resources are identified by URIs, e.g.
/students/42. - Resources are manipulated through representations (JSON, XML) using standard HTTP methods: GET (read), POST (create), PUT/PATCH (update), DELETE (remove).
- Self-descriptive messages: correct status codes (200, 201, 404, 500) and headers such as
Content-Type. - HATEOAS: responses can include links to related actions/resources.
- Resources are identified by URIs, e.g.
- Layered system: the client does not need to know whether it talks to the actual server or to intermediaries such as load balancers, proxies or caches.
- Code on demand (optional): the server may send executable code (e.g. JavaScript) to extend the client.
Example of RESTful endpoints
GET /api/books list books
GET /api/books/7 get book 7
POST /api/books create a book
PUT /api/books/7 replace book 7
DELETE /api/books/7 delete book 7
Good practice also includes using nouns (not verbs) in URIs, versioning (/api/v1/), and idempotent PUT/DELETE.