Exercise: Querying SAP APIs in a TypeScript NestJS Application

Objectives

After completing this lesson, you will be able to:

  • Querying SAP APIs in a TypeScript NestJS application

Querying SAP APIs in a TypeScript NestJS application

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:

  1. Execute the following exercises:
    • Creating your free trial account in SAP BTP
    • Setting up the mock server, so that you have the Mock server program available in the cloud-s4-sdk-book folder.
  2. Run the following commands in a terminal, in case you didn't do it already in previous exercises, to clone the Git repository for the course:
    Code snippet
    cd ~/projects
    git clone https://github.com/SAP-samples/cloud-sdk-learning-journey.git
    Expand
  3. Choose FileOpen Folder and open the /home/user/projects/cloud-sdk-learning-journey/ folder.

Steps

  1. Checkout the branch tsnestjs_v1.0_query in SAP Business Application Studio

    1. Run the following commands in the terminal

      Code snippet
      cd ~/projects/cloud-sdk-learning-journey
      git checkout -f tsnestjs_v1.0_query
      Expand
  2. Build and run the project

    1. Run the application by executing the following commands in the terminal

      Code snippet
      cd cloudsdk_bp_ts
      npm i
      npm run start:dev
      Expand
  3. Make sure that the Mock Server is up and running as described in the related previous exercise.

    1. In case the Mock server is installed but not started, you need to open a new terminal window and run the following commands:

      Code snippet
      cd ~/projects/cloud-s4-sdk-book
      npm run start
      Expand
  4. Query the Business Partner API of SAP S/4HANA Cloud

    1. Open the requests.http file in the cloudsdk_bp_ts folder

    2. Click on Send Request in the requests.http file to submit the GET request for business partners

    3. Stop the running application using Ctrl + C in the corresponding terminal window.

  5. Optional: Query the Business Partner API of the sandbox in api.sap.com.

    1. Access https://api.sap.com/api/API_BUSINESS_PARTNER. Login and get your API Key.

    2. 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

    3. Re-build and run the application:

      Code snippet
      cd ~/projects/cloud-sdk-learning-journey/cloudsdk_bp_ts
      npm run start:dev
      Expand
    4. Re-test the Business Partners query in the requests.http file.

    5. Stop the running application using Ctrl + C in the corresponding terminal window.

  6. Optional: code walk-through.

    1. Review code in the following source files:

      File nameComments on code changes
      business-partner.controller.ts

      Code logic is as follows...

      1. Annotate the method getBusinessPartners with @Get() to handle the GET method
      2. Call the getAllBusinessPartners method of the BusinessPartnerService instance asynchronously
      business-partner.service.ts

      Code logic is as follows...

      1. Annotate the class BusinessPartnerService with @Injectable() to enable dependency injection
      2. Call the getAll method to retrieve the business partners
      3. Use the SAP Cloud SDK fluent API method chaining to provide additional parameters
      4. Return the list of business partners
      requests.httpProvides various HTTP requests that can be submitted to query the Business Partner API
      commands.txtProvides various commands that can be run on the terminal
      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-request
  7. Optional: Manually re-create the project in ~/projects/cloudsdk_bp_ts .

    1. Create a new project by running the following command in the terminal and install dependencies. Select npm when prompted for the package manager

      Code snippet
      cd ~/projects
      npm i -g @nestjs/cli
      nest new cloudsdk_bp_ts (select npm)
      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
      Expand
    2. Open the cloudsdk_bp_ts folder in the workspace.

    3. Edit the main.ts file in the src folder to listen on port 8080

    4. Create a new controller and service by executing the following command

      Code snippet
      cd ~/projects/cloudsdk_bp_ts
      nest g controller business-partner
      nest g service business-partner
      Expand
    5. Create a folder named service-specifications, at the root of the project (folder cloudsdk_bp_ts).

      Code snippet
      mkdir service-specifications
      Expand
    6. Browse to the https://api.sap.com/api/API_BUSINESS_PARTNER/overview in a separate tab, then login.

    7. Click on API Specification button and click on the download EDMX button. This will download the API_BUSINESS_PARTNER.edmx file

    8. Copy the API_BUSINESS_PARTNER.edmx file into the service-specifications folder

    9. Create a file named options-per-service.json in the service-specifications folder. Copy contents from here...

    10. Generate the BusinessPartner service by running the following command

      Code snippet
      npx generate-odata-client --input service-specifications --outputDir services --optionsPerService service-specifications/options-per-service.json
      Expand
    11. Create or update the following files in the table below

      File nameTypeContents
      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 contents from here...

      Replace the APIKEY value with your API Key.

      requests.http

      Path: ~/projects/cloudsdk_bp_ts

      New

      Copy contents from here...

    12. Build and run the project in projects/cloudsdk_bp_ts:

      Code snippet
      cd ~/projects/cloudsdk_bp_ts
      npm i
      npm run start:dev
      Expand
    13. Stop the running application using Ctrl + C in the corresponding terminal window.

Log in to track your progress & complete quizzes