Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST API that calls another REST API

Is it proper programming practice/ software design to have a REST API call another REST API? If not what would be the recommended way of handling this scenario?

like image 856
MontyTheMack Avatar asked Dec 21 '17 01:12

MontyTheMack


People also ask

Can I call one API from another API?

Yes, an API can do whatever you want it to.

How you get the response from one API and send to another API?

One way to do is, to have 2 requests created (GET, POST). Within GET request you, transform your response to the request structure of POST request using javascript and then store it to either, global, environment or collection variables.


1 Answers

If I understand your question correctly, then YES, it is extremely common.

You are describing the following, I presume:

Client makes API call to Server-1, which in the process of servicing this request, makes another request to API Server-2, takes the response from Server-2, does some reformatting or data extraction, and packages that up to respond back the the Client?

This sort of thing happens all the time. The downside to it, is that unless the connection between Server-1 and Server-2 is very low latency (e.g. they are on the same network), and the bandwidth used is small, then the Client will have to wait quite a while for the response. Obviously there can be caching between the two back-end servers to help mitigate this.

It is pretty much the same as Server-1 making a SQL query to a database in order to answer the request.

An alternative interpretation of your question might be that the Client is asking Server-1 to queue an operation that Server-2 would pick up and execute asynchronously. This also is very common (it's how Google crawls your website, for instance). This scenario would have Server-1 respond to Client immediately without needing to wait for the results of the operation undertaken by Server-2. A message queue or database table is usually used as an intermediary between servers in this case.

like image 160
Nicholas Shanks Avatar answered Sep 22 '22 03:09

Nicholas Shanks