Search code examples
loopbackjsstronglooploopback4

Call an existing API using Loopback 4


I have a pre-developed API connected to my backend. I am using loopback 4 as a data provider for my react application. I want to call the API using Loopback and get the result in my application. I went through the loopback 4 documentation calling other API's but found it a bit confusing. Data returned by my API is :
{ "name": "xyz", "place": "xyz", "email": "xyz" }

I want this data to be returned to my application using Loopback. I do not want to make a direct API call from my React application. I want Loopback to make all the API calls.
Any help is much appreciated.


Solution

  • A LoopBack 4 application is still a Node.js application. Hence, you can make the request directly in the Controller via the built-in http module, or by importing another package (e.g. request).

    From there, the result can be mutated (if needed) then returned within the Controller.

    If API is commonly-used across numerous Controllers, then it is recommended to refactor the requests behind a Service. Services benefit from being bootable at runtime via this.boot() and injectable into controllers using @inject or @service.