Best Practices for Naming REST API URIs: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 22: Line 22:


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_Status_Codes#301_Moved_Permanently|HTTP 301]] response code.
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_Status_Codes#301_Moved_Permanently|HTTP 301]] response code.
=<span id='Hyphen'></span>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 [[#Underscore|underscores]] (first_name) or camel case (firstName), which is discouraged due to its use of capital letters.
=<span id='Underscore'></span>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 [[#Hyphen|hyphens]] to separate words.


=Use Nouns to Name URIs=
=Use Nouns to Name URIs=

Revision as of 00:38, 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.

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.

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.

Use Nouns to Name URIs

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}

Singular or 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.

TODO