Skip to Content
Technical Articles
Author's profile photo Debashish Das

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

 

 

NOTE on 3rd December 2022: 

If you follow this blog you need to lower the version “@sap/cds”: “^5” in package.json file. 

 

 

 

Assigned Tags

      8 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Ismael Muela
      Ismael Muela

      Hello,

       

      Thank you for the tutorial. I have a little problem in the last step because when I deploy my app and I test it the server send me a 401 Unauthorized error. I think the XSUAA doesn't work properly but I'm not sure. Could you help me?

       

      Thank you very much,

      Ismael

      Author's profile photo Debashish Das
      Debashish Das
      Blog Post Author

      Hello Ismael Muela

      Please validate your configuration in BTP destination service and apikey. 

      Author's profile photo Maqsood Ahammed
      Maqsood Ahammed

      Hi Debashish,

      Even am receiving the same 401 - unauthorized error after deployment. Have crossed checked all the configurations multiple times, could you pls share any insights here ?

      Locally it is working fine with apiKey.

      Reproduce the issue:

      1. Access the Cloud Foundry URL
      2. Click on A_BusinessPartner => 401 unauthorized error is displaying

      Regards,

      Maq

      Author's profile photo Debashish Das
      Debashish Das
      Blog Post Author

      Hi Maqsood,

      Do changes in your mta.yml file. Delete the previous destination resource.

      resources:
       -  name: BusinessPartner-destination
          type: org.cloudfoundry.managed-service
          parameters:
            service: destination
            service-plan: lite

      This will create a BusinessParnter-destination service instance. Later you have to create a  New Destination same as before process.

       

       

       

       

      Author's profile photo Maqsood Ahammed
      Maqsood Ahammed

      Hi Debashish,

      Thanks for the detailed response.

      I tried all the mentioned options but no luck, issue remains same. Are there any other steps to cross check ? this looks to be little strange.

      1. Existing destination instance is deleted
      2. Added the destination as you mentioned
      3. Redeployed
      4. Maintained the destination "API_BUSINESS_PARTNER"

       

       

       

      Author's profile photo Maqsood Ahammed
      Maqsood Ahammed

      Added the mta yaml screenshot

      API BUSINESS PARTNER:

       

      Author's profile photo Debashish Das
      Debashish Das
      Blog Post Author

      Hi Maqsood,

      You have to lower the version of “@sap/cds”: “^5” in package.json file.

      Run npm install > Build > deploy.

       

      Author's profile photo Maqsood Ahammed
      Maqsood Ahammed

      Hi Ismael,

       

      Were you able to fix the issue ?

       

      Thanks..