REST APIs
Resources and verbs.
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.

- 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.
| Request | Meaning |
|---|---|
| GET /products | list products |
| GET /products/42 | one product |
| POST /orders | create an order |
| PATCH /orders/7 | update order 7 |
| DELETE /orders/7 | cancel 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.
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))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