Find a course

Module 3: REST APIsLesson 5 of 1213 min

REST: Resources, URLs and Representations

Learn the ideas behind REST: resources identified by URLs, representations in JSON, stateless requests and a uniform interface built on standard HTTP methods.

Step 1 of 13Learn

Two ways to design the same API

Imagine two teams building an API for a library.

Team A names an endpoint after every action: /getBook?id=7, /createNewBook, /deleteBookById?id=7, /listAllBooksForMember?member=3, /renewTheLoan. Every endpoint is a special case, and developers must read the docs for each one.

Team B uses REST. Everything is a resource with an address, and the same few HTTP methods act on all of them:

GET    /books           list books
POST   /books           create a book
GET    /books/7         read book 7
PATCH  /books/7         update part of book 7
DELETE /books/7         delete book 7
GET    /members/3/loans list member 3's loans

Once you've seen one Team B endpoint, you can guess the rest. That predictability is REST's biggest practical benefit.

REST stands for representational state transfer. It was described in 2000 by Roy Fielding, one of the authors of the HTTP specification, as the architectural style that made the web itself scale. A "RESTful" API applies the same ideas to data: resources identified by URLs, manipulated through a small, uniform set of operations, with each request carrying everything needed to understand it.

In practice, "REST API" usually means "a JSON-over-HTTP API that uses resource URLs and HTTP methods sensibly". Few APIs follow every constraint Fielding described, but the core ideas in this lesson are what matter day to day.

Step 2 of 13Learn

Resources and representations

A resource is any thing the API exposes that's worth naming: a book, a member, a loan, a search result, even the current weather in a city. Each resource has an identifier, its URL.

Two common kinds:

  • Collections: a set of resources, named with a plural noun: /books, /members.
  • Items (or elements): one member of a collection, identified by ID: /books/7.

Relationships appear as nested paths: /members/3/loans means "the loans that belong to member 3". Keep nesting shallow; one level is usually enough. Deep paths like /libraries/2/branches/5/shelves/9/books/7 are brittle and hard to use.

The server never sends the resource itself, which might be a row in a database. It sends a representation of the resource's current state, usually JSON:

{ "id": 7, "title": "Tide Lines", "author": "M. Haddad", "available": true }

The same resource can have several representations: JSON for apps, CSV for a data export, HTML for a browser. The client says which it prefers with the Accept header. That's the "representational" in REST, and the "state transfer" is the client and server passing representations of state back and forth.

Naming rule of thumb: URLs are nouns, methods are verbs. /books/7 with DELETE, not /deleteBook/7.

Step 3 of 13Practice · Sort into groups

Which endpoint designs follow REST naming conventions, and which don't?

Tap an item, then tap its group (or drag it there).

Step 4 of 13Diagram

A library API as a tree of resources

Collections are plural nouns; one item is the collection plus its ID; related items nest one level below.

A library API as a tree of resourcesHierarchy diagram: api.library.example, /books, /books/7, /books/7/reviews, /members, /members/3, /members/3/loans.api.library.example/books/books/7/books/7/reviews/members/members/3/members/3/loans

Tap a shape with to see more.

api.library.example
/books

The collection of all books.

/books/7

One book, identified by its ID.

/books/7/reviews

Reviews belonging to book 7.

/members
/members/3
/members/3/loans

Loans held by member 3.

Step 5 of 13Learn

Stateless requests

A central REST constraint is statelessness: each request must contain everything the server needs to process it. The server keeps no memory of earlier requests from that client between calls.

That means no "step 1: select book 7, step 2: delete the selected book". Instead, every request is complete on its own: DELETE /books/7 with an Authorization header that identifies the caller.

Why insist on it?

  • Scaling: if no request depends on server memory, any of 50 identical servers can handle any request. A load balancer can spread traffic freely and replace failed machines without losing anyone's session.
  • Reliability: a request can be retried on another server after a failure.
  • Simplicity: each request can be understood, logged and debugged in isolation.

Stateless doesn't mean the server stores nothing. The resources (books, orders, accounts) live in a database, of course. What's forbidden is session state about the conversation itself. Authentication is the typical example: rather than "logging in" once and relying on the server to remember, the client sends a token with every request.

Two other REST constraints are worth knowing by name. Responses should say whether they're cacheable, so clients and proxies can reuse them. And the system can be layered: the client doesn't know or care whether it's talking to the real server or to a cache or gateway in front of it.

Step 6 of 13Practice · What would you do?

What would you do?

A mobile app's API works like this: the app first calls POST /select-account with an account ID, and the server remembers the selection in memory. Then the app calls GET /balance, which returns the balance of "the selected account". Users report seeing other people's balances during busy periods, when traffic is spread over several servers. What's the REST-style fix?

Step 7 of 13Flashcards

REST vocabulary

Tap a card to flip it.

Step 8 of 13Note

Step 9 of 13Learn

Caching: the forgotten REST superpower

Because REST uses standard HTTP, it inherits HTTP's built-in caching. A server can tell clients and intermediate caches how long a response stays fresh:

Cache-Control: public, max-age=300
ETag: "v7-4f2a"

max-age=300 means the response may be reused for 300 seconds without asking the server again. A product catalog that changes hourly can be served from caches near users for most requests, cutting load and speeding up apps.

When the time is up, the client doesn't need to download everything again. It sends a conditional request with the ETag it has:

GET /products/42
If-None-Match: "v7-4f2a"

If nothing changed, the server replies 304 Not Modified with no body, and the client keeps using its copy. Only if the product changed does a full 200 response come back.

Caching is why GET requests must be safe and why REST favors stable URLs for resources: a cache can only reuse what it can identify. Responses with private data should be marked Cache-Control: private or no-store so shared caches never keep them.

Step 10 of 13Practice · Work it out

A response is sent with Cache-Control: max-age=600. For how many minutes may a client reuse it without contacting the server?

Step 11 of 13Diagram

How an HTTP cache answers a request

How an HTTP cache answers a requestProcess diagram: Client asks for /books/7 → Cache has a copy? → Still fresh? → Stale: revalidate → 304 or new 200.Client asks for/books/7Cache has a copy?Still fresh?Stale: revalidate304 or new 200

Tap a shape with to see more.

Client asks for /books/7
Cache has a copy?

If not, the request goes straight to the server.

Still fresh?

Younger than max-age: serve the copy with no network call at all.

Stale: revalidate

Ask the server with If-None-Match and the saved ETag.

304 or new 200

304 Not Modified: reuse the copy. 200: replace it with the new version.

Step 12 of 13Practice · True or false

In REST, an endpoint such as /books/7 identifies a resource, and the HTTP method (GET, PATCH, DELETE) says what to do with it.

Step 13 of 13Practice · Choose one

Which URL best represents "all reviews written for product 55" in a RESTful API?