Querying Business Partner API of SAP S/4HANA Cloud
In this exercise, you will learn how to query the Business Partner API of SAP S/4HANA Cloud. We will use the Mock Server instead of the actual SAP S/4HANA Cloud.
Prerequisites
For the complete execution of current exercise, you must execute the following activities first, using SAP Business Application Studio:
- Execute the following previous exercises:
- Exercise Creating your Pay-As-You-Go Account in SAP BTP, so that you have a SAP BTP account.
- Exercise Setting up the mock server. You need to have the mock server running on port 8081, eventually start it via the following terminal commands:
Steps
Clone the SAP Cloud SDK Samples repository and checkout the branch tsnestjs_v1.0_query in SAP Business Application Studio
Open SAP Business Application Studio.
Run the following commands in the terminal
Code snippetExpandcd ~/projects git clone https://github.com/SAP-samples/cloud-sdk-learning-journey.git cd cloud-sdk-learning-journey git checkout tsnestjs_v1.0_query
Build and run the project
Open the
cloud-sdk-learning-journey
folder in the workspaceRun the application by executing the following commands in the terminal
Query the Business Partner API of SAP S/4HANA Cloud
Open the requests.http file
Click on Send Request in the requests.http file to submit the GET request for business partners
Verify that the list of business partners are returned from the Mock Server. Note: If the Mock Server is not running, make sure to run the Mock Server on port 8081 by following the prerequisites at the top
Optional: Open .env file in the root folder. Replace the URL property with https://sandbox.api.sap.com/s4hanacloud to retrieve business partners from SAP API Business Hub. Note that you also have to provide the APIKEY
Optional: Review code in the following source files
Note
Check out the SAP Cloud SDK documentation on Executing an OData GET request using SAP Cloud SDK for JavaScript. https://sap.github.io/cloud-sdk/docs/js/tutorials/getting-started/execute-an-odata-requestCode walk-through
File name Comments on code changes business-partner.controller.ts Code logic is as follows...
- Annotate the method getBusinessPartners with @Get() to handle the GET method
- Call the getAllBusinessPartners method of the BusinessPartnerService instance asynchronously
business-partner.service.ts Code logic is as follows...
- Annotate the class BusinessPartnerService with @Injectable() to enable dependency injection
- Call the getAll method to retrieve the business partners
- Use the SAP Cloud SDK fluent API method chaining to provide additional parameters
- Return the list of business partners
requests.http Provides various HTTP requests that can be submitted to query the Business Partner API commands.txt Provides various commands that can be run on the terminal
Optional: Manually re-create the project from zero, without using the git branch, in the folder
~/projects/cloudsdk_bp_ts
.Create a new project by running the following command in the terminal and install dependencies. Select npm when prompted for the package manager
Code snippetExpandcd ~/projects npm i -g @nestjs/cli nest new cloudsdk_bp_ts cd cloudsdk_bp_ts npm install -D @sap-cloud-sdk/generator npm install @nestjs/config @sap-cloud-sdk/http-client @sap-cloud-sdk/resilience @sap-cloud-sdk/odata-v2
Open the cloudsdk_bp_ts folder in the workspace. Edit the main.ts file to listen on port 8080
Create a new controller and service by executing the following command
Code snippetExpandcd ~/projects/cloudsdk_bp_ts nest g controller business-partner nest g service business-partner
Create a folder named service-specifications, at the root of the project (folder cloudsdk_bp_ts).
Browse to the SAP API Business Hub in a separate tab, then login.
Click on API Specification button and click on the download EDMX button. This will download the API_BUSINESS_PARTNER.edmx file
Copy the API_BUSINESS_PARTNER.edmx file into the service-specifications folder
Create a file named options-per-service.json in the service-specifications folder. Copy contents from here...
Generate the BusinessPartner service by running the following command
Code snippetExpandnpx generate-odata-client --input service-specifications --outputDir services --optionsPerService service-specifications/options-per-service.json
Create or update the following files in the table below
File name Type Contents business-partner.controller.ts Path: ~/projects/cloudsdk_bp_ts/src/business-partner
Modified Copy contents from here...
business-partner.service.ts Path: ~/projects/cloudsdk_bp_ts/src/business-partner
Modified Copy contents from here...
app.module.ts Path: ~/projects/cloudsdk_bp_ts/src
Modified Copy contents from here...
.env Path: ~/projects/cloudsdk_bp_ts
New Copy the following contents APIKEY=YOUR_API_KEY
URL=http://localhost:8081/
requests.http Path: ~/projects/cloudsdk_bp_ts
New Copy contents from here...
Build and run the project in
projects/cloudsdk_bp_ts
in the same exact way you did in the previous steps with project inprojects/cloud-sdk-learning-journey/cloudsdk_bp_ts
.