SAP CAP: Consume SAP API Hub Sandbox Data as an external service Part- 2

What: 

This blog post is about consuming external services from BTP after deploying your service/application. This is the continuation of the previous post Part- 1.

Here we are going to deploy and access the same external service from the cloud foundry

How:

Deploying Application

Now we are going to deploy our CAP project in SCP Cloud Foundry

Before deploying using the command, we need to build the project.

$ cds build/all

Create mta.yaml file by using the below command.

$ cds add mta

MTA file will look like below –

---
_schema-version: '3.1'
ID: BusinessPartner
version: 1.0.0
description: "A simple CAP project."
parameters: enable-parallel-deployments: true
build-parameters: before-all: - builder: custom commands: - npm ci - npx -p @sap/cds-dk cds build --production modules: - name: BusinessPartner-srv type: nodejs path: gen/srv parameters: buildpack: nodejs_buildpack build-parameters: builder: npm-ci provides: - name: srv-api # required by consumers of CAP services (e.g. approuter) properties: srv-url: ${default-url}

As this file does not hold enough information to run the application in the Cloud foundry, We need two resources binding with our service application to up and running in CF.

  1. Destination service
  2. xsuaa service

Let create the Destination service in BTP

Login to your account and search for destination under service marketplace.

Click on create Button and provide the Instance name. In my case, I have created demo-destination instance.

Service: Destination Service
Plan: lite
Runtime Environment: Cloud Foundry
Space: dev
Instance Name: demo-destination

Click on demo-destination instance and configure a new destination

URL=https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_BUSINESS_PARTNER/
Name=API_BUSINESS_PARTNER
ProxyType=Internet
Type=HTTP
Authentication=NoAuthentication
Description=Business Partner SandBox API

Now we have created one service instance for our application. Let’s create xs-security.json for uaa service instance.

Run the below command

$ cds compile srv/ --to xsuaa > xs-security.json

We have to update our MTA file to make binding these two services with our application

---
_schema-version: '3.1'
ID: BusinessPartner
version: 1.0.0
description: "A simple CAP project."
parameters: enable-parallel-deployments: true
build-parameters: before-all: - builder: custom commands: - npm ci - npx -p @sap/cds-dk cds build --production
modules: - name: BusinessPartner-srv type: nodejs path: gen/srv parameters: buildpack: nodejs_buildpack build-parameters: builder: npm-ci provides: - name: srv-api # required by consumers of CAP services (e.g. approuter) properties: srv-url: ${default-url} requires: - name: demo-destination - name: businesspartner-uaa resources: - name: demo-destination type: org.cloudfoundry.existing-service parameters: service-name: demo-destination - name: businesspartner-uaa type: org.cloudfoundry.managed-service parameters: path: ./xs-security.json service: xsuaa service-plan: application config: xsappname: businesspartner-${org}-${space} tenant-mode: dedicated

We have to change the credentials to access the destination service in the package.json file.

"cds": { "requires": { "API_BUSINESS_PARTNER": { "kind": "odata-v2", "model": "srv/external/API_BUSINESS_PARTNER", "[development]": { "credentials": { "url": "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_BUSINESS_PARTNER", "headers": { "APIKey": "<api-key>" } } }, "[production]": { "credentials": { "destination": "API_BUSINESS_PARTNER" } } }, "uaa": { "kind": "xsuaa" } } }

When you are going to access sandbox data for your local development you have to use “[development]” credentials.

Follow the below commands to deploy your cap project –

$ cds build/all
$ mbt build -t ./
$ cf deploy BusinessPartner_1.0.0.mtar

BusinessPartner_1.0.0.mtar, is generated after your MBT build command.

Now check in the BTP under the application section. The application has been deployed and it is has been Started.

Click on the application URL.

Click on A_BusinessPartner

Oops!! It was working fine in our BAS.

The reason is we are not passing APIkey to access the sandbox data. In package.json file we pass only destination name inside “[production]”, not the apikey.

There are two ways I found how to use API key for the production environment.

  1. Set the apikey in as an environment variable and use it in your code. In every request send it in as a header request.
  2. We can modify the destination configuration in our destination service.

Here I am we are going to follow the second approach.

Open the destination service and an additional parameter

URL.headers.apiKey – <api-key>

Now again click on A_BusinessPartner

Response is coming from the API hub sandbox.

Now you know how to access API Hub sandbox data from a cap application