REST API Design
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
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 (Stack Overflow Blog)
- OpenAPI Specification (Swagger)
- Microsoft REST API Guidelines (Microsoft)