Currently Available: Need a skilled Software Developer for your next project?
Categories
APIs

Modern API Design Best Practices

When you design an HTTP API, you define how software clients ask a service to do work and how the service responds. The API’s contract is the shared description of those requests and responses, including their meaning, limits, and error behavior. Clear contracts help clients use the service predictably and let its developers improve it without surprising consumers.

For REST-style APIs, which organize paths around resources such as orders or accounts, strong design comes from consistent HTTP behavior, explicit documentation, planned compatibility, and authorization at the level of each resource and action. These practices give you concrete ways to make an API easier to use, safer to operate, and more adaptable as requirements change.

Define the contract before implementation

Start with what a client needs to accomplish, then describe each operation in terms the client can use. Document the operation’s purpose, inputs, outputs, authentication and authorization requirements, errors, limits, and examples. Use the same names and rules for related operations, so clients know what to expect from each endpoint.

The OpenAPI Specification provides a standard format for describing HTTP API operations and data. Use a specification your documentation and code-generation tools support, and identify its version clearly. Keep the specification aligned with the running service and use it in design reviews and automated checks. A valid specification does not prove that an implementation follows it, so test actual requests and responses as well.

When several people design or build endpoints, shared conventions help them follow the same rules. Google’s API design guide offers patterns for HTTP APIs and RPC APIs, while making clear that its guidance is not a complete rulebook for every service. Adopt conventions that fit your API, document them, and distinguish protocol requirements from local choices.

Use HTTP methods and status codes accurately

For a REST-style API, use paths to identify resources and HTTP methods to describe what the client wants to do. For example, /orders/{orderId} identifies an order, while GET retrieves it and DELETE requests its removal. If an action does not fit a simple resource operation, document its purpose and what happens when the client retries it.

HTTP defines specific meanings for common methods. GET retrieves information and should not perform a hidden state change. PUT and DELETE are idempotent, so repeating the same request should have the same intended result as sending it once. POST does not guarantee that behavior. If a connection fails after a client sends a POST, the client might not know whether the service completed the request. Document when a retry is safe and how clients can check the result. These semantics come from RFC 9110.

Choose status codes that describe what happened. When a request creates a resource, return 201 Created. When the service has accepted work that is still in progress, return 202 Accepted and give the client a way to check its status. Return 204 No Content when a request succeeds and the response has no body. Accurate status codes let clients make decisions without interpreting vague messages.

Plan collection endpoints for growth

Paginate collections from the start. Pagination divides a large result into smaller responses, which helps control response size and the work a client must do at once. Adding pagination later can change the meaning of an existing response: clients that expect the full collection might silently process only the first page.

A common approach uses a page-size parameter and a continuation token. The service returns a token when more results remain; the client sends that token to request the next page. Google’s pagination guidance says clients should follow the continuation token rather than infer that results are complete because a page contains fewer items than requested. Treat tokens as opaque: clients pass them back without interpreting or constructing them. The token does not grant access, so the service must authorize every page request.

A cursor points to a position in the result set and suits many large or changing collections. An offset identifies how many results to skip and can be convenient when clients need to jump to a particular page. Choose between them based on the collection’s size, the client’s needs, how the service queries the data, and how clients move through the results. For more implementation considerations, see this pagination and performance overview.

Evolve the API without surprising clients

Design changes to preserve compatibility. A change is compatible when existing clients continue to behave as expected without changes of their own. Additions are not automatically safe: a new required request field prevents older clients from making the request, and a new response value can break client code that assumes it has seen every possible value. A changed default can also change what clients receive.

The backward-compatibility guidance recommends preserving existing behavior as an API evolves. Before releasing a change, check whether it alters defaults, response meanings, or the amount of data a client receives. Document deprecations and give consumers a migration path before removing functionality.

Zalando’s RESTful API guidelines emphasize avoiding unnecessary versions, while Microsoft’s guidance recommends a new version for breaking changes. The practical decision depends on how independently clients update and how long the service can support parallel versions. Minimize versions when changes remain compatible; when a breaking change is unavoidable, specify how clients migrate and how long the old interface remains supported.

Make errors useful and safe

Return errors in a consistent format so clients can recognize the type of failure and decide what to do next. The status code identifies the general outcome, while structured details describe the specific problem, such as invalid input. RFC 9457 defines a common format for these HTTP problem details.

Keep error fields stable and explain how clients can recover, for example by distinguishing a validation failure from a domain rule that prevents an action. Tell clients which errors they can retry when that distinction matters. Do not use error responses to expose stack traces, secrets, or sensitive internal details. RFC 9457 advises that detail text help the client correct the problem rather than reveal implementation internals.

Secure resources, fields, and actions

Authentication establishes who is making a request. Authorization decides what that caller may access or do. An API needs both, and checking only that a caller is signed in does not protect individual resources or operations.

Check access to the specific object on every request. For example, a caller who can read one order should not automatically gain access to another customer’s order by changing an identifier in the URL. Also check which fields the caller may read or change; do not rely on clients to omit protected properties. Apply authorization to actions as well as reads, since permission to view a resource does not necessarily grant permission to delete or modify it.

The OWASP API Security Top 10 highlights risks involving object-level authorization and property-level authorization. It also covers unrestricted resource use and abuse of business flows. Set limits for collection sizes and large requests, and consider how repeated access to a business action could be abused. For OAuth 2.0 deployments, RFC 9700 recommends protections such as PKCE for public clients using the authorization code flow. OAuth protections help secure delegated access, but they do not replace authorization checks on each API resource and action.

Document the behavior clients depend on

Alongside endpoint names and schemas, document request limits and pagination behavior. Explain error meanings and give retry guidance. Describe any caching behavior the service supports. Set those policies according to the service’s workload and client needs, then test that the implementation matches the documentation. Avoid publishing a limit or retry rule that the service does not enforce.

Before release, review the specification against the running service, test representative success and error responses, and check that authorization covers objects, fields, and actions. Test pagination beyond the first page and verify that clients can follow continuation tokens. Review proposed changes for effects on existing clients, especially changes to defaults and response values.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

Your email address will not be published. Required fields are marked *