REST API Design Guide and Rules
REST API Design Guide and Rules
What is an API?
An Application Programming Interface (API) is a set of rules, protocols, and tools that
allows different software applications to communicate with each other. APIs act as
intermediaries, enabling one program to request data or services from another without
needing to understand the internal workings of the other system. For example, a
weather app might use an API to retrieve real-time weather data from a remote server.
Understanding the following terms is essential for API developers, as they form the
foundation of API design, implementation, and interaction. These terms are drawn from
the REST API Design Rulebook and general API development practices.
5. Status Code: Numeric codes in HTTP responses (e.g., 200 OK, 404 Not Found)
that indicate the outcome of a request. They are crucial for communicating
success or failure.
10. Stateless: A REST constraint requiring each client request to contain all
necessary information, with no server-side session state, enhancing scalability.
11. Cache: A mechanism to store response data, reducing latency and server load.
REST APIs use headers like Cache-Control to manage caching.
12. Link: A reference to another resource (e.g., a URL in a JSON response) that
enables navigation or actions, central to hypermedia in REST.
13. Schema: A definition of a resource's structure (fields and links), used to validate
and version representations, often in formats like JSON Schema.
14. OAuth: An authorization protocol for securing API resources, allowing clients to
access protected data without sharing credentials.
16. JSONP (JSON with Padding): A technique to bypass same-origin restrictions for
JavaScript clients, enabling cross-origin GET requests.
17. Entity Tag (ETag): A header used to track the version of a resource’s state,
supporting conditional requests and versioning.
18. Query Parameter: Part of a URI (e.g., ?name=John) used to filter, paginate, or
customize API responses.
These terms are critical for designing, implementing, and consuming APIs, particularly
REST APIs, as they underpin the architectural principles and practical considerations
outlined in the book.
A REST API (Representational State Transfer API) is a type of web API that adheres to the
architectural style of REST, as defined by Roy Fielding in his 2000 Ph.D. dissertation.
REST APIs enable clients (e.g., web or mobile applications) to interact with server
resources over HTTP, using a standardized, stateless, and resource-oriented approach.
REST APIs are governed by six architectural constraints, which collectively define their
principles and ensure scalability, simplicity, and maintainability. These principles are
derived from Fielding’s work and elaborated in the REST API Design Rulebook (pages 23-
24).
1. Client-Server
2. Stateless
3. Cacheable
4. Layered System
5. Uniform Interface
6. Code-on-Demand (Optional)
o Description: Servers can send executable code (e.g., JavaScript) to
clients to extend functionality, though this is rarely used in REST APIs due
to security and complexity concerns.
These principles ensure REST APIs are scalable, maintainable, and aligned with web
standards, making them ideal for distributed systems.
REST APIs differ from other API types (e.g., SOAP, GraphQL, or RPC-based APIs) in
several ways:
• Use of HTTP Standards: REST leverages HTTP methods (GET, POST, PUT,
DELETE) and status codes (200, 404) directly, aligning with web standards,
whereas SOAP uses XML-based messaging over various protocols.
• Flexibility in Data Formats: REST supports multiple formats (e.g., JSON, XML),
while SOAP is tied to XML, and GraphQL uses a specific query language.
Compared to SOAP, REST is lighter, more flexible, and web-native, avoiding complex
XML schemas and middleware. Compared to GraphQL, REST is simpler to implement
but less flexible for clients needing specific data structures, as GraphQL allows precise
querying. Compared to RPC, REST is more standardized and resource-focused,
avoiding proprietary operation definitions.
REST APIs are widely adopted due to their alignment with web architecture, ease of use,
and ability to scale, making them ideal for public APIs, mobile apps, and microservices.
Below is the list of 37 design rules from the REST API Design Rulebook, organized by
chapter and section, with each rule accompanied by its description and page number
from the book.
This chapter provides rules for designing URIs to address resources consistently and
effectively.
2. A trailing forward slash (/) should not be included in URIs (Page 30)
Trailing slashes add no semantic value and may cause confusion, so they should
be avoided.
3. Hyphens (-) should be used to improve the readability of URIs (Page 30)
Hyphens enhance readability in long URI path segments, replacing spaces or
hyphens in English text.
7. Consistent subdomain names should be used for your APIs (Page 32)
APIs should use a consistent subdomain (e.g., api.soccer.restapi.org) to identify
the service owner.
10. A plural noun should be used for collection names (Page 33)
Collection resource names should use plural nouns (e.g., /leagues).
11. A plural noun should be used for store names (Page 33)
Store resource names should use plural nouns (e.g., /favorites).
12. A verb or verb phrase should be used for controller names (Page 33)
Controller resource names should use verbs or verb phrases (e.g., /calculate).
13. Variable path segments may be substituted with identity-based values (Page
33)
URI path segments can include variable values based on resource identity (e.g.,
/users/{id}).
14. CRUD function names should not be used in URIs (Page 33)
URIs should not include CRUD operation names (e.g., avoid /getUser or
/deleteUser).
15. The query component of a URI may be used to filter collections or stores
(Page 33)
Query parameters can be used to filter resources in collections or stores (e.g.,
?name=John).
16. The query component of a URI should be used to paginate collection or store
results (Page 33)
Query parameters should support pagination (e.g., ?page=2&size=10).
This chapter outlines rules for using HTTP methods and status codes appropriately in
REST API interactions.
17. GET and POST must not be used to tunnel other request methods (Page 34)
GET and POST should not be used to simulate other HTTP methods.
22. POST must be used to create a new resource in a collection (Page 34)
POST creates new resources in server-managed collections.
24. DELETE must be used to remove a resource from its parent (Page 34)
DELETE removes a resource from its parent collection or store.
26. 200 ("OK") should be used to indicate nonspecific success (Page 34)
200 indicates general success for a request.
27. 200 ("OK") must not be used to communicate errors in the response body
(Page 34)
Errors should use appropriate 4xx or 5xx codes, not 200 with error details.
28. 201 ("Created") must be used to indicate successful resource creation (Page
34)
201 indicates a resource was successfully created.
30. 204 ("No Content") should be used when the response body is intentionally
empty (Page 35)
204 is used for successful requests with no response body.
31. 301 ("Moved Permanently") should be used to relocate resources (Page 35)
301 indicates a resource has been permanently moved to a new URI.
33. 303 ("See Other") should be used to refer the client to a different URI (Page
35)
303 directs clients to another URI for further action.
34. 304 ("Not Modified") should be used to preserve bandwidth (Page 35)
304 indicates the resource hasn’t changed since the last request.
35. 307 ("Temporary Redirect") should be used to tell clients to resubmit the
request to another URI (Page 35)
307 indicates a temporary redirect to another URI.
36. 400 ("Bad Request") may be used to indicate nonspecific failure (Page 35)
400 indicates a general client error.
37. 401 ("Unauthorized") must be used when there is a problem with the client’s
credentials (Page 35)
401 indicates invalid or missing credentials.