The
URI path conveys a
REST API’s resource model, with each forward slash separated path segment corresponding to a unique resource within the model’s hierarchy. For example, this URI design:
http://api.soccer.restapi.org/leagues/seattle/teams/trebuchet
This process is similar to the data modeling for a relational database schema or the classical modeling of an object-oriented system.
http://api.soccer.restapi.org/leagues/seattle/teams/treb
http://api.soccer.restapi.org/leagues/seattle/teams
http://api.soccer.restapi.org/leagues/seattle
http://api.soccer.restapi.org/leagues
http://api.soccer.restapi.org
In order to communicate a clear and clean resource model to its clients, a REST API should align each resource with only one of these archetypes. Like design patterns, the resource archetypes help us consistently communicate the structures and behaviors that are commonly found in REST API designs. A REST API is composed of four distinct resource archetypes:
document,
collection,
store, and
controller.
For uniformity’s sake, resist the temptation to design resources that are
hybrids of more than one archetype. Instead, consider designing separate resources that are related hierarchically and/or through links.
1. Document
A document resource is a singular concept that is akin to an object instance or database record. A document’s state representation typically includes both
fields with values and
links to other related resources
Each URI below identifies a document resource:
http://api.soccer.restapi.org/leagues/seattle
http://api.soccer.restapi.org/leagues/seattle/teams/trebuchet
http://api.soccer.restapi.org/leagues/seattle/teams/trebuchet/players/mike
2. Collection
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 to choose to create a new resource, or not. Resources can be grouped into collections. Each collection is homogeneous so that it contains only one type of resource, and unordered. Resources can also exist outside any collection. In this case, we refer to these resources as singleton resources. Collections are themselves resources as well.
Each URI below identifies a collection resource:
http://api.soccer.restapi.org/leagues
http://api.soccer.restapi.org/leagues/seattle/teams
http://api.soccer.restapi.org/leagues/seattle/teams/trebuchet/players
3. Store
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. 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 a client when it was initially put into the store.
To insert a document resource named “Alex” in their store of favorites:
PUT /users/1234/favorites/alex
4. Controller
A controller resource models a procedural concept. Controller resources are like executable functions, with parameters and return values; inputs and outputs. Like a traditional web application’s use of HTML forms, a REST API relies on controller resources to perform application-specific actions that cannot be logically mapped to one of the standard methods.
Standard Methods: Create, Retrieve, Update, and Delete, also known as CRUD
POST /alerts/245743/resend
Controller names typically appear as the last segment in a URI path.
📷 SwapnIl Dwivedi