Back to Blog

The Art of Writing Clean APIs

January 05, 20263 min read
API DesignRESTBackendBest Practices
The Art of Writing Clean APIs

The Art of Writing Clean APIs

An API is a promise. It tells other developers: "Send me this, and I will give you that." The best APIs are the ones where reading the documentation feels unnecessary because the design is intuitive. Getting there takes deliberate effort.

Naming Conventions

Use nouns for resources, not verbs. The HTTP method already carries the verb.

  • Good: GET /users/123, POST /users, DELETE /users/123
  • Bad: GET /getUser/123, POST /createUser, DELETE /deleteUser/123

Use plural nouns consistently. If one endpoint uses /users, do not have another that uses /order. Consistency reduces cognitive load for consumers.

Nest related resources logically: GET /users/123/orders returns orders for user 123. But do not nest more than two levels deep. /users/123/orders/456/items/789 is hard to read and harder to maintain.

Error Handling

Good error responses are the difference between a frustrating API and a pleasant one. Every error response should include:

  1. A machine-readable error code. Not just the HTTP status, but a specific code like VALIDATION_ERROR or RESOURCE_NOT_FOUND.
  2. A human-readable message. Explain what went wrong in plain language.
  3. Field-level details for validation errors. If the email field is invalid, say so. Do not make the consumer guess which field failed.

Use HTTP status codes correctly. 400 for bad requests, 401 for unauthenticated, 403 for unauthorized, 404 for not found, 409 for conflicts, 422 for validation failures, 500 for server errors. Do not return 200 with an error body.

Pagination, Filtering, and Sorting

Any endpoint that returns a list should support pagination from day one. Cursor-based pagination scales better than offset-based for large datasets.

Use query parameters for filtering: GET /users?role=admin&status=active. Use a consistent pattern for sorting: GET /users?sort=createdAt&order=desc.

Return pagination metadata in the response: total count, current page, next cursor, and whether more results exist.

Versioning

APIs evolve. Fields get renamed, endpoints get deprecated, response shapes change. Version your API from the start.

I prefer URL-based versioning (/api/v1/users) over header-based versioning because it is explicit, visible in logs, and easy to test. When a breaking change is needed, create a new version and give consumers a migration timeline.

Authentication and Rate Limiting

Use bearer tokens for authentication. Include rate limit headers in every response so consumers know their limits before hitting them: X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset.

Documentation

The best documentation is generated from the code. I use OpenAPI (Swagger) specifications and generate documentation automatically. This ensures the docs stay in sync with the implementation.

Include example requests and responses for every endpoint. Show both success and error cases. A single good example saves a consumer an hour of trial and error.

The Principle

Design your API as if you are the consumer. Test it by building a client before you finalize the design. If the client code feels awkward, the API design is wrong. Clean APIs are not about following every REST convention. They are about making the consumer's job easy.

Enjoyed this post?

I'm always happy to connect and discuss ideas.