Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTful resources: Plural vs. singular when a resource is always singular in the database

If I decide to go with plural for all of my route names but some of the resources only exist as one thing, do you keep it as singular (more intuitive) or respect that decision of using plurals and stay that way?

We were designing a new API for our customer portal in PHP and we had something like this come up:

/api/orders/or-a41931-0001/special-agreement/

And an order can only have one special agreement (and no other types of agreements btw so I can't do /agreements/?type=special or something like that).

is it typical to do /special-agreements/ or is /special-agreement/ used if an order must have exactly one?

like image 931
NullHypothesis Avatar asked Jul 20 '15 19:07

NullHypothesis


People also ask

Should REST API be singular or plural?

Plural Nouns Are Preferable, Rather Than Singular Using plural or singular nouns for defining resources has no any impact on how your API will work; however, there are common conventions that are used in all good RESTful APIs. One of such conventions is the use of plural nouns for defining resources.

Can resources be singular?

Declaring a resource or resources generally corresponds to generating many default routes. resource is singular. resources is plural.

Is API plural?

The plural of "API" is "APIs". Not "API's".

Should URL be plural or singular?

Yes, URL could be plural, but type singular. More to say, they might be completely different words. For example GET /vehicles endpoint might return resources of type Car , Aircraft , Train , Ship , and so on.


1 Answers

More of an opinion based question but...

As far as I see, there are two main types of resources: collection and instance.

A collection would be "users". You can access /users to get the list of users, or access /users/id to get a specific user. The specific user would usually be the instance.

In this case, the reason for the plurality of users is because it is a collection. It makes no sense to use /users if there is no potential to go further (accessing an instance of the collection).

If you were accessing your own profile, you only have one profile, so you should be accessing it at /profile, not /profiles (instance not collection)

In your case, there is only one agreement, so it is not collection, does not require an agreement ID to access it, and therefore should not be plural.

like image 77
Devon Avatar answered Sep 23 '22 02:09

Devon