Module 1 · Beginner · System design fundamentals

REST API design basics, with examples

REST API design for beginners: resources and URLs, HTTP methods, status codes, JSON bodies, pagination, filtering, versioning and idempotency, with examples.

By theprogrammers team · · 7 min read

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: GET to read, POST to 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:

GoodAvoidWhy
GET /ordersGET /getAllOrdersThe method already says "get"
GET /orders/42GET /order?id=42The ID identifies the resource
POST /ordersPOST /createOrderPOST to a collection creates
GET /users/7/ordersGET /ordersOfUser/7Nesting 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

MethodDoesExampleSafe?Idempotent?
GETReadGET /orders/42YesYes
POSTCreatePOST /ordersNoNo
PUTReplace completelyPUT /orders/42NoYes
PATCHUpdate some fieldsPATCH /orders/42NoUsually
DELETERemoveDELETE /orders/42NoYes
  • Safe means it doesn't change anything. GET must never change data, because browsers, caches and crawlers send GET requests 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:

CodeUse it when
200 OKA successful read or update, with a body
201 CreatedPOST created something; include its URL in a Location header
204 No ContentSuccess with nothing to return, such as DELETE
400 Bad RequestThe input is invalid: missing field, wrong type
401 UnauthorizedNot logged in, or the token is missing or expired
403 ForbiddenLogged in, but not allowed to do this
404 Not FoundThe resource doesn't exist
409 ConflictClashes with current state, such as a duplicate email
429 Too Many RequestsThe client hit a rate limit
500 Internal Server ErrorA 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.

ActionEndpoint
List books, search by titleGET /v1/books?title=python&limit=20
Get one bookGET /v1/books/{book_id}
Add a book (librarian)POST /v1/books → 201
Borrow a bookPOST /v1/loans with {"book_id": 12, "member_id": 7} → 201, or 409 if already borrowed
Return a bookPATCH /v1/loans/{loan_id} with {"returned_at": "..."}
A member's loansGET /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

StyleGood for
RESTPublic and general-purpose APIs; simple, cacheable
GraphQLApps that need flexible queries, fetching exactly the fields they want in one request
gRPCFast communication between internal services
WebSocketsReal-time two-way updates, such as chat

Practice

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

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

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

  1. Measure how fast your API is in latency, throughput and availability.
  2. Protect it from overload with rate limiting.