APIs & REST
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 things | URLs naming actions |
|---|---|
| GET /bikes/42 | GET /getBike?id=42 |
| POST /rentals | POST /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.
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.
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"}{"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.
Keep old ferry apps working
Version 2 requires a vehicle field, but 30% of phones still use version 1.
Keep /v1/bookings working until old apps are updated. Both API versions call the same booking code after translating their requests.
The old app sends requests to /v1/bookings, and the new app uses /v2/bookings. Both routes lead to the booking service.
The insight
Keep promises to installed apps; give breaking changes a new API version.
Page through irrigation readings
New readings arrive every minute for irrigation field 12.
Cursor pagination
- In plain English
- Cursor pagination returns a bookmark for your position in an ordered collection so the next page continues after it.
- Think of it like
- It is like a bookmark that stays on your page when new pages are added at the front.
Request /fields/12/readings?limit=100&cursor=abc. The cursor bookmarks a timestamp and unique reading ID. New readings before that bookmark do not shift your next page.
A cursor marks a place, not a frozen copy of the data. Decide how to handle edits to existing readings.
The insight
Choose a paging method that handles incoming data without losing your place.
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
Takeaways
- An API is a promise between programs.
- REST = resources (nouns) + HTTP methods (verbs).
- Version when you must break the promise.