Chapters

  • 1. Identifier Design with URIs
  • 2. Interaction Design with HTTP
  • 3. Metadata Design
  • 4. Representation Design
  • 5. Client Concerns
  • 6. Final Thoughts

Web Architecture

ConstraintCore ideaMain benefit
Client-serverSeparate the roles of client and serverIndependent development and deployment
Uniform interfaceAll components talk through standardized interfacesConsistent interoperability
Layered systemIntermediaries can sit between client and serverSecurity, caching, load balancing
CacheServers declare which responses are cacheableLower latency and server load
StatelessServer keeps no client session stateScalability

1. Client-server

  • Concerns are separated: clients and servers each have a distinct role.
  • Each side can be built and deployed independently, in any language or technology.
  • The only requirement is that both follow the uniform interface.

2. Uniform interface

This is the central constraint. If any component strays from the standards, communication breaks down. It consists of four sub-constraints.

a. Identification of resources

  • Every distinct concept on the Web is a resource with a unique identifier, such as a URI.
  • Example: a site’s home page URI identifies that site’s root resource.

b. Manipulation of resources through representations

  • Clients work with representations, never the resource itself.
  • One resource can have multiple representations (for example HTML for a browser, JSON for a program) without its identifier changing.

c. Self-descriptive messages

  • Requests can carry a desired state, and responses carry the current state.
  • The server decides whether to accept or deny a requested change.
  • Metadata (resource state, format, size, message details) travels in uniform fields such as HTTP headers.

d. Hypermedia as the engine of application state (HATEOAS)

  • Representations include links to related resources.
  • Links let users navigate in a directed way, and the presence or absence of a link is part of the resource’s current state.

3. Layered system

  • Network intermediaries such as proxies and gateways can be inserted transparently.
  • They typically intercept traffic for a specific purpose: security enforcement, response caching, or load balancing.

4. Cache

  • Servers must declare whether each response can be cached.
  • Benefits: lower perceived latency, higher availability and reliability, and reduced server load, which lowers the overall cost of the Web.
  • Caches can live anywhere on the path: inside an organization’s network, in a CDN, or in the client.

5. Stateless

  • The server does not need to remember the state of client applications.
  • Each request must carry all the context the server needs.
  • Trade-off: clients take on more complexity, but servers can handle far more clients, which is a key source of the Web’s scalability.

6. Code-on-demand (optional)

  • Servers may temporarily send executable code, such as scripts or plug-ins, to clients.
  • Downside: it couples server and client technology, since the client must be able to run the code.
  • That coupling is why it is the only optional constraint.
  • Examples: Java applets, JavaScript, Flash.

Key takeaways

  • Decoupling (client-server, uniform interface) lets each part evolve on its own.
  • Scalability comes mainly from statelessness, caching, and layered intermediaries.
  • Standardization is what holds the system together, so deviating from the uniform interface is the most damaging mistake.
  • Optionality applies only to code-on-demand, because it adds coupling.

REST API Design

Designing a REST API often feels more like an art than a science. Some best practices are implicit in HTTP, while others are pseudo-standards that emerged over time. The book offers a set of design rules that answer recurring questions with consistency, so clients can rely on them. The rules can be adopted as a full set or a la carte. Chapter 1 recaps the Web’s invention and stabilization, motivates the rule-based approach, and introduces WRML, a conceptual framework for uniform REST API design.

Rules questions

  • When should URI path segments use plural nouns?
  • Which request method should update resource state?
  • How do I map non-CRUD operations to URIs?
  • Which HTTP status code fits a given scenario?
  • How do I manage versions of a resource’s state representations?
  • How should a hyperlink be structured in JSON?

Vocabulary, regrouped by theme

1. Architecture and REST basics

TermMeaning
APIExposes data and functions so programs can interact
Architectural constraintLimits component behavior to enforce uniformity and achieve a desired property
Architectural styleFielding’s term for a set of constraints on interconnected components
RESTFielding’s derivation of the Web’s architectural style
REST API / Web APIA web service interface conforming to the Web’s architectural style / used by clients to interact with a web service
ScalabilityGracefully handling increasing workload
WRMLConceptual framework for designing and implementing uniform REST APIs

2. REST constraints

TermMeaning
Client-serverSeparates concerns so each side evolves independently
StatelessServer holds no client-specific state, so it can support more clients
CacheLets intermediaries hold representations to help servers meet demand
Layered systemIntermediaries sit between client and server without breaking the uniform interface
Uniform interfaceFour constraints that standardize communication between components
HATEOASA state-aware list of links conveying a resource’s available actions
Code-on-demandOptional: server transfers executable programs to clients as needed

3. Resources and representations

TermMeaning
ResourceAny Web concept with a unique identifier, manipulated via the uniform interface
Resource identifier / URIUniversally unique ID of a concept / the syntax Berners-Lee invented for it
Resource modelAn assembly of interlinked Web concepts
Representation (resource state representation)The formatted state of a resource, transferred between client and server
Media typeSyntax describing the form of content

4. Messages

TermMeaning
MessageSelf-descriptive envelope, often carrying a representation
Request messageSent by clients to a URI; may carry a suggested state
Response messageReturned by servers with the result; may carry a state
Entity bodyPart of an HTTP message holding optional content
Entity headersPart of an HTTP message carrying metadata about the resource and representation

5. Web technologies

TermMeaning
HypertextDocuments with embedded links, forming a navigable mesh
HypermediaHypertext extended to multiple formats tied together by links
HTMLBerners-Lee’s markup language for a resource’s information and relationships
HTTP / HTTP/1.1Message-based protocol for computer communication / its standardized version, shaped by Fielding, Berners-Lee, and others
JavaScriptPopular web scripting language
JSONStandardized text format derived from JavaScript for structured data exchange

6. Components

TermMeaning
Web componentClient, intermediary, or server that follows the uniform interface
Web client / browserProgram that accepts and sends representations / the common client type
Web serverProgram that accepts and sends representations to clients
Web serviceA server programmed with specific, often reusable, logic

Key takeaways

  • The rules aim for consistency, which clients can exploit.
  • Each rule deserves consideration even if you contest it.
  • The vocabulary falls into three layers: constraints (the principles), resources and messages (the data model), and components (the actors).

URIs

REST APIs address resources with URIs. Good URIs communicate the API’s resource model to developers, even though clients must still treat them as opaque identifiers, as Tim Berners-Lee’s axiom states. The chapter’s format rules are: use forward slashes for hierarchy, omit trailing slashes, use hyphens for readability, avoid underscores, prefer lowercase, and leave file extensions out of URIs. The guiding principle is that every character in a URI counts toward a resource’s identity, so URIs should be clean, precise, and predictable.

Background: why URI design matters

  • Good example: http://api.example.restapi.org/france/paris/louvre/leonardo-da-vinci/mona-lisa (the URI explains the resource model).
  • Bad example: http://api.example.restapi.org/68dd0-a9d3-11e0-9f1c-0800200c9a66 (opaque and hard to understand).
  • Berners-Lee’s axiom: an identifier is only for referring to an object. When not dereferencing, clients should not read the URI string for extra information.
  • Clients must follow the Web’s linking paradigm and treat URIs as opaque, but designers should still make URIs informative for client developers.
  • Generic syntax (RFC 3986): URI = scheme "://" authority "/" path [ "?" query ] [ "#" fragment ]

Rules

RuleLevelShort example
Use / to indicate hierarchymust/shapes/polygons/quadrilaterals/squares
No trailing /should notuse /shapes, not /shapes/
Use hyphens for readabilityshould/entries/this-is-my-first-post
No underscoresshould notuse my-doc, not my_doc
Prefer lowercase in pathsshould/my-folder/my-doc
No file extensionsshould notuse /fall, not /fall.json

1. Forward slash separator (/) for hierarchy (must)

  • The slash in the path shows a hierarchical relationship between resources.
  • Example: http://api.canvas.restapi.org/shapes/polygons/quadrilaterals/squares

2. No trailing forward slash (should not)

  • A trailing slash adds no semantic value and can cause confusion.
  • APIs should not expect trailing slashes and should not include them in links given to clients.
  • Many frameworks treat /shapes/ and /shapes as equal, but every character counts toward identity: different URIs mean different resources.
  • A REST API should generate clean URIs and be intolerant of imprecise client requests.
  • More forgiving APIs may redirect to the version without the trailing slash using 301 (“Moved Permanently”).

3. Hyphens ("-") for readability (should)

  • Use hyphens to make long path segments easy to scan.
  • Wherever English would use a space or hyphen, use a hyphen in the URI.
  • Example: http://api.example.restapi.org/blogs/mark-masse/entries/this-is-my-first-post

4. No underscores ("_") (should not)

  • Viewers often underline URIs to signal they are clickable.
  • Depending on the font, the underline can partially or fully hide an underscore.
  • Use hyphens instead.

5. Prefer lowercase letters in paths (should)

  • RFC 3986 says URIs are case-sensitive except for the scheme and host.
  • Capital letters in paths can therefore cause problems.
URIStatus
http://api.example.restapi.org/my-folder/my-docFine
HTTP://API.EXAMPLE.RESTAPI.ORG/my-folder/my-docIdentical to the first (scheme and host are case-insensitive)
http://api.example.restapi.org/My-Folder/my-docDifferent from the first two, which may confuse

6. No file extensions (should not)

  • Do not use .json or similar to indicate the format of the message body.
  • Rely on the media type in the Content-Type header to decide how to process content.
  • Clients should select formats with the HTTP Accept request header (media type negotiation).
  • Bad: .../transcripts/2005/fall.json
  • Good: .../transcripts/2005/fall
  • Note: for simple links and easy debugging, an API may support media type selection through a query parameter.

Key takeaways

  • Identity: every character in a URI matters, so be consistent and precise.
  • Readability: hyphens and lowercase help humans, while underscores and capitals cause problems.
  • Format selection: leave format to HTTP headers, not to the URI.
  • Opaque but informative: clients should not parse URIs, yet good design helps developers understand the resource model.