DEV Community

Dang Hoang Nhu Nguyen
Dang Hoang Nhu Nguyen

Posted on


[BTY] Day 2: REST Resource Naming Guide

This post was for 05.02.2022

There are only two hard things in Computer Science: cache invalidation and naming things. -- Phil Karlton

Recently, I have had to build an API service for an internal project in my company. URI naming has stressed me out.

Fortunately, I found an interesting article that lists out the best practices to name REST Resource when you design API for your application.

The article is here:

Best Practices

Use nouns to represent resources

RESTful URI should refer to a resource that is a thing (noun) instead of referring to an action (verb) because nouns have properties that verbs do not have – similarly, resources have attributes. Some examples of a resource are:

Users of the system
User Accounts
Network Devices etc.
and their resource URIs can be designed as below:{device-id}{id}
Enter fullscreen mode Exit fullscreen mode

For more clarity, let’s divide the resource archetypes into four categories (document, collection, store, and controller). Then it would be best if you always targeted to put a resource into one archetype and then use its naming convention consistently.

For uniformity’s sake, resist the temptation to design resources that are hybrids of more than one archetype.


A document resource is a singular concept that is akin to an object instance or database record.

In REST, you can view it as a single resource inside resource collection. A document’s state representation typically includes both fields with values and links to other related resources.

Use “singular” name to denote document resource archetype.{device-id}{id}
Enter fullscreen mode Exit fullscreen mode


A collection resource is a server-managed directory of resources.

Clients may propose new resources to be added to a collection. However, it is up to the collection resource to choose to create a new resource or not.

A collection resource chooses what it wants to contain and also decides the URIs of each contained resource.

Use the “plural” name to denote the collection resource archetype.{id}/accounts
Enter fullscreen mode Exit fullscreen mode


A store is a client-managed resource repository. A store resource lets an API client put resources in, get them back out, and decide when to delete them.

A store never generates new URIs. Instead, each stored resource has a URI. The URI was chosen by a client when the resource initially put it into the store.

Use “plural” name to denote store resource archetype.{id}/playlists
Enter fullscreen mode Exit fullscreen mode


A controller resource models a procedural concept. Controller resources are like executable functions, with parameters and return values, inputs, and outputs.

Use “verb” to denote controller archetype.{id}/cart/checkout{id}/playlist/play
Enter fullscreen mode Exit fullscreen mode

Consistency is the key

Use consistent resource naming conventions and URI formatting for minimum ambiguity and maximum readability and maintainability. You may implement the below design hints to achieve consistency:

Use forward slash (/) to indicate hierarchical relationships

The forward-slash (/) character is used in the path portion of the URI to indicate a hierarchical relationship between resources. e.g.{id}{id}/scripts{id}/scripts/{id}
Enter fullscreen mode Exit fullscreen mode

Do not use trailing forward slash (/) in URIs
As the last character within a URI’s path, a forward slash (/) adds no semantic value and may confuse. It’s better to drop it from the URI.  /*This is much better version*/
Enter fullscreen mode Exit fullscreen mode

Use hyphens (-) to improve the readability of URIs

To make your URIs easy for people to scan and interpret, use the hyphen (-) character to improve the readability of names in long path segments.    /*This is much better version*/
Enter fullscreen mode Exit fullscreen mode

Do not use underscores ( _ )

It’s possible to use an underscore in place of a hyphen to be used as a separator – But depending on the application’s font, it is possible that the underscore (_) character can either get partially obscured or completely hidden in some browsers or screens.

To avoid this confusion, use hyphens (-) instead of underscores ( _ ).{id}/install-script-location  //More readable{id}/installScriptLocation  //Less readable
Enter fullscreen mode Exit fullscreen mode

Use lowercase letters in URIs

When convenient, lowercase letters should be consistently preferred in URI paths.       //1
HTTP://API.EXAMPLE.ORG/my-folder/my-doc     //2       //3
Enter fullscreen mode Exit fullscreen mode

In the above examples, 1 and 2 are the same but 3 is not as it uses My-Folder in capital letters.

Do not use file extensions

File extensions look bad and do not add any advantage. Removing them decreases the length of URIs as well. No reason to keep them.

Apart from the above reason, if you want to highlight the media type of API using file extension, then you should rely on the media type, as communicated through the Content-Type header, to determine how to process the body’s content.  /*Do not use it*/    /*This is correct URI*/
Enter fullscreen mode Exit fullscreen mode

Never use CRUD function names in URIs

We should not use URIs to indicate a CRUD function. URIs should only be used to uniquely identify the resources and not any action upon them.

We should use HTTP request methods to indicate which CRUD function is performed.

HTTP GET  //Get all devices
HTTP POST  //Create new Device

HTTP GET{id}  //Get device for given Id
HTTP PUT{id}  //Update device for given Id
HTTP DELETE{id}  //Delete device for given Id
Enter fullscreen mode Exit fullscreen mode

Use query component to filter URI collection

Often, you will encounter requirements where you will need a collection of resources sorted, filtered, or limited based on some specific resource attribute.

For this requirement, do not create new APIs – instead, enable sorting, filtering, and pagination capabilities in resource collection API and pass the input parameters as query parameters. e.g.
Enter fullscreen mode Exit fullscreen mode

If you have any useful resources, please help me know by commenting on the post. Thank you!

Top comments (0)