Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
showkath_naseem
Product and Topic Expert
Product and Topic Expert
11,539

Introduction


In this blog post, we will learn How to Connect to Cloud Services From CAP Application Locally

Though our SAP CAP Official Document is clearly explained hybrid-testing, I would like to illustrate with an use-case  “Connect to Northwind Cloud Service Locally in CAP Node JS Application using Function Import”

In General  Service can be from any external remote system or SAP System like S/4 HANA

For Example Business Partner OData service in SAP S/4HANA

Reference :  Connect to Remote Services from Local 

I assume you already aware how to integrate any Remote Service on BTP

  • If it is Cloud Service for example (S/4 HANA Cloud ) then you need to use BTP Destination service

  • If it is On Premise System which requires VPN to connect to then you need to use BTP BTP Connectivity service & SAP Cloud Connector


 

Reference : CAP BTP Connection - Destination

Target Audience


Developers

Prerequisites


I assume you already configured required services for CAP APP

Destination

Authorization and Trust Management Service XSUAA  Service





Sample Source Code


Below is sample CAP Node JS Source code to connect to BTP Destination


this.on("getRemoteNWData", async req => {

letresponse;

constcpqservice = awaitcds.connect.to('ext_northwind')

try {


LOG.info("start demo Northwind connecting")

response = awaitcpqservice.send({

query:'GET /v2/northwind/northwind.svc/Products',

headers: {

'Content-Type':'application/json'

}

})

LOG.info("end Northwind connection ")

returnresponse;

} catch (error) {

req.error(500, error.message)

}

});




...

I hope you know further code & how to prepare CAP Application to communicate to BTP cloud services



Scenario :  Use Local Application Destinations (Non BTP)


For Testing ,If you don’t want to use SAP BTP destinations, you can also define destinations, which means the URL, authentication type, and additional configuration properties, in your application configuration or code.

As of now only Basic Type authentication Destination works with Local CAP Destination


Refer : https://cap.cloud.sap/docs/guides/using-services?q=destination#app-defined-destinations




Scenario : On-Premise Connectivity


Few. SAP systems are located within a company's internal network ,Example : S/4HANA On-Premise  , SAP ERP (CRM) ... . They are not exposed to the internet. The SAP Cloud SDK provides helpful features to allow connecting to on-premise systems.


Reference : https://sap.github.io/cloud-sdk/docs/js/features/connectivity/on-premise




Consuming Services in CAP APP 




You can easily test your CAP application using a local database and mock ups. But at some point, you’re going to want to test with real cloud services. Of course, you can also deploy your application to the cloud & test but if you want to connect to Real Service locally before deploying to the cloud then you may want to run some ad-hoc tests,CAP Provides below options


Approach 1



  • Create default-env.json

  • Copy VCAP_SERVICES Values from BTP Environment Variable of your application

  • run cds watch from terminal


 


Note :

https://cap.cloud.sap/docs/node.js/cds-env?q=default-env.json#in-default-envjson

Recently The use of default-env.json is deprecated



Approach 2 [Hybrid Testing]


 

A new approach to bind BTP services locally is to use cds bind

Execute below command with your service instance name ,service key as shown below from Terminal

cds bind -2 servicename:keyname
#1) cds bind -2 sap-organickart-destination-instance:sap-organickart-srv-key



#2)  cds bind -2 sap-organickart-auth:sap-organickart-auth-key


Alternatively you can also bind Multiple BTP Service Instances with One Command as shown below in shortcut by comma separated service instances

cds bind -2 servicename1,servicename2

Note : This implies that a service key exists with the suffix -key. In this example: sap-organickart-srv-key.

Example :
cds bind -2 sap-organickart-destination-instance,sap-organickart-auth

After cds bind  execution ,it will generate cdsrc-private.json file in project as shown below


Unlike Default-env.json which is un secure to maintain service sensitive data ,Running the "cds watch" in hybrid mode, which allows you to retrieve service credentials from BTP CF/Kyma on runtime

#3)  cds watch --profile hybrid

cds serve all --with-mocks --in-memory? --profile hybrid
watching: cds,csn,csv,ts,mjs,cjs,js,json,properties,edmx,xml,env,css,gif,html,jpg,png,svg...
live reload enabled for browsers
[bind] - resolving cloud service bindings...
[bind] - bound auth to Cloud Foundry service sap-organickart-auth key sap-organickart-auth-key
[bind] - bound destinations to Cloud Foundry service sap-organickart-destination-instance key sap-organickart-srv-key


 

 

Now you can browse your function import using local host port which fetch Products from NorthWind Remote  Services

Example : https://port4004-workspaces-ws-x.eu10.applicationstudio.cloud.sap/auth/getRemoteNWProducts()


 

Reference

Consuming Services


You can also use cds bind to set up testing to any BTP Service Instance

For Example :

 


 

Thank you for reading! If you enjoyed this post, please consider giving it a ‘'Like' ,'Share'  it with your friends and followers, and 'Follow' me for more content like this. Your support means a lot to me!

 


Happy Testing  🙂 !
7 Comments
massimiliano_cardosi
Product and Topic Expert
Product and Topic Expert
0 Kudos
hi Showkath,
I've tried doing what you have explained, this command "cds bind -2" has failed at the beginning because "-2" option is not available in this environment:
@sap/cds: 5.8.4
@sap/cds-compiler: 2.13.8
@sap/cds-dk: 4.1.5
@sap/cds-foss: 3.1.1
@sap/hana-client: 2.12.22
@sap/hdi: 3.0.2
@sap/hdi-deploy: 4.2.3
@sap/odata-commons: 2.4.2
Node.js: v14.21.1
Opus: 1.0.0

After upgrading "cds" component to last available version, I've been able to run "cds bind -2" - question: what's the first cds version where "bind" is available?
We're in trouble updating cds to last version so we have to proceed version by version

Many thanks
Massimiliano
showkath_naseem
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi massimiliano.cardosi ,

 

Good Question

cds bind -2 feature available from 6.0.4

"@sap/cds": "^6.0.4",
"@sap/cds-dk": "^6.0.3",
can you try this versions also try to update cds-complier version , post your error 
CAP Latest  versions are
"@sap/cds": "^6.4.0",
"@sap/cds-dk": "^6.4.0",
"@sap/cds-compiler": "^3.5.0",
Regards,
Showkath.

 

massimiliano_cardosi
Product and Topic Expert
Product and Topic Expert
Hi Showkath,
installing all components with "latest" version issue is solved

Thanks

Massimiliano
showkath_naseem
Product and Topic Expert
Product and Topic Expert
0 Kudos
Thanks massimiliano.cardosi for confirming it worked.
MariamIdman
Product and Topic Expert
Product and Topic Expert
0 Kudos
Thank you for the great detailed blog post!
LuizGomes
Participant
0 Kudos
I've been trying to work on localhost (VSCODE) but without success, is this a limitation of this new approach?
LucasBorin
Product and Topic Expert
Product and Topic Expert
0 Kudos

Is it possible to bind connectivity? I need it to access a S4 system hidden behind the company proxy.