Remote services and resilience — SAP Guided Tutorial
Free sample lesson · open to everyone
Consuming SAP S/4HANA and other OData APIs from CAP: importing the API definition, binding it through BTP destinations, mocking it locally, and designing timeouts, retries and caching so a slow backend does not take your app down.
In this lessonShow sectionsHide sections
Visual overview
The situation: a real project scenario
A supplier portal showed supplier names by calling S/4HANA once per row. When S/4HANA slowed during month-end, the portal timed out entirely. Batching the keys into one remote call, caching names for 15 minutes and showing IDs when the backend was unavailable kept the portal usable during the next close.
Explanation
Most real CAP applications eventually need data they don't own, typically from SAP S/4HANA or another OData API. The process starts by importing the remote service's metadata — downloading the EDMX from the SAP Business Accelerator Hub or the target system's $metadata endpoint, then running cds import <file>, which generates a CDS model of the remote API under srv/external.
That remote service is then configured in cds.requires, naming its kind (odata-v2 or odata), its generated model, and its credentials, which in production usually means pointing at a destination name rather than a hardcoded URL, for example "[production]": { "credentials": { "destination": "S4_BP" } }.
From there you expose only the fields you actually need through a projection, such as entity Suppliers as projection on bp.A_BusinessPartner { key BusinessPartner as ID, BusinessPartnerFullName as name }.
To actually serve data, you delegate: const bp = await cds.connect.to('API_BUSINESS_PARTNER') gives you a handle to the remote service, and a handler like this.on('READ', 'Suppliers', req => bp.run(req.query)) forwards the CQN query, which CAP translates into the equivalent OData request options for the remote system.
One important limitation is that joins between a local entity and a remote one don't happen in the database — the usual pattern is to fetch the relevant keys locally, then issue a single remote call for all of them, rather than calling the remote system once per row, which is the remote equivalent of the N+1 problem.
Locally, the remote service can be mocked with sample data so development doesn't depend on a live backend connection. In the cloud, the Destination service supplies the URL and authentication — OAuth2SAMLBearer, basic auth, or client credentials — and for on-premise systems, the Cloud Connector plus the Connectivity service bridge the network gap, using a destination of type OnPremise. Where the remote system's own authorization needs to reflect the actual end user rather than a generic technical user, principal propagation carries that user's identity through to the backend call.
Because your app's own availability is now coupled to a system you don't control, resilience has to be designed in rather than assumed: set explicit timeouts so a slow backend can't hang your request indefinitely, retry only idempotent reads with backoff (retrying a POST that creates a document risks creating duplicates), and cache or replicate slowly changing data so a temporary outage degrades gracefully instead of taking the whole feature down.
Hands-on steps: import API_BUSINESS_PARTNER and expose a Suppliers projection; run it locally against mocked data and inspect the OData request CAP generates in the logs; configure a real destination and run in a hybrid profile against a sandbox system; then simulate a slow backend and confirm your timeout and error handling actually kick in. The goal is that when the remote system is unavailable, your app returns a controlled error within your own timeout, and the rest of the app keeps working.
Code example
// srv/supplier-service.jsmodule.exports = cds.service.impl(async function () { const bp = await cds.connect.to('API_BUSINESS_PARTNER'); this.on('READ', 'Suppliers', req => bp.run(req.query));});Watch out
Common mistakes
- Hard-coding backend URLs and credentials instead of using destinations.
- Reading the remote API once per row of a local list (remote N+1).
- Retrying POST requests that create documents, causing duplicates.
- Using a technical user where the backend needs the business user's authorization.
Do this
Best practices
- Project remote entities down to the fields you use.
- Use destinations and principal propagation where backend authorization matters.
- Define timeout, retry and fallback behaviour explicitly for every remote dependency.
In the interview
Interview angle
Interviewers look for the full chain — cds import, cds.requires config, destinations and Cloud Connector, delegating CQN — and then for resilience thinking: timeouts, safe retries, caching or replication, and principal propagation when backend authorizations apply.
Practice this lesson
Original educational case studies for SAP consultants, not official SAP certification exam questions. Verify product edition and implementation details against the current SAP documentation.
Quick check
0 of 4 questions attempted
Question 1 of 4
What does `cds import API_BUSINESS_PARTNER.edmx` do?
Question 2 of 4
Where should a CAP app on Cloud Foundry get the URL and credentials of an on-premise S/4HANA system?
Question 3 of 4
Which retry policy is safe for remote calls?
Question 4 of 4
Users must see only S/4HANA data they are authorized for in S/4HANA itself. What connectivity approach is needed?
Practise this in an interview
Answer in your own words and get instant feedback, the way an interviewer would judge it.
1How do you consume an S/4HANA OData API in CAP and make it production-ready?
2When would you replicate S/4HANA data into your CAP app instead of reading it live?
Was this lesson helpful?
Keep exploring
Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.