REST API Design: Multiple calls vs. single call to the API

apiapi-designresturl

We are developing a Rest API for eCommerce website which will be consumed by mobile apps.

In the home page of an app we need to call multiple resources like Sliders, Top Brands, Best Selling Products, Trending Products etc.

Two options to make API calls:

Single Call:

www.example.com/api/GetAllInHome

Multiple Calls:

www.example.com/api/GetSliders

www.example.com/api/GetTopBrands

www.example.com/api/GetBestSellingProducts

www.example.com/api/GetTrendingProducts

Which is the best approach for rest api design – single or multiple call, explain the pros and cons?

Which will take more time to respond to the request?

Best Answer

In Theory the multiple simultaneous calls are more flexible and just as fast.

However, in practice if you load a page, and then load each part of that page, displaying loading spinners all over it until you get the results back the result is slow and disjointed.

For this reason AJAX requests for data should be used sparingly and only when you have a section of the page which is slow to load or needs to be refreshed on a different cycle to the rest of the page. Say a master/detail display, where you want to select an option from the master and display the corresponding detail without reloading the master.

A common design is to keep the separate APIs for coding flexibility and micro-service concerns, but combine the data server side in the website. so that the client needs to make only a single call to its own website. The API calls with appropriate caching should be fast within the data center.

Also, consider having NO client API calls at all. simply generate the HTML server side. Although javascript single page app frameworks push you down the api route. It's usually not the optimal approach for high volume e-commerce sites.

enter image description here