Search code examples
restarchitectureapi-design

How to organize an API that needs to return all titles from a given entity?


I have the following endpoint that returns a page of one entity

/entity?page={page}

However, I need another endpoint that returns a list containing the title of all entities. I need that because there is a dropdown filter option that a user can filter items by title and because of that I have to list all titles there.

I would like to know what would be the best practices to organize this in my API. Maybe just something like:

/entity/titles

Solution

  • From a basic REST perspective, I'd start by restructuring I'd restructure to follow a more standardized structure. In general REST mentality, resources should be named with a plural unless there is only one of them in the scope of the request. Based on that, I'd propose these endpoints

    GET /entities/ -> Provides a list of all entities (index action)
    GET /entities/:id -> where :id is a URL parameter indicating the ID of the page, Provides the details for a single entity (show action)
    

    With this basic structure, your question becomes more straightforward. If the payload for entity is small and you're not worried about performance, I'd have the front-end request GET /entities and extract out the titles.

    If the payload for entity is large, then I'd suggest adding an optional query parameter to the index action:

    GET /entities?onlyTitles=true
    

    When this parameter is true, the payload returned can be scoped to just the titles. When omitted or false, the payload returned can be the full payload for entities.