Consume Destination
In this example, we will fetch data from any API by configuring it as a Destination in SAP BTP and then consuming it through a CAP application.
To achieve this, we will follow these steps-
- Create Destination on BTP Cockpit
- Create Service Binding on CAP application
- Install dependencies
- Consume Destination
Step 1 - Create Destination on BTP Cockpit
Create a Destination in the SAP BTP subaccount where your application will be deployed. Configure the Destination based on the requirements of the API, including the necessary endpoint, authentication details, and any other credentials required to establish the connection.
In our case, we created Destination with properties like-
| Property | Value |
|---|---|
| Destination Name | Log_Destination |
| Type | HTTP |
| URL | endpoint you want to consume |
| Authentication | OAuth2ClientCredentials |
| ProxyType | Internet |
| ClientId | credentials required to generate access token |
| ClientSecret | credentials required to generate access token |
| TokenServiceURL | URL to generate access token |
Step 2 - Create Service Binding on CAP application
Service binding gives your CAP application permission and the credentials to access the Destination service itself.
Without the service binding, the application has no way to retrieve or use the Destination configuration, even though the Destination exists in the subaccount.
To achieve this, we can declare the Destination Service requirement under modules requires section in mta.yaml file like -
- name: cap-application-destination-serviceAnd then we can define the service details under resources section on mta.yaml file like -
# Define Destination Service Resource detials
- name: cap-application-destination-service
type: org.cloudfoundry.managed-service
parameters:
service: destination
service-plan: lite
service-name: cap-application-destination-instance # <-- Service instance name
config:
HTML5Runtime_enabled: true
Step 3 - Install Dependencies
We need connectivity and http-client dependencies from sap-cloud-sdk and can install using below command -
npm install @sap-cloud-sdk/connectivity @sap-cloud-sdk/http-clientStep 4 - Consume Destination
Now, we can consume the destination through custom logic.
Make sure to import the required SAP Cloud SDK dependencies into your custom logic js file-
import SdkConnectivity from '@sap-cloud-sdk/connectivity';
import SdkHttpClient from '@sap-cloud-sdk/http-client';And here is the sample code to consume the destination-
const myDestination = await SdkConnectivity.getDestination({
destinationName: 'Log_Destination'
});
if (!myDestination) {
return request.error({ code: 404, message: 'Destination not found' });
}
const endpoint = '/auditlog/v2/auditlogrecords?time_from=2026-07-10T00:00:00&time_to=2026-07-10T00:03:00' ;
console.log('Destination retrieved:', myDestination.name);
const destinationResponse = await SdkHttpClient.executeHttpRequest(myDestination, {
url: endpoint,
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
// data: JSON.stringify(requestBody)
}
);
if (!destinationResponse) {
return request.error({ code: 404, message: 'No response from Destination' });
}
const data = destinationResponse.data;- As a best practice, store the Destination name and API endpoint in User-Provided Variables, and access them in your CAP application through environment variables.
- This approach improves maintainability and avoids hardcoding configuration values in your application code.
!!! Its Done !!!