REST (Representational State Transfer)
REST is an architectural style for web APIs: resources have an address of their own, clients work on them with the standard methods of HTTP, and every request stands on its own, without the server keeping state between requests.
REST (representational state transfer) is an architectural style for web APIs. An API describes resources, each with an address of its own, and clients work on them with the standard methods of HTTP. Every request stands on its own; the server keeps no state between two requests.
The building blocks
Microsoft's documentation on designing RESTful web APIs names these concepts:
- URI: Every resource, such as an order, has a unique address like
/orders/1. - Representation: The client gets the resource in a format such as JSON or XML, not the resource itself.
- Uniform interface: The operations are the HTTP methods
GET,POST,PUT,PATCH, andDELETE. This couples client and service loosely. - Statelessness: Requests are independent and can arrive in any order. That makes scaling easier.
- Hypermedia links: An answer can contain links to related resources.
From this follow rules of thumb for addresses: name collections in the plural (/customers, /customers/5), keep relationships simple (at most collection/item/collection), no verbs in the address, and do not design an API as a mirror of the database tables. The method names the action: POST /api/appointments creates, DELETE /api/appointments/7 deletes.
Whether an API is "really" REST is disputed. Richardson's maturity model distinguishes four levels: one address for everything, addresses of their own per resource, HTTP methods for the operations, and finally hypermedia. Many published APIs are at about level 2. In practice, that is good enough when the API is designed cleanly.
REST in ASP.NET Core
With minimal APIs, a REST API in ASP.NET Core looks like this (the types are placeholders):
var appointments = app.MapGroup("/api/appointments");
appointments.MapGet("/{id:int}", async (int id, IAppointmentStore store) =>
await store.FindAsync(id) is { } appointment
? Results.Ok(appointment)
: Results.NotFound());
appointments.MapPost("/", async (NewAppointment input, IAppointmentStore store) =>
{
var created = await store.AddAsync(input);
return Results.Created($"/api/appointments/{created.Id}", created);
});
Sensible status codes, errors as ProblemDetails, and a description in the OpenAPI format belong to it. With OpenAPI, the contract can be designed first ("contract first"), and documentation and client libraries can be generated from it. For lists, paging and filters belong in the API so that answers stay small.
An example from the Kladde
The Kladde, the sample application of a small IT office, has an API for contacts and appointments. In the course, it is hardened for outside callers: a list that does not swallow an appointment while paging, two people who do not overwrite each other's changes (ETag and If-Match), and a booking that is booked only once when it arrives twice (Idempotency-Key).
Common misconceptions
- REST is not a protocol. It is a style that uses HTTP well.
- REST is not the same as JSON. JSON is a format, REST a way of cutting resources.
- REST is not always the best choice. For real-time communication there are other ways, such as WebSocket.
- An API is not automatically REST. A method like
POST /api/createAppointmentis not.
In the course
You learn this in K2, module 2.12, in the lessons REST done right: resources, methods, state and Contract first: OpenAPI. You already build the first API in module 2.3 in The first web API.
Further reading on the blog
Sources
- Microsoft Learn, Best practices for RESTful web API design: https://learn.microsoft.com/azure/architecture/best-practices/api-design