REST API that calls another REST API

19,009

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.

Share:
19,009

Related videos on Youtube

MontyTheMack
Author by

MontyTheMack

Updated on June 04, 2022

Comments

  • MontyTheMack
    MontyTheMack almost 2 years

    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?

  • Shubham Chopra
    Shubham Chopra over 5 years
    Polling the database, again and again, is very inefficient and is considered as anti-pattern
  • chepukha
    chepukha over 4 years
    How about if you are designing some API. Say you have method X and Y. Y calls X and then do something else? Is that a good practice? In my opinion, it's not. Client should call X, then call Y. And Y should not repeat what X did. But I couldn't find any written rule about it. Also, is there any case when it's ok to have Y calling X?
  • chepukha
    chepukha over 4 years
    actually, found this to support my argument en.wikipedia.org/wiki/Single_responsibility_principle
  • Nicholas Shanks
    Nicholas Shanks over 4 years
    @chepukha that depends on if X and Y are considered to be at the same "level" or not. it's ubiquitous for an API to call a lower-level API to get work done that is part of the necessary functioning of that API. Does Y depend on X? or is it ancillary? take an area() API for example—it needs to call the multiply() API to perform its function. That does not violate SRP.