Technical Articles
CAP Wrapper for SAP Graph API
Overcoming Data Integration Challenges: Introducing SAP Graph as a Unified Solution for Multiple Business Systems
- Business Problem — You got multiple business systems for example SAP S/4 HANA Cloud / SAP SuccessFactors / SAP S/4 HANA System on Premise but bringing data from these systems using one Unified service is a challenge but that Change got a beautiful solution and that is SAP Graph
- SAP Graph — “Streamlining functionality, we provide developers with a single connected and unified view of all their business data through a single endpoint for all purposes.”
Resources for learning:
- Thank you SAP HANA Academy for such a great Youtube Series on SAP Graph please follow it for building your solutions end-2-end.
- You can also follow SAP Graph tag on SAP Community and blog series on SAP Graph
Pre-requisites:
- BTP Cockpit trial account or Paid Account
- SAP graph Free tier Services ( Check SAP Discovery Center for more detail )
- Create Graph instance and Key Service Key ( Download and Save it )
- Graph Instance parameters – ( Download and save it ) More details on HANA Academy GitHub – > SAP Graph
- Go to PowerShell or VS Code and install graphctl tool for graph connect
Setup Graph Environment:
- Get your free tier service from SAP Discovery center and add the entitlement in your BTP Subaccount
- Download your graph key and store it somewhere in your system ( I will not go into detail as there are already few blogs and series on how to set it up but this is needed for start hence adding it for connect)
Sandbox API Destination Configuration:
- Get your API Key and API url from SAP’ API Sandbox system.
- Business Acceleration Hub – https://api.sap.com/api/businesspartner/overview
- Login with your credentials – go to -> Settings -> Show API Key – Copy and Save it
- Somewhere Create destination in BTP Cockpit Destination Config will look like this :-
Login to BTP Subaccount : ( CF LOGIN -a <API> )
- Connect your BTP Subaccount – Enter your registered email ID and Password and select working Dev Space
- Install graphctl tool using npm : Execute command – npm i @sap/graph-toolkit
Login to SAP GRAPH Instance : ( GRAPHCTL LOGIN -F CREDENTIALS)
Execute:
- graphctl login -f shagraph-creds.JSON.txt
- ( Note: This is going to open Web Browser login with your BTP Credentials and then close the window )
Graph Generate & Activate Config:
Execute:
- graphctl generate config -f “C:\Users\Yoga\Desktop\graph\instance-parameters.json” -i cxsales
- graphctl activate config -f “C:\Users\Yoga\Desktop\graph\instance-parameters.json” –force
Graph List Config: ( GRAPHCTL LIST CONFIG ):
Graph is available for consumption 
“The graph is now operational and ready to serve. Please generate your service keys on the graph instance and proceed to test the graph API”.
We are pleased to inform you that sap.graph is successfully serving the Cloud for Customer entities. You have the flexibility to select any entity and seamlessly integrate or link it directly to your application. This exemplifies the power of SAP Graph – an incredible technology that yields remarkable results.
Test your GRAPH API:
Create One GET Request in POSTMAN Client and Enter your Graph URL ( https://<host>/api/v1/sap.graph )
Choose Authorization: Oauth 2.0 Credentials
Execute GET Request on Graph Endpoint:
Lets GET Company Information from Graph
Outcome:
“Graph Environment Setup: Integration with SAP Graph and SAP CX Sales Cloud APIs, Business Data Graph Generation, and Successful Testing of Graph APIs in POSTMAN Client”
Congratulations on your successful setup of the graph environment! It’s great to hear that you have successfully integrated SAP Graph with SAP CX Sales Cloud APIs. Additionally, generating a business Data Graph for consumption and conducting successful tests of the Graph APIs in the POSTMAN client demonstrate significant progress. Well done!
Part 2: CAP Wrapper for Graph API:
Architecture
“The architecture diagram illustrates the connection of two business systems to SAP Graph. SAP Graph is exposed as an endpoint (referred to as a destination in the Business Technology Platform), and a CAP App consumes that destination to access and expose the business data.”
What all we need ?
- Node in your VS Code / BAS
- Destination for Graph endpoint in BTP Cockpit
- Authentication , Destination and connectivity services in CAP
Create Destination in BTP Cockpit
- Obtain the Client ID and secret from the Graph Service Key file.
- Log in to the BTP Cockpit.
- Navigate to the “Destinations”.
- Click on “New Destination” .
- Provide a name for the destination (e.g., “SAP Graph API”).
- Fill in the required details, such as the URL endpoint of the SAP Graph API.
- In the “Authentication” section, select the appropriate authentication method (e.g., OAuth2 Client Credentials).
- Enter the Client ID and secret obtained from the Graph Service Key file in the respective fields.
- Save the destination configuration.
Create CAP Application in VS Code
- Create Simple CAP Application either in BAS or in VS Code , I am using VS Code
- Lets Create one Folder in your workspace in VS Code Name it like getBusinessData
- Open VS Terminal and execute
cds init
cds add mta
Add these in package.json Add this CDS Section
"cds": {
"requires": {
"shagraph": {
"kind": "odata-v4",
"credentials": {
"destination": "shagraph",
"path": "/sap.graph"
}
}
}
}
Add sales.cds file in srv folder and add the below snippet , I have created on function import which will be returning the dynamically generated results from SAP Graph API.
@protocol: 'rest'
service root {
@open
type object {};
function graph(name:String) returns array of object;
}
Add sale.js file in srv folder
const get = require("./handlers/GET/getSales");
module.exports = cds.service.impl(async function () {
this.on("graph", get.graph);
});
handler for graph.
const graph = async (req) => {
const srv = await cds.connect.to("shagraph");
if (req.data.name !== null && req.data.name != undefined) {
req.query = SELECT.from(req.data.name);
} else {
req.query = SELECT.from(`sap.graph`);
req.query.SELECT.from.ref[0] = "";
}
return await srv.run(req.query);
};
module.exports = {
graph
};
Execute these Commands
cds build
"BAS Does this automatically but in VS Code we can run these commands to Bind the Application"
cds bind -2 destination-srv:key -- Bind your App with Destination using Key
cds bind -2 uaa-srv:key Bind your App with Authentication service using Key
cds watch --profile Hyrbrind ( to run your App locally )
End-2-end execution of CAP Wrapper for Graph API:
Note: Just created this small Video for executing calls to graph API instead putting screenshots 🙂
Here CAP Endpoint is serving first for sap.graph and after that we are passing entities exposed using business data graph and it comes to an end then.
I hope you have enjoyed the reading and please feel free to post your comments or questions if any 🙂
Thanks Shivam
Hi, I'm getting this error after running cds watch in the CAP application on BAS
Any Ideas on how to fix this?
Yeah clearly — it is not able to navigate to the function mentioned in Specified Path.
Correcting that navigation path will fix this error
Hi shivam shukla
Thanks for writing this very interesting blog!
Graph recently became generally available as a capability in SAP Integration Suite: https://blogs.sap.com/2023/07/12/graph-new-in-sap-integration-suite/
It is no longer possible to create new instances of the standalone Graph BTP service. There is new supporting documentation: https://help.sap.com/docs/graph/graph
Please consider updating some of the links in your blog, which no longer work due to the change. I am happy to support with rewriting the Graph setup and configuration parts of the blog, which have also changed (email - stephanie.lewellen@sap.com).
Best regards,
Stephanie (product manager @ Graph)
Hi Yes Sure ,
I will try to find out some time during this week and update this.
Thanks,
Shivam