Exercise: Querying SAP APIs in a TypeScript NestJS Application

Objectives
After completing this lesson, you will be able to:

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

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:

  1. 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:
      Code snippet
      cd ~/projects/cloud-s4-sdk-book
      npm run start
      Expand

Steps

  1. Clone the SAP Cloud SDK Samples repository and checkout the branch tsnestjs_v1.0_query in SAP Business Application Studio

    1. Open SAP Business Application Studio.

    2. Run the following commands in the terminal

      Code snippet
      cd ~/projects
      git clone https://github.com/SAP-samples/cloud-sdk-learning-journey.git
      cd cloud-sdk-learning-journey
      git checkout tsnestjs_v1.0_query
      Expand
  2. Build and run the project

    1. Open the cloud-sdk-learning-journey folder in the workspace

    2. 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. Query the Business Partner API of SAP S/4HANA Cloud

    1. Open the requests.http file

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

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

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

  4. Optional: Review code in the following source files

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

      Code walk-through

      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
  5. Optional: Manually re-create the project from zero, without using the git branch, in the folder ~/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
      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. Edit the main.ts file to listen on port 8080

    3. 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
    4. Create a folder named service-specifications, at the root of the project (folder cloudsdk_bp_ts).

      Code snippet
      mkdir service-specifications
      Expand
    5. Browse to the SAP API Business Hub in a separate tab, then login.

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

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

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

    9. 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
    10. 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

      NewCopy the following contents

      APIKEY=YOUR_API_KEY

      URL=http://localhost:8081/

      requests.http

      Path: ~/projects/cloudsdk_bp_ts

      New

      Copy contents from here...

    11. Build and run the project in projects/cloudsdk_bp_ts in the same exact way you did in the previous steps with project in projects/cloud-sdk-learning-journey/cloudsdk_bp_ts.

Log in to track your progress & complete quizzes