Search code examples
restapi-design

RESTful API endpoint for custom response data


I am building a RESTful API where I have a resource named solar_systems. solar_sytems has id(int), system_size(int), system_cost(int) columns with many other columns.

I understand that API endpoints will be-

/v1/solar-systems - for all systems

/v1/solar-systems/{id} - for a single system

And I have to pass query params for filter, search, sorting etc.

But what will be the best practice for API endpoints if I need some kind of custom data like if I need average system_cost for each system_size.

Will it be silly, if I use - /v1/solar-systems/average-system-cost?

Please I need your opinion from your experience.


Solution

  • It is not silly at all to use /v1/solar-systems/average-system-cost

    It is easy to get caught up in the fact that technically the average-system-cost is not a resource. But it is a piece of data that is useful to retrieve. Ultimately the goal of REST is to make APIs that are understandable and readable. A specific endpoint that gets a useful piece of data definitely falls inside that.