Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
filipebst
Product and Topic Expert
Product and Topic Expert
In this blog post I will guide you in how to integrate and consume REST APIs from Business Technology Platform (BTP) with Cloud Application Programming Model (CAP). I will walk trough the essentials, demystify the process, and equip you with practical insights to enhance you SAP CAP development.

Pre-requisites: You already have a REST API configured in BTP and an application deployed.

 

Understanding SAP BTP Connectivity


There are two different services that SAP BTP offers. The Connectivity Service and the Destination Service.

Connectivity Service: You want to use this service to connect your CAP Application to on-premise systems.

Destination Service: You want to use this service to connect your CAP Application to remote services.

For this blog post I will use the Destination Service as an example and I will be using Node.js.

 

Step by step


Step 1: Bound the Destination Service to your Node.js Application

There are multiple ways to do this, I will give an example using the MTA Development Descriptor.


Find your Node.js App module and in the 'requires' section add your destination service. The name of the destination service can be anything, you just need to make sure that it has the same name as the one that you will use to define the resource. The resources section is where you define your BTP Services, so this is where you will define the Destination Service.


 

Step 2: Install the http-client package and build your API request

I use the executeHttpRequest method to trigger a HTTP request to my REST API. This method is from the SAP Cloud SDK - HTTP Client. You can install it as a dependency by using the following command in your terminal at the 'srv' folder level.

npm install @Sap-cloud-sdk/http-client

You can learn more about the sap-cloud.sdk/http client package here: https://www.npmjs.com/package/@sap-cloud-sdk/http-client

You can learn more about the executeHttpRequest method here: https://sap.github.io/cloud-sdk/docs/js/features/connectivity/http-client#executehttprequest

After installing it, in your service handler (JavaScript file) add it as requirement and build your function that triggers a request to your API. Here is an example of a POST request to the destination:

const { executeHttpRequest } = require('@sap-cloud-sdk/http-client');

async function triggerDestinationTest() {
try {
return await executeHttpRequest(
{
destinationName: 'my_destination'
}, {
method: 'POST',
url: "/your-api-endpoint",
data: yourData
}
);
} catch (e) {
console.error(e);
}
};

destinationName: This has to be the same name of your destination in BTP.


 

Step 3: Configure your package.json file

In your package.json file at root level you need to add the following configuration.


After these steps you should be able to consume your REST API Destination from BTP in your CAP Application both locally and also on deployed versions.

Conclusion


As I conclude this tutorial, you should now feel more informed about consuming REST APIs in SAP CAP using BTP. It should be an easy and fast process. I've covered some fundamentals and shared some tips based on my personal experiences to enrich your understanding.

 

Thank you.
4 Comments
gregorw
Active Contributor
Hi Filipe,

is the "kind": "destination" that you specify something new that didn't made it yet to the documentation? In the list of documented kind values at Import API Definition I can't find it. Also I would suggest that when you're talking about consuming REST APIs with CAP you should show how it's done natively with CAP as documented here: Execute Queries with Node.js. You showed the direct use of SAP Cloud SDK.

Best Regards
Gregor
filipebst
Product and Topic Expert
Product and Topic Expert
0 Kudos
Hello Gregor,

Thank you for your feedback, I will definitely follow your suggestion.

Regarding the "kind":"destination" specification, I was only able to consume the REST API destination when running the application locally if I matched with the parameter "kind" from the system service name.


If I use one of the "kind" values that are in the Import API Definition , like the "rest" value I cannot bind "my_destination" in the RUN CONFIGURATIONS tab because it does not match the "destination" service name value that is set in the VCAP_SERVICES and consequently I'm not able to test it when running the application locally. That was the reason that forced me to set the parameter "kind" equal to "destination".



 

Thank you.

 

Best Regards,

Filipe
gregorw
Active Contributor
0 Kudos
Hi Filipe,

I do my CAP development mostly locally in VS Code so I can't help with the run configuration. I would suggest you ask regarding that in the BAS Q&A.

Best Regards
Gregor
Bhaktavatsalam
Employee
Employee
0 Kudos

Hello CAP Gurus. 

 

Not sure if this is right forum to ask this question....pls pardon me for that - it would be great if you can point me in right direction

I want to consume a RESTful web service from Java based SpringBoot service built in CAP framework. 

That REST API that i want consume is a standard RESTful service not Compliant with ODATA and not build using CAP framework it is totally external- Pls point me to a resource where I can take a look at how to accomplish this.