Beginner · APIs and request handling · topic 7 of 54 · 25 min
What an API is and how REST APIs are designed
Explanation
What is an API?
API means Application Programming Interface. It is a list of requests a program accepts and the answers it gives back. The app on your phone does not open the database itself. It talks to the backend through its API.
REST: resources as nouns
REST (REpresentational State Transfer) is a popular style for designing web APIs. In REST, every kind of data is a resource: users, products, orders. Each resource has a URL. The URL is a noun (a thing), and the HTTP method is the verb (the action).
| Request | Action | Success code |
|---|---|---|
| GET /products | List all products | 200 |
| GET /products/42 | Read product 42 | 200 |
| POST /products | Create a new product | 201 |
| PUT /products/42 | Replace product 42 completely | 200 |
| PATCH /products/42 | Change some fields of product 42 | 200 |
| DELETE /products/42 | Delete product 42 | 204 |
A collection is the whole list (/products). An item is one member of it (/products/42). The number at the end is the id, a unique value that names one record. Related data can be nested: /users/42/orders means "the orders of user 42".
Good names and bad names
| Avoid | Use instead | Why |
|---|---|---|
| GET /getUsers | GET /users | The method already says "get" |
| POST /createOrder | POST /orders | POST on a collection means create |
| GET /deleteUser?id=5 | DELETE /users/5 | GET must never change data |
| /user/5 and /orders | /users/5 and /orders | Use plural names everywhere, consistently |
| /orderItems or /Order_Items | /order-items | Small letters, words joined with hyphens |
Filtering and versions
To filter, sort or page through a list, use the query string on the collection, not new URLs: GET /products?category=rice&maxPrice=200&sort=price&page=2. One URL, many options.
APIs change over time, but old apps on people's phones still call the old API. So many APIs put a version at the start of the path: /v1/products. When you must make a breaking change, you add /v2/products and keep /v1 working for a while.
function describe(method, path) {
const parts = path.split('/').filter(Boolean); // ['products', '42']
const isItem = parts.length === 2;
return method + ' on ' + (isItem ? 'one ' + parts[0] + ' item (id ' + parts[1] + ')' : 'the ' + parts[0] + ' collection');
}
console.log(describe('GET', '/products')); // GET on the products collection
console.log(describe('DELETE', '/products/42')); // DELETE on one products item (id 42)Watch the lesson
What an API is and how REST APIs are designed · 1/6
An API is like a waiter. You order from the menu in a fixed way, and the kitchen sends back your food.
Prefer a video by a teacher? Find free videos on “rest api design best practices for beginners” on YouTube ↗
In real life: The API behind a college results site
A results site could have GET /v1/students/21CS045 for one student, GET /v1/students/21CS045/results for that student's marks, and GET /v1/results?semester=4&branch=CSE for a filtered list. Teachers upload marks with POST /v1/results, and fix a wrong mark with PATCH /v1/results/9981. Anyone who learns one URL can guess the others.
Practice (0/3 done)
Write a function actionFor(method, path) for paths like "/products" (a collection) or "/products/42" (one item). Return "list" for GET on a collection, "create" for POST on a collection, "read" for GET on an item, "replace" for PUT on an item, "update" for PATCH on an item, "delete" for DELETE on an item, and null for anything else.
Write a function isRestful(path) that checks a URL path. Every part between slashes must be either digits (an id), a version like "v1", or a name in small letters and hyphens. A name must not be a verb or start with a verb and a hyphen. The verbs are: get, create, update, delete, add, remove, fetch. Return true or false.
Design the REST endpoints for a small library app with books and members. Members can borrow books. Write each endpoint as METHOD /path and one line saying what it does and which success code it returns.
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.