Jerry’s previous article SAP S/4HANA Cloud SDK Introduction describes how to use the SAP S/4HANA Cloud SDK to consume the OData service of the S/4HANA system in a third-party application.

In my code at the time, the URL to the SAP API Business Hub Sandbox was hardcoded and not flexible enough. This article describes how to configure these endpoints as environment variables.

The website links

Create an.env file in the project root directory with the following content:

destinations=[{"name": "sandbox", 
                "url": "https://sandbox.api.sap.com/s4hanacloud"}]

Run the following command:

npm install @nestjs/config

To load the environment variables defined in the.env file, we need to add the ConfigModule provided by the config package to the @module definition of the application. Open app.module.ts and update it with the following code:

The source code:

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { BusinessPartnerController } from './business-partner.controller';

@Module({
  imports: [ConfigModule.forRoot()],
  controllers: [AppController, BusinessPartnerController],
  providers: [AppService],
})
export class AppModule {}

Change the parameter of the execute method in the application code from the hard-coded URL to pass a Destination name maintained in the.env file.