Using human-readable paths for RESTful resources

One of the key ideas, and something that clients would typically expect when dealing with a RESTful API, is that each resource instance can be individually addressed via a Uniform Resource Identifier (URI). Since the format of URIs plays a significant role in conveying the API's resource model to the clients that will be consuming it, software engineers should always strive to come up with consistent URI naming schemes when designing new APIs or introducing new resource types to existing APIs.

The following opinionated set of conventions for naming resources can help you design APIs that are easier for end users to understand and work with:

The following table summarizes the combination of HTTP verbs and URI patterns for performing CRUD operations against a collection of products. The set of HTTP verbs and resource paths for working with a products resource are given as follows:

HTTP Verb Path Expects (JSON) Returns (JSON) HTTP Status Description
POST /products A product entry The new product entry including its ID 200 (success) or 201 (created) Create a new product
GET /products Nothing An array with product entries 200 (success) Get a list of products
GET /products/:id Nothing The product with the specified ID 200 (success) or 404 (not found) Get product by ID
PUT /products/:id A product entry The updated product entry 200 (success) or 404 (not found) Update product by ID
PATCH /products/:id A partial product entry The updated product entry 200 (success) or 404 (not found) Update individual fields for a product by ID
DELETE /products/:id Nothing Nothing 200 (success) or 404 (not found) Delete product by ID

 

As you can probably surmise, the aforementioned patterns can also be applied to address resources that form hierarchies. For instance, to retrieve the set of permissions that have been assigned to the user with ID 123 within a security group with ID 789/security-groups/789/users/123/permissions can be used as a path. In this example, the use of a forward slash to separate the security group and user resources implies the existence of a hierarchical relationship between them.