Skip to content

Beginner · APIs and request handling · topic 7 of 54 · 25 min

What an API is and how REST APIs are designed

Explanation

An API is a set of rules that lets one program ask another program for data or actions. A REST API treats data as resources with plural noun URLs like /users and /users/42, and uses the HTTP method to say what to do. Clear, predictable URLs make an API easy to learn and hard to misuse.

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.

Think of it like this: An API is like a waiter in a restaurant. You do not walk into the kitchen. You read the menu (the API documentation), tell the waiter your order in a fixed way (a request), and the waiter brings back your food (a response). The kitchen can change its stoves, and you never notice, as long as the menu stays the same.

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).

RequestActionSuccess code
GET /productsList all products200
GET /products/42Read product 42200
POST /productsCreate a new product201
PUT /products/42Replace product 42 completely200
PATCH /products/42Change some fields of product 42200
DELETE /products/42Delete product 42204

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

AvoidUse insteadWhy
GET /getUsersGET /usersThe method already says "get"
POST /createOrderPOST /ordersPOST on a collection means create
GET /deleteUser?id=5DELETE /users/5GET must never change data
/user/5 and /orders/users/5 and /ordersUse plural names everywhere, consistently
/orderItems or /Order_Items/order-itemsSmall letters, words joined with hyphens
Tip: Simple rule: nouns in the URL, verbs in the method. If you are about to write a verb like get, create or delete in a URL, use the right method instead.

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.

javascript
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)
Splitting the path tells you whether the request is about the whole collection or one item.
Careful: Do not change what an existing endpoint returns (renaming or removing fields) without a new version. Old app versions on users' phones will break, and you cannot force everyone to update.

Watch the lesson

What an API is and how REST APIs are designed · 1/6

API = the waiter Request = your order Response = your food

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)

Task 1 · Code · Javascript

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.

Task 2 · Code · Javascript

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.

Task 3 · Practice

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

Question 1

1.Which URL follows REST naming best?

Question 2

2.How do you ask for the orders of user 42 in REST style?

Question 3

3.You want only rice products under 200 rupees. What is the REST way?

Question 4

4.Why do APIs put /v1 at the start of paths?

Question 5

5.In the waiter analogy, what is the menu?

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.

← Previous: JSON: the language of APIs