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 @Sisn-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.