RESTful API Design Cheatsheet
Tags: tutorial, webdev, api
Published: 2023-04-09 11:04:40

RESTful API design is a complex process that involves many factors, including scalability, performance, security, and user experience. This cheat sheet provides a quick reference for best practices and common pitfalls to avoid when designing RESTful APIs.

## HTTP request methods

[HTTP request methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)/HTTP verbs are used to perform operations on resources in a RESTful API. Each HTTP verb has a specific purpose, and using them correctly is essential for API consistency and security. Here are the most common HTTP verbs and their usage:

-   GET: retrieve a resource
-   POST: create a new resource or perform an action on a resource
-   PUT: update an existing resource
-   PATCH: partially update an existing resource
-   DELETE: delete a resource

## Naming Conventions

Resource [naming conventions](https://restfulapi.net/resource-naming/) are crucial for API readability and usability.

-   Use plural nouns for collections of resources (e.g., /users)
-   Use singular nouns for individual resources (e.g., /users/123)
-   Use lowercase letters and hyphens for resource names (e.g., /users/123/orders)
-   Use nouns instead of verbs in resource names (e.g., /orders/123 instead of /get-order/123)

## Resource Pagination

Resource pagination is necessary when dealing with large collections of resources.

-   Use limit and offset parameters for pagination (e.g., /users?limit=10&offset=20)
-   Use a default limit and let the client control the offset
-   Include pagination metadata in the response headers (e.g., X-Total-Count)

## Error Responses

[Error responses](https://www.baeldung.com/rest-api-error-handling-best-practices) are crucial for API usability and should be consistent across the API.

Use HTTP status codes to indicate the error type (e.g., 400 for bad request, 401 for unauthorized, 404 for not found, 500 for internal server error)

-   Include a descriptive error message in the response body
-   Use a consistent error format across the API

## Authentication and Authorization

[Authentication and authorization](https://stackoverflow.blog/2021/10/06/best-practices-for-authentication-and-authorization-for-rest-apis/) are essential for API security.

-   Use a secure authentication mechanism (e.g., OAuth 2.0, JWT)
-   Use HTTPS for secure communication
-   Use role-based access control (RBAC) to restrict access to resources
-   Use appropriate scopes to restrict access to specific actions

## Versioning

[API versioning](https://www.xmatters.com/blog/blog-four-rest-api-versioning-strategies/) is necessary when changes are made to the API, and backward compatibility needs to be maintained.

-   Use a version number in the URI (e.g., /v1/users)
-   Use a version header in the request (e.g., Accept-Version: v1)
-   Deprecate old versions and provide a migration path

## Caching

Caching can improve API performance and reduce server load. Here are some best practices for API caching:

-   Use appropriate cache headers (e.g., Cache-Control, Expires)
-   Use ETags for resource validation
-   Consider using a content delivery network (CDN) for caching

## HATEOAS

[HATEOAS](https://restfulapi.net/hateoas/) (Hypermedia as the Engine of Application State) is a RESTful API design principle that enables clients to discover and interact with resources dynamically.

-   Include links to related resources in the response
-   Use standard link relations (e.g., self, next, prev)
-   Use media types that support hypermedia (e.g., HAL, JSON-LD)

## Filtering and Sorting

-   Use query parameters for filtering (e.g., /users?status=active)
-   Use query parameters for sorting (e.g., /users?sort=name_asc)

## Rate Limiting

-   Use HTTP headers to communicate rate limits (e.g., X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset)
-   Apply rate limiting based on client IP, authenticated user, or API key

## Content Negotiation

-   Use the Accept header to specify desired response format (e.g., application/json)
-   Use the Content-Type header to specify the format of the request payload (e.g., application/json)

## API Documentation

-   Provide clear and comprehensive documentation
-   Use tools like Swagger/OpenAPI or RAML for API documentation
-   Include examples for different request scenarios

## CORS (Cross-Origin Resource Sharing)

-   Implement CORS headers to control which domains can access your API
-   Use Access-Control-Allow-Origin, Access-Control-Allow-Methods, Access-Control-Allow-Headers, and Access-Control-Max-Age headers as needed
-   Ensure that preflight requests (OPTIONS) are properly handled

## API Monitoring and Testing

![API Monitoring](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g031ww97gkr7xcru5g4b.png)

I have recently been using a service called Checkly, combined with Instatus for my API monitoring/status alerting. You can view my [status page here](https://pubindexapi.instatus.com/).

-   Monitor API performance and uptime
-   Use tools like Postman, Insomnia, or curl for manual testing
-   Implement automated testing for critical API functionality

I put together this REST API cheatsheet to assist with the production/planning of my own [API documentation](https://docs.pubindexapi.com/), hopefully it is useful for someone reading.