API · Backend · Node.js · Architecture
REST API Design Best Practices: URLs, Pagination, Versioning and Errors
Designing REST APIs that are easy to use and last for years: resource naming, HTTP methods, status codes, pagination, filtering, versioning and idempotency.

Once an API is public it is hard to change: mobile apps, integrations and customer scripts depend on it. So first design decisions live for years. These are the principles I took from APIs I consumed as a frontend developer and designed as a backend developer.
Resources are nouns, actions are HTTP methods
GET /devices list devices
POST /devices add a device
GET /devices/{id} one device
PATCH /devices/{id} partial update
DELETE /devices/{id} delete
GET /devices/{id}/runs test runs for a device
- Use plural nouns:
/devices,/runs. - Keep verbs out of URLs: not
/getDevicesor/deleteRun. - Keep nesting to one level;
/a/{id}/b/{id}/c/{id}becomes unreadable. - For actions that don't fit CRUD, a sub-resource is acceptable:
POST /runs/{id}/cancel.
The right status codes
| Code | When |
|---|---|
| 200 OK | Successful read or update |
| 201 Created | Resource created (with a Location header) |
| 204 No Content | Success, no body (for example, delete) |
| 400 Bad Request | Invalid input |
| 401 / 403 | Not authenticated / not authorised |
| 404 Not Found | No such resource |
| 409 Conflict | Conflict (for example, a duplicate name) |
| 422 Unprocessable Entity | Well-formed but breaks a business rule |
| 429 Too Many Requests | Rate limit exceeded |
Returning 200 for everything with success: false in the body makes life hard for clients and monitoring tools.
A consistent error format
Every error should come back in the same shape. I describe how to build that with custom error classes and a central middleware in error handling in TypeScript Node.js APIs.
Pagination
Two common approaches:
- Offset:
?page=3&limit=50. Simple and lets you jump to a page, but slows down on big tables and pages shift when rows are inserted. - Cursor:
?after=eyJpZCI6MTIzNH0&limit=50. Constant performance and consistent results; ideal for infinite scroll and realtime feeds.
{
"data": [ ... ],
"page": { "next": "eyJpZCI6MTI4NH0", "limit": 50 }
}
Always cap the page size (say, limit at most 100); an unbounded list request will take the server down one day.
Filtering, sorting and field selection
GET /runs?status=failed&deviceId=42&sort=-createdAt&fields=id,status,createdAt
A - prefix for descending order is a common, readable convention. Document which fields support filtering and sorting, and back them with indexes.
Versioning
- The most practical option is the major version in the URL:
/v1/devices. Explicit, cache-friendly and easy to test. - Non-breaking changes (a new field, a new endpoint) don't need a version. Teach clients to ignore fields they don't know.
- For breaking changes, keep the old version alive for a while and announce it with
DeprecationandSunsetheaders.
Idempotency
When the network drops, clients retry. GET, PUT and DELETE should be naturally repeatable. For POST requests such as payments or orders, accept an Idempotency-Key header and return the first result to a second request with the same key; that prevents duplicate records.
Documentation
An OpenAPI schema is both documentation and contract: you can generate client code, validate requests and run tests against it. Generating the schema from code, or code from the schema, keeps the two from drifting apart.
Conclusion
A good REST API is predictable: consistent names, meaningful status codes, errors in one shape, paginated lists and changes that don't break clients. Set these rules on day one and the API grows for years without maintenance pain.


