System Design Academy

Saving…

Lesson 7 of 10 in Module 10 of 54 lessons complete

APIs & REST

Module 1, lesson 7Concept10 min

Definition

API

In plain English
An API is a defined set of requests a program accepts and the responses it promises to return.
Think of it like
It is like a restaurant menu, where you choose from listed items without entering the kitchen.

REST

In plain English
REST is a style of API design where everything is a resource with its own URL and HTTP methods say what to do with it.
Think of it like
It is like a library where every book has a shelf number and you can borrow, return or reshelve it.

Why it exists

Your app and server teams release updates separately. Agree on requests and responses so server changes do not break installed apps.

REST uses familiar HTTP methods and status codes. Clients reuse saved responses when the headers allow it.

How it works

Resource

In plain English
A resource is any thing your API exposes, such as a bike, a dock or a rental.
Think of it like
It is like an item in a catalogue.

JSON

In plain English
JSON is a lightweight text format for structured values including objects, arrays, strings and numbers.
Think of it like
It is like a form with labelled boxes.
URLs naming thingsURLs naming actions
GET /bikes/42GET /getBike?id=42
POST /rentalsPOST /createRental
  • Use nouns in URLs and HTTP methods for actions.
  • Filter lists with values in the URL, such as ?near=51.5,-0.12.
  • Return long lists one page at a time.
  • Give breaking changes a separate version, such as /v1/ and /v2/.

The docks list leads to dock 7, then to that dock's bikes. Separately, the rentals list leads to rental 9.

Resource URLs identify collections, individual records and related collections.

The docks list leads to dock 7, then to that dock's bikes. Separately, the rentals list leads to rental 9.

The request creates a rental and the response identifies the new resource.
HTTP
POST /rentals HTTP/1.1
Host: bikes.example
Content-Type: application/json

{"bikeId":"B42"}

HTTP/1.1 201 Created
Location: /rentals/9
Content-Type: application/json

{"id":"9","bikeId":"B42"}

Examples

Model bike rentals as resources

Your bike-share app finds docks, starts rentals and records returns.

Exchange excerpts: find nearby docks, then update an existing rental.
HTTP
GET /docks?near=51.5,-0.12 HTTP/1.1
Host: bikes.example

PATCH /rentals/9 HTTP/1.1
Host: bikes.example
Content-Type: application/json

{"status":"returned"}
The JSON request body for POST /rentals.
JSON
{"bikeId":"B42"}

Filter docks by location. A rental records one borrowing event. Returning the bike updates that rental, so use its URL.

The insight

Resources and methods make the API guessable.

Trade-offs

Pros

  • Expose familiar readable requests.
  • Support browsers and partners.
  • Reuse GET responses when headers allow it.

Cons

  • Some screens need several calls or receive unused fields.
  • You must check message fields and types yourself.
  • Text JSON can be bulky.

Use when

  • Build public or partner APIs.
  • Model create, read, update and delete operations.

Avoid when

  • Measurements show frequent internal calls need a faster format.
  • Continuous server push is central.

Quick check

Most RESTful way to cancel booking 88?
Why is cursor pagination better than page numbers for constantly arriving sensor readings?

Takeaways

  • An API is a promise between programs.
  • REST = resources (nouns) + HTTP methods (verbs).
  • Version when you must break the promise.

Try the quick check above whenever you feel ready.

Previous: HTTPS & TLS