Best Practices for Naming REST API URIs: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 81: Line 81:
</font>
</font>
===<span id='A92'></span>Use Plural Nouns for Collection Names===
===<span id='A92'></span>Use Plural Nouns for Collection Names===
A URI identifying a collection should be named with a plural noun, or noun phrase path segment. A collection's name should be chosen to reflect what it uniformly contains.


==Store==
==Store==

Revision as of 23:12, 14 February 2024

External

  • REST API Design Rulebook by Mark Masse, O'Reilly, Chapter 2. Identifier Design with URIs.

Internal

Overview

REST APIs use Uniform Resource Identifiers (URIs) to address resources.

General URI Rules

Use Forward Slash to Indicate a Hierarchical Relationship

The forward slash (/) character is used in the path portion of the URI to indicate a hierarchical relationship between resources. Also see Resource Modeling below.

Example:

http:://api.canvas.restapi.org/shapes/polygons/quadrilaterals/squares

A Trailing Forward Slash Should Not Be Included in URIs

Every character within a URI counts towards a resource's unique identity. If the URIs differ, then so do the resources, and vice-versa. Even if the REST API implementation discards the trailing forward slashes, the definition of the REST API must communicate clean URIs. For example, an API implementation may redirect clients that use a forward slash to the URI without forward slash using a HTTP 301 response code.

Separate Words with Hyphens

When a REST API URI contains multiple words in a path segment, separate the words with hyphens. It's a good way to make the URI easier to read and is a universal method that everyone can understand. It's generally accepted that a hyphen is clearer and more user-friendly than underscores (first_name) or camel case (firstName), which is discouraged due to its use of capital letters.

Do Not Use Underscores

One practical reason is that the underscored may get partially obscured in the UIs that render clickable URIs, depending on the fonts that are used. To avoid this, use hyphens to separate words.

Prefer Lowercase Letters

The URIs are case-sensitive, except for the scheme and host components.

Lowercase is preferred, if practical, because capital letters can sometimes cause problems.

File Extensions Should Not Be Included in URIs

A REST API should not include artificial file extensions in URIs to indicate the format of a message's entity body. Instead, they should rely on media type, as communicated through the Content-Type header.

Authority Rules

APIs Should Use Consistent Subdomain Names

The top-level domain and first subdomain name (example: accounting.example.com) should identity the service owner. The full domain name of an API should add a subdomain named "api": api.accounting.example.com.

Developer Portal Names

Many REST APIs have an associated website, known as developer portal, to help on-board new clients with documentation, etc. Conventionally it is named using the "developer" subdomain: developer.accounting.example.com.


Resource Archetypes

The URI path conveys a REST API's resource model, which each forward slash separated path segment corresponding to a unique resource within the model hierarchy. Resource modeling establishes the key API's concepts, and it is similar to data modeling in a relational database or class hierarchy modeling in an object-oriented system. Assigning meaningful values for each path segment helps to communicate the hierarchical structure of the REST API resource model. For example in this URI http:://api.directory.exmaple.com/departments/marketing/teams/{id}, each of the path elements identifies an addressable resource:

http:://api.directory.exmaple.com/departments/marketing/teams
http:://api.directory.exmaple.com/departments/marketing
http:://api.directory.exmaple.com/departments
http:://api.directory.exmaple.com

It is useful to assign every resource modeled in a system to one of four archetypes: document, collection, store and controller. Resist the temptation to design resources as hybrids of more than one archetypes. If that situation arises, consider designing separate resources that are either part of a hierarchy, or in relationships, modeled as links.

Document

A document resource models a singular concept in the system exposed by the REST API, similar conceptually with an object instance in an object-oriented system. A document's state includes fields with values, and links to related resources, modeling the relationships that exist between instances. The following URIs identifies a document resource:

http:://api.directory.exmaple.com/departments/marketing/teams/advertising
http:://api.directory.exmaple.com/departments/marketing

A document may have child resources that represent different subordinate concepts.

Use Singular Nouns or Identity-Based Values for Document Names

A URI representing a document resource should be named with a singular noun or noun phrase path segment:

http:://api.directory.exmaple.com/departments/marketing

The URI template syntax allows for dynamic path elements, automatically filled in with some identifier that provides the URI its uniqueness. When the instance corresponding to a document resource has a unique ID, that unique ID can be used in the path segment corresponding to the document resource:

http:://api.directory.exmaple.com/departments/1022

Collection

A collection is a system-managed directory of resources. Clients may propose new resources to be added to the collection, but it is up to the collection resource to decide whether a new resource is created or not. The collection chooses what it wants to contain, and also decides the URI of each contained resource. The following URIs represent collections:

http:://api.directory.exmaple.com/departments/marketing/teams
http:://api.directory.exmaple.com/departments

Use Plural Nouns for Collection Names

A URI identifying a collection should be named with a plural noun, or noun phrase path segment. A collection's name should be chosen to reflect what it uniformly contains.

Store

A store is a client-managed resource repository. A store resource lets the client put resources in, via API calls, to get them back out and delete them when the client wishes. On their own, stores do not create new resources. Therefore, a store never generates new URIs. Instead, each stored resource has a URI that was chosen by the client when it was initially put in the store.

An example of a request initiated by a user ("1122") to insert a document resources name "chocolate" in the user's store of "favorites":

PUT /users/1122/favorites/chocolate

Controller

A controller resource models a procedural concept that goes beyond those that can be modeled with HTTP operations GET, POST, PUT, PATCH, DELETE, etc. Controller resources are similar to executable functions, with parameters and return values. The REST API relies on controller resources to perform application-specific actions that cannot be mapped onto CRUD operation. Controller names typically appear as the last segment in a URI path, with no child resources to follow them in the hierarchy.

POST /alerts/245743/resend

Resource Modeling Rules

Use Singular Nouns or Identity-Based Values for Document Names

See Use Singular Nouns or Identity-Based Values for Document Names above.

Use Plural Nouns for Collection Names

See Use Plural Nouns for Collection Names above.

Use Nouns to Name Resources

A REST API endpoint has a URL at which it can be accessed: https://example.com. Subdirectories of the URL denote different resources, which are accessed with URIs.

In general, URIs should be named with nouns that specify the content of the resource, rather than using a verb for the function performed. This is because CRUD operations should already be specified in the HTTP request.

Example:

https://example.com/users
https:://example.com/users/{id}

Prefer Plural

In general, you should name the URIs that return collections using plural. The URIs that return an individual element will use the same prefix and an additional path qualifier, usually an ID, to designate the specific element.