Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 3 OF 5 · 3 MIN READ

REST API Design

REST API design is about modelling resources as nouns, using HTTP methods as verbs, and keeping responses consistent. A clean, predictable API is what interviewers look for in API design rounds and take-home assignments.

What you will learn

  • Name resources and nest them sensibly
  • Design pagination, filtering and sorting
  • Return a consistent error format
  • Version an API without breaking clients

Key concepts

Resource

A thing your API exposes, named with a plural noun: /users, /orders/42, /orders/42/items. Actions are expressed with methods, not verbs in the URL (/createUser is a smell).

Statelessness

Each request carries everything needed to handle it (token, parameters). The server keeps no per-client session in memory, so any instance can serve any request and you can scale out.

Cursor pagination

Instead of ?page=50 (OFFSET, slow on big tables and shifts when rows are inserted), the client sends the last seen id: ?after=ord_9f2&limit=20. The query uses WHERE id > ? ORDER BY id LIMIT 20.

Problem Details

RFC 9457 defines a standard JSON error shape: type, title, status, detail, instance. Using one shape for every error lets clients handle errors in one place.

Step by step

URLs and methods

GET    /orders              list (filter: ?status=paid&sort=-createdAt&limit=20)
POST   /orders              create
GET    /orders/{id}         read one
PATCH  /orders/{id}         update fields
DELETE /orders/{id}         delete
POST   /orders/{id}/cancel  an action that is not plain CRUD

Consistent responses

// list
{ "data": [ ... ], "nextCursor": "ord_9f2", "hasMore": true }

// error (Problem Details)
{
  "type": "https://api.example.com/errors/validation",
  "title": "Validation failed",
  "status": 422,
  "errors": [{ "field": "email", "message": "must be a valid email" }]
}

Versioning

Add fields freely (clients ignore unknown fields). Removing or renaming a field is breaking, so put it behind a new version: /v2/orders, or a header like Accept: application/vnd.example.v2+json. Announce deprecation with a Sunset header.

Examples

Cursor pagination in Node.js with SQL

app.get('/orders', async (req, res) => {
  const limit = Math.min(Number(req.query.limit) || 20, 100)
  const after = req.query.after || null
  const rows = await sql`
    SELECT id, total, status, created_at FROM orders
    WHERE user_id = ${req.user.id} AND (${after}::text IS NULL OR id > ${after})
    ORDER BY id LIMIT ${limit + 1}`
  const hasMore = rows.length > limit
  const data = rows.slice(0, limit)
  res.json({ data, hasMore, nextCursor: hasMore ? data.at(-1).id : null })
})

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Design the endpoints for a blog: posts, comments, likes, and publishing a draft.
  • Explain why OFFSET pagination gets slow and shows duplicates, then fix it with a cursor.
  • Decide which of these changes is breaking: adding a field, renaming a field, adding a required query parameter.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

What makes an API RESTful?

Resources identified by URLs, standard methods with their HTTP meaning, stateless requests, cacheable responses with proper headers, and representations such as JSON. In practice most "REST" APIs follow these without full HATEOAS links.

How do you handle an action like "cancel order" in REST?

Either model it as a state change (PATCH /orders/42 with status: "cancelled") or as a sub-resource action (POST /orders/42/cancel) when it has side effects such as refunds. Both are accepted; be consistent.

How would you design rate limits for a public API?

Limit per API key or user with a token bucket or sliding window stored in Redis, return 429 with Retry-After, and expose RateLimit headers so clients can back off before hitting the limit.

Summary

Plural nouns for resources, methods for actions, stateless requests, cursor pagination for large lists, one error shape everywhere, and additive changes with explicit versions for breaking ones.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus