Search code examples
resturl-design

Correct way to design endpoints


I am designing REST. I have user and user has contacts of different types. What should my endpoints be like according to REST?

This looks reasonable:

GET /users/:id/contacts
GET /contacts

On users endpoint I check contacts for another user and on contacts endpoint I check logged in user in contacts, but then if I need to get all contacts for all users I need to make a filter:

GET /contacts?user_id=:id

And make this endpoint return all contacts. And this makes endpoint on users redundant.

What is correct way to do it according to REST?


Solution

  • What should my endpoints be like according to REST? [...] What is correct way to do it according to REST?

    That's a misconception.

    The REST architecture doesn't enforce any URI design (see notes below). It's totally up to you to pick the URIs that better identify your resources.


    The URI syntax is defined in the RFC 3986. As general rule, the path is organized in hierarchical form (with segments separated by /) and can contain non-hierarchical data in the query component (starting with ?).

    So /users/{id}/contacts seems to be just fine to identify a collection of contacts that belongs to a particular user.


    Note 1: The REST architectural style is described in the chapter 5 of Roy T. Fielding's dissertation and it defines a set of constraints that must be followed by the applications that follow such architecture. However it says nothing about what the URIs must be like.

    Note 2: The examples of a popular article written by Martin Fowler explaining a model defined by Leonard Richardson suggest a URI structure that looks friendly and easy to read.