ERPClimb logoERPClimb

Code examples · SAP BTP · Intermediate

SAP BTP CAP: consume an S/4HANA OData service (Business Partner) and expose it

The problem

A new BTP app must show business partners that live in S/4HANA.

How we solve it

  1. 1Import APIcds import
  2. 2Exposeprojection
  3. 3Forward readshandler
  4. 4Connectdestination

The result

A CAP service /partners that returns business partners, running against a local mock and, after configuration, against S/4HANA.

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

1. The requirement

A new BTP app must show business partners that live in S/4HANA. The app must not copy the data and must not talk to S/4HANA directly from the browser. It should read through its own service layer.

2. How we design it

CAP treats a remote API like a local service. We import the API description (EDMX), CAP generates a model for it, and we expose a read-only projection of the few fields we need. During development CAP mocks the remote service automatically. In production the same code calls S/4HANA through a BTP destination that holds the URL and credentials, so no secret is in the code.

Why we decided it this way

  1. 1Import the API descriptionYou work with typed entities and get compile-time help instead of hand-built URLs.
  2. 2Project only the needed fieldsSmaller payloads, fewer authorizations needed, and a stable interface for the UI.
  3. 3Read-only projectionThe app only displays partners. No write path means no accidental changes in S/4HANA.
  4. 4Destination for the connectionURL and credentials are configured in BTP, never in source code or Git.

Before you start

  • A CAP Node.js project (see the Support Desk example) and @sap/cds-dk installed.
  • The EDMX file of API_BUSINESS_PARTNER, downloaded from SAP Business Accelerator Hub (api.sap.com) under 'API Specification'.
  • For production use: a destination in your BTP subaccount named S4HANA that points to your S/4HANA system.

3. The code, step by step

Step 1 of 4: Import the API into the project

Convert the downloaded EDMX file into a CAP model.

Shell · Terminal
# cds import
cds import ./API_BUSINESS_PARTNER.edmx
Part by part (plain text)
  1. cds import. Copy the downloaded API_BUSINESS_PARTNER.edmx into the project folder and run the import. CAP creates srv/external/API_BUSINESS_PARTNER.csn (add --as cds for a .cds file) and a copy of the EDMX, and adds a cds.requires entry for the API in package.json.

Open in the portal

The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.

4. The final outcome

A CAP service /partners that returns business partners, running against a local mock and, after configuration, against S/4HANA.

What you should see

Locally with cds watch: GET /partners/BusinessPartners returns the partners from your mock CSV file. After deployment with a working destination: the same URL returns real partners from S/4HANA.

Try it and check the result

  • Create srv/external/data/API_BUSINESS_PARTNER-A_BusinessPartner.csv with the header BusinessPartner,BusinessPartnerFullName,BusinessPartnerCategory and a few rows. Run cds watch and open http://localhost:4004/partners/BusinessPartners?$top=3 : you get your mock rows. Without the CSV the list is empty, not an error.
  • Add $select=BusinessPartner and see that only that field comes back.
  • After deployment with a destination: call the same URL with a valid login and compare a partner with the S/4HANA app 'Manage Business Partner'.

Common mistakes

  • Credentials in package.json or in Git: never. Use the destination service or environment bindings.
  • Mock and real service can differ. Always test once against a real system before release.
  • The user's authorization in S/4HANA is not the user in BTP unless you propagate the identity; with a technical user everyone sees what that user sees. Decide this on purpose.
  • Fetching all fields of a partner is slow. Keep the projection small and page with $top.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-25. Release: SAP Cloud Application Programming Model with Node.js and the OData V2 API API_BUSINESS_PARTNER; check the current API version in SAP Business Accelerator Hub.

Original ERPClimb teaching code with illustrative names. Not taken from a customer system. Syntax can differ by release; check the references below and activate in a sandbox first. ERPClimb is independent of SAP SE.