AI Hints
Home›System Design Tutorial›Designing APIs
System Design · Lesson 16 of 120

REST APIs

Resources and verbs.

⏱ 2 min read▶ Video at 18:08🎓 Free
Watch this lesson in the System Design courseWatch on YouTube ↗Full System Design course

An API is how one program talks to another. On the web, the most common style is REST. You think in resources, like products and orders, and act on them with HTTP verbs.

REST APIs
From the video: REST APIs
  • Programs talking to programs
  • REST: resources
  • Plus HTTP verbs

Verbs and resources

Here's Shoply's API in REST style. Get slash products lists products. Get slash products slash forty two fetches one product. Post slash orders creates an order. Put or patch updates one. And delete removes it. The path names the thing, and the verb says what to do.

RequestMeaning
GET /productslist products
GET /products/42one product
POST /orderscreate an order
PATCH /orders/7update order 7
DELETE /orders/7cancel order 7

A REST response

Responses are usually J SON, a simple text format for data. Here we build the J SON a server might return for one product. Run it. It's easy to read for humans, and easy to parse for any programming language. That's a big reason REST and J SON became the default.

response.pypython
import json

product = {
    "id": 42,
    "name": "Blue Mug",
    "price": 12.5,
    "in_stock": True,
}
print("HTTP/1.1 200 OK")
print("Content-Type: application/json")
print()
print(json.dumps(product, indent=2))
Output
HTTP/1.1 200 OK
Content-Type: application/json

{
  "id": 42,
  "name": "Blue Mug",
  "price": 12.5,
  "in_stock": true
}

Summary

REST uses nouns for resources, and HTTP verbs for actions, usually with J SON. Next, what separates a good API from a painful one.

Key takeaways
  • Nouns in the path
  • Verbs say what to do
  • JSON responses
Next up: Good API Design — names, versions, errors