# REST API Design

> Design predictable, versionable JSON APIs with clear resources, status codes, pagination and filtering that frontends and mobile apps can rely on. Free notes,...

Canonical: https://www.internhack.in/roadmaps/tracks/full-stack/rest-api-design
Last updated: 2026-11-28

Design predictable, versionable JSON APIs with clear resources, status codes, pagination and filtering that frontends and mobile apps can rely on.

Intermediate · about 5 hours · difficulty 3/5

Learn first: [HTTP, Requests and Responses](https://www.internhack.in/roadmaps/tracks/full-stack/http-basics)

A good API is boring and predictable. Frontend developers, mobile apps and future you should be able to guess an endpoint before reading the docs.

### Resources and methods

- Nouns for resources: /students, /companies/42/openings, /applications.
- Methods for actions: GET to list or read, POST to create, PATCH to partially update, DELETE to remove.
- Return **201** with the created resource after POST, **204** after a delete, **404** for missing records, **409** for conflicts such as a duplicate application.

### Lists at scale

- **Pagination**: offset/limit is simple; cursor-based pagination stays fast and stable on large, changing tables.
- **Filtering and sorting** with query parameters: ?branch=CSE&minStipend=20000&sort=-createdAt.
- Return a consistent envelope, for example { data, nextCursor }.

### Consistency and evolution

- Use one error format everywhere (status, code, message, field errors).
- Version when you must break clients (/v1) and prefer additive changes.
- Make retried operations safe: idempotency keys matter for payments and order creation.
- Document with **OpenAPI** so others can generate clients and test against it.

### Pitfalls

- Verbs in URLs like /getAllStudents or /deleteCompany.
- Returning everything in one unpaginated response.
- Different error shapes from every endpoint.

You are ready to move on when you can write an OpenAPI-style design for a placement portal API (students, companies, openings, applications) that a teammate could build a frontend against without asking questions.

## Key concepts

- **Resource naming**
- **Methods and status codes**
- **Pagination: offset vs cursor**
- **Filtering and sorting**
- **Consistent error format**
- **Versioning**
- **Idempotency keys**
- **OpenAPI documentation**
- **GraphQL and tRPC**: Other API styles worth knowing about

## Mini project

Write an OpenAPI 3 spec for the placement portal API with at least ten endpoints, cursor pagination on lists and a shared error schema, and preview it in Swagger Editor.

## Check yourself

Why does offset pagination give duplicate or missing rows when new records are being inserted, and how does cursor pagination avoid it?

## Free resources

- [Best practices for REST API design](https://stackoverflow.blog/2020/03/02/best-practices-for-rest-api-design/) (Stack Overflow Blog)
- [OpenAPI Specification](https://swagger.io/specification/) (Swagger)
- [Microsoft REST API Guidelines](https://github.com/microsoft/api-guidelines) (Microsoft)

Previous: [Node.js Runtime Essentials](https://www.internhack.in/roadmaps/tracks/full-stack/nodejs-runtime) · Next: [Building APIs with Express](https://www.internhack.in/roadmaps/tracks/full-stack/express)

## More in Node.js and API Design

- [Node.js Runtime Essentials](https://www.internhack.in/roadmaps/tracks/full-stack/nodejs-runtime)
- [REST API Design](https://www.internhack.in/roadmaps/tracks/full-stack/rest-api-design)
- [Building APIs with Express](https://www.internhack.in/roadmaps/tracks/full-stack/express)
- [Building APIs with Fastify](https://www.internhack.in/roadmaps/tracks/full-stack/fastify)
- [API Routes with Next.js Route Handlers](https://www.internhack.in/roadmaps/tracks/full-stack/next-route-handlers)
- [Error Handling, Validation and API Hardening](https://www.internhack.in/roadmaps/tracks/full-stack/api-errors-and-validation)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Full-Stack Developer Roadmap](https://www.internhack.in/roadmaps/tracks/full-stack)
