Sub topic 12
Consuming a Released API with CAP
This chapter assumes you know OData and REST basics, can read simple Node.js code, and understand what a BTP destination is from the previous chapter. You will build a small side-by-side service with the SAP Cloud Application Programming Model (CAP). It reads business partners from SAP S/4HANA through a released API. No data is copied into the extension.
The working model
CAP treats an external API as a remote service. You import the API's metadata, register it under cds.requires, and CAP creates a client for it. Your own service projects the fields you need, and a handler forwards the read requests to S/4HANA.
Because you import only the released Business Partner API, the extension depends on a stable contract. An S/4HANA upgrade does not break it.
Step 1: Import the API
Download the EDMX file of the released API from the SAP Business Accelerator Hub. Then import it:
cds import ./API_BUSINESS_PARTNER.edmx --as cdsThis generates the model under srv/external/ and adds an entry to package.json. Check the result, because the generated entry needs the connection details below.
Step 2: Register the remote service
The kind is odata-v2 because this API is OData V2. The destination supplies the host and credentials, and path is appended to the destination URL.
{
"cds": {
"requires": {
"API_BUSINESS_PARTNER": {
"kind": "odata-v2",
"model": "srv/external/API_BUSINESS_PARTNER",
"credentials": {
"destination": "S4HC_BP",
"path": "/sap/opu/odata/sap/API_BUSINESS_PARTNER"
}
}
}
}
}Step 3: Project the fields you need
Expose only what the use case requires. A narrow projection limits what your extension depends on and what it reveals.
using { API_BUSINESS_PARTNER as bp } from './external/API_BUSINESS_PARTNER';
service PartnerReview {
@readonly
entity BusinessPartners as projection on bp.A_BusinessPartner {
key BusinessPartner,
BusinessPartnerFullName,
BusinessPartnerIsBlocked
};
}Step 4: Delegate reads to S/4HANA
Without a handler, CAP looks for the data in its own database, where the entity does not exist. The handler sends the query to the remote service instead.
const cds = require('@sap/cds')
module.exports = class PartnerReview extends cds.ApplicationService {
async init() {
const bp = await cds.connect.to('API_BUSINESS_PARTNER')
this.on('READ', 'BusinessPartners', req => bp.run(req.query))
return super.init()
}
}req.query is the query CAP built for your projection. bp.run translates it into an OData request, so the filters, $top, and field selection reach S/4HANA.
Common errors
| Symptom | Likely cause |
|---|---|
| `404` from the remote call | The `path` does not match the API's service path, or the destination URL already includes it. |
| `401` or `403` | The destination's authentication or the technical user's authorization for the API is wrong. |
| Database error such as a missing table | The `READ` handler is not registered, or the entity name in `this.on` differs from the service. |
| Destination not found | The destination is not in the subaccount or service instance your app is bound to. |
For local testing, add a development-profile credentials.url that points to a sandbox or mock. Keep real destination names for deployed environments.
Exercise
Your handler is missing and GET /BusinessPartners fails with a database error. Why does this happen, and how does the one-line handler fix it?
Show answer
CAP serves the projected entity from its own persistence by default. The entity belongs to the external API and has no local table. The READ handler calls bp.run(req.query), which sends the request to S/4HANA through the destination instead of the database.