An API (application programming interface) is how one program talks to another. When a mobile app shows your orders, it asks the server's API for them; when a website takes a payment, it calls a payment company's API. Most web APIs follow a style called REST, and designing a clean one is a core skill for backend developers and a standard part of system design interviews.
This lesson explains REST in plain language and shows how to design endpoints, choose methods and status codes, shape JSON, and handle pagination, filtering, versions and retries.
What makes an API "REST"?
REST (representational state transfer) is a set of conventions on top of HTTP. The main ideas:
- Everything is a resource, a thing with a name: a user, an order, a product.
- Each resource has a URL, such as
/orders/42. - HTTP methods say what to do with it:
GETto read,POSTto create, and so on. - Requests are stateless: each one carries everything the server needs, such as a login token. The server doesn't remember earlier requests, which makes scaling out much easier.
- Data is usually JSON.
Resources and URLs
Name URLs after things (nouns), not actions (verbs). Use plural nouns and lowercase with hyphens:
| Good | Avoid | Why |
|---|---|---|
GET /orders | GET /getAllOrders | The method already says "get" |
GET /orders/42 | GET /order?id=42 | The ID identifies the resource |
POST /orders | POST /createOrder | POST to a collection creates |
GET /users/7/orders | GET /ordersOfUser/7 | Nesting shows the relationship |
Keep nesting shallow: /users/7/orders is clear, but /users/7/orders/42/items/3/reviews is hard to use. Once an item has its own ID, give it a top-level URL: /items/3.
HTTP methods
| Method | Does | Example | Safe? | Idempotent? |
|---|---|---|---|---|
GET | Read | GET /orders/42 | Yes | Yes |
POST | Create | POST /orders | No | No |
PUT | Replace completely | PUT /orders/42 | No | Yes |
PATCH | Update some fields | PATCH /orders/42 | No | Usually |
DELETE | Remove | DELETE /orders/42 | No | Yes |
- Safe means it doesn't change anything.
GETmust never change data, because browsers, caches and crawlers sendGETrequests freely. - Idempotent means doing it twice has the same effect as doing it once. Deleting order 42 twice leaves it deleted; creating an order twice makes two orders. This matters when a network glitch makes a client retry a request.
Status codes
Return a status code that tells the client what happened, so it can react without reading the message:
| Code | Use it when |
|---|---|
200 OK | A successful read or update, with a body |
201 Created | POST created something; include its URL in a Location header |
204 No Content | Success with nothing to return, such as DELETE |
400 Bad Request | The input is invalid: missing field, wrong type |
401 Unauthorized | Not logged in, or the token is missing or expired |
403 Forbidden | Logged in, but not allowed to do this |
404 Not Found | The resource doesn't exist |
409 Conflict | Clashes with current state, such as a duplicate email |
429 Too Many Requests | The client hit a rate limit |
500 Internal Server Error | A bug or failure on the server |
The rule of thumb: 4xx means the client should change something; 5xx means the server failed. Never return 200 with {"error": "..."} in the body: monitoring tools and clients will think it worked.
Request and response bodies
Use JSON, consistent names (pick snake_case or camelCase and stick to it), and ISO dates:
POST /orders
Content-Type: application/json
Authorization: Bearer eyJhbGciOi...
{
"items": [{"product_id": 501, "quantity": 2}],
"delivery_pincode": "208001"
}
HTTP/1.1 201 Created
Location: /orders/9031
{
"id": 9031,
"status": "placed",
"total": 1798,
"currency": "INR",
"created_at": "2026-12-02T10:15:00Z"
}
Give errors a predictable shape too, so clients can show helpful messages:
{
"error": {
"code": "invalid_pincode",
"message": "Pincode must be 6 digits.",
"field": "delivery_pincode"
}
}
Store money as whole paise or as a decimal string, never as a float: floats can't store most decimal amounts exactly.
Pagination
Never return an unlimited list. A shop with a million orders would send a gigantic response. Return one page at a time:
Offset pagination is simple: GET /orders?limit=20&offset=40 gives orders 41 to 60. But it gets slow on deep pages, because the database still counts past all the skipped rows, and items can be skipped or repeated if new ones arrive while someone is paging.
Cursor pagination is better for large or changing data. The response includes a pointer to where the next page starts:
{
"data": [ ... 20 orders ... ],
"next_cursor": "b3JkZXJfOTAxMQ"
}
The client asks for GET /orders?limit=20&cursor=b3JkZXJfOTAxMQ. Infinite-scroll feeds work this way.
Filtering, sorting and searching
Use query parameters on the collection:
GET /products?category=books&max_price=500&sort=-rating
A - before a field commonly means "descending". Only allow filters and sorts the database can handle quickly, usually fields with an index.
Versioning
Once apps depend on your API, changing it can break them, and you can't force every phone to update. Put a version in the URL, /v1/orders, and when you must make a breaking change (removing or renaming a field), release /v2/orders while keeping /v1 running for a while.
Adding new optional fields is not a breaking change, so you can do that without a new version.
Retries and idempotency keys
Here's a real problem. A user taps "Pay". The request reaches the server and the payment succeeds, but the response is lost on a weak mobile network. The app retries, and the user is charged twice.
The fix is an idempotency key: the client generates a unique ID for each action and sends it with the request:
POST /payments
Idempotency-Key: 6f1c2a9e-7d4b-4e0a-9b1c-2f3d4e5f6a7b
The server stores the key with the result. If the same key arrives again, it returns the stored result instead of charging again. Payment APIs use exactly this approach.
Worked example: a library API
Design an API for a college library: books, members and loans.
| Action | Endpoint |
|---|---|
| List books, search by title | GET /v1/books?title=python&limit=20 |
| Get one book | GET /v1/books/{book_id} |
| Add a book (librarian) | POST /v1/books → 201 |
| Borrow a book | POST /v1/loans with {"book_id": 12, "member_id": 7} → 201, or 409 if already borrowed |
| Return a book | PATCH /v1/loans/{loan_id} with {"returned_at": "..."} |
| A member's loans | GET /v1/members/{member_id}/loans |
Notice the trick in the middle: "borrow" sounds like a verb, but modelling it as creating a loan keeps the design noun-based and gives each loan an ID, a history and a due date.
REST isn't the only option
| Style | Good for |
|---|---|
| REST | Public and general-purpose APIs; simple, cacheable |
| GraphQL | Apps that need flexible queries, fetching exactly the fields they want in one request |
| gRPC | Fast communication between internal services |
| WebSockets | Real-time two-way updates, such as chat |
Practice
- Design the endpoints to like and unlike a post.
Show the answer
Model a "like" as a resource: PUT /posts/{id}/likes/me to like and DELETE /posts/{id}/likes/me to unlike. Both are idempotent, so double taps are harmless. POST /posts/{id}/likes also works but needs duplicate handling.
- Which status code for: (a) wrong password, (b) a student trying to open the admin page, (c) signing up with an email already in use?
Show the answer
(a) 401 Unauthorized, (b) 403 Forbidden, (c) 409 Conflict (some APIs use 400 or 422).
- What's wrong with
GET /deleteUser?id=5?
Show the answer
It uses a verb in the URL, and a GET that changes data. A browser prefetch, crawler or cache could delete the user by accident. Use DELETE /users/5.
Interview questions
What is REST?
An architectural style for APIs over HTTP: resources are identified by URLs, manipulated with standard methods (GET, POST, PUT, PATCH, DELETE), represented as JSON or similar, and requests are stateless.
What is the difference between PUT and PATCH?
PUT replaces the whole resource with the request body, and is idempotent. PATCH applies a partial update to some fields.
What does idempotent mean, and why does it matter?
Repeating the request has the same effect as making it once. Clients retry on timeouts, so idempotent operations, or idempotency keys for non-idempotent ones like payments, prevent duplicates.
Offset vs cursor pagination?
Offset (limit and offset) is simple and allows jumping to page n, but gets slow for deep pages and can skip or repeat items when data changes. Cursor pagination uses a pointer to the last item, which stays fast and consistent, but only supports next and previous.
What to learn next
- Measure how fast your API is in latency, throughput and availability.
- Protect it from overload with rate limiting.