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-

PropertyValue
Destination NameLog_Destination
TypeHTTP
URLendpoint you want to consume
AuthenticationOAuth2ClientCredentials
ProxyTypeInternet
ClientIdcredentials required to generate access token
ClientSecretcredentials required to generate access token
TokenServiceURLURL 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 -

mta.yaml
- name: cap-application-destination-service

And then we can define the service details under resources section on mta.yaml file like -

mta.yaml
# 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 -

terminal
npm install @sap-cloud-sdk/connectivity @sap-cloud-sdk/http-client

Step 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-

custom-service.js
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-

custom-service.js
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;
Note
  • 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 !!!