API Calls – Best Practices for Calling Multiple APIs and Data Manipulation

apijavascriptpythonweb-applications

I am building a web application that makes use of multiple API's (Google Maps, Weather APIs, and more). I am using Python + Flask on the backend, and Javascript (probably with React, I have not decided yet) on Front end.

The application will take the results of these various API query's and then give the user a simple, summarized result, which involves a fair amount of calculations and conditional logic based on the API response.

What the best practice to do this?

If I do all the API calls on the client side, and I deal with calculations and such there too, that feels cumbersome.

If I do it all on the backend, the calls are not asyncronous and so I am worried the calls might take a while. Though in my use case a 2-3 second wait is probably acceptable as long as there is a "Loading" message. This is what I currently have (halfway through writing all the API calls and the needed calculations)

I could also do the API calls on the client, pass it to the server for calculations, and then sent the results back to the front end. Not sure if that's a practical solution or if it would really solve anything.

Best Answer

You should be using a server to manage and call external APIs then combine the responses as necessary and return them to the client.

  • You shouldn't be hijacking your clients resources to call all these services and process results depending on how intensive it is. You also don't know what kind of connection your client has, what takes you a few seconds on a good connection could be significantly longer.
  • You can't guarantee your client can access all the various API endpoints which requires more work to fallback gracefully. Corporate environments are a good example where this is common.
  • You may be able to take advantage of caching some calls across users.

The general goal should be to minimize the number of requests and amount of data transferred. Third party requests should also be minimized unless they can reduce the amount of data transferred or provide similar benefit (ie CDNs).

Related Topic