Sub topic 10
Connecting BTP to SAP S/4HANA with destinations
This chapter assumes you know what a side-by-side extension is and have a CAP project that needs data from SAP S/4HANA. You should also be comfortable with OAuth 2.0 basics and JSON configuration. The next chapter covers user propagation in depth. Here the focus is on how an application finds and reaches S/4HANA.
Why destinations exist
A side-by-side application needs the URL, proxy route, and credentials of S/4HANA. Hard-coding these in the application has three problems:
- Secrets end up in source code or environment files.
- The same code cannot move between development, test, and production systems without a rebuild.
- Rotating a password means redeploying the application.
The Destination service on SAP BTP solves this. A destination is a named set of connection properties stored outside your code. The application asks for "S4HC_BP" at runtime, and the service returns the URL and the authentication data, such as an access token or an Authorization header.
Anatomy of a destination
A destination of type HTTP is a list of key-value properties. You can create it in the BTP cockpit under Connectivity > Destinations, or import it from a file like this one:
Name=S4HC_BP
Type=HTTP
Description=S/4HANA Cloud, Business Partner API
URL=https://my123456-api.s4hana.cloud.sap
ProxyType=Internet
Authentication=BasicAuthentication
User=BP_REVIEW_USERThe password is not in the file. You enter it in the cockpit after the import, so it never lands in version control.
The URL holds only the host. The service path, /sap/opu/odata/sap/API_BUSINESS_PARTNER, stays in the CAP configuration, as the credentials.path setting in the spine's package.json example shows. This lets one destination serve several APIs on the same system.
The main properties are:
| Property | Purpose |
|---|---|
| Name | The identifier your code uses to look the destination up |
| URL | Base URL of the target system, without the API path |
| ProxyType | How the connection is routed: Internet, OnPremise, or PrivateLink |
| Authentication | How the call is authenticated |
| sap-client | Optional client number for ABAP-based systems |
Choosing the proxy type
The proxy type depends on where S/4HANA runs.
| Target system | ProxyType | Extra component |
|---|---|---|
| SAP S/4HANA Cloud, public edition | Internet | None. The system has a public API endpoint. |
| S/4HANA in a private network, such as a hyperscaler VPC | PrivateLink | Private Link service setup |
| S/4HANA on-premise or private cloud, no public endpoint | OnPremise | Cloud Connector and the Connectivity service |
With OnPremise, your app does not call S/4HANA directly. It sends the request to the Connectivity service proxy. The proxy forwards it through a secure tunnel to the Cloud Connector, which runs inside the customer network. The Cloud Connector opens the tunnel outbound, so no inbound firewall port is needed. It also exposes only the URL paths an administrator has explicitly allowed. The destination's URL then holds a virtual host name, such as s4.internal:443, that the Cloud Connector maps to the real host.
Authentication types at a glance
The Authentication property decides what the destination service does before it hands data back to the app.
- BasicAuthentication sends a technical user's name and password. It is simple and suits a prototype or a system-to-system call that does not need the end user's identity.
- OAuth2ClientCredentials fetches a token for the application itself, not for the logged-in user. Use it for background jobs.
- OAuth2SAMLBearerAssertion exchanges the logged-in user's identity for an S/4HANA token. S/4HANA then applies that user's authorizations.
- PrincipalPropagation forwards the user's identity through the Cloud Connector to an on-premise system.
- NoAuthentication is for public endpoints only.
Where destinations live
A destination can exist at two levels:
- Subaccount level. It is visible to every application in the subaccount. This is the usual choice for a shared connection to S/4HANA.
- Service instance level. It belongs to one Destination service instance and is visible only to applications bound to that instance. Use it when the connection is private to one application.
The same name at both levels is an error source. The runtime looks for a service-instance destination first, then falls back to the subaccount. A stale copy at the instance level can therefore silently shadow the subaccount destination you just corrected.
Wiring a destination into CAP
The first spine example registers the API as a CAP remote service. The setting credentials.destination names the destination, and credentials.path is appended to its URL. For production, bind the app to the services it needs. In a Cloud Foundry multitarget application, the mta.yaml resources for this are:
resources:
- name: partner-review-destination
type: org.cloudfoundry.managed-service
parameters:
service: destination
service-plan: lite
- name: partner-review-connectivity
type: org.cloudfoundry.managed-service
parameters:
service: connectivity
service-plan: liteThe Connectivity service is needed only for OnPremise destinations. The lite plan of the Destination service is sufficient for lookups. You also need to list both resources under requires in the module that runs the CAP server. Without the binding, the app has no credentials to reach the Destination service and cannot resolve the name.
Using the remote service in code is unchanged from the spine example:
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()
}
}When bp.run executes, CAP resolves the destination through the SAP Cloud SDK, adds the authentication data, and sends the request. Your handler never sees a URL or a password.
Developing locally
On a laptop there is no Destination service binding. You have two choices:
- Use the hybrid mode of the CAP tools (
cds bindfollowed bycds watch --profile hybrid) to bind your local process to real BTP service instances. The destination is then resolved as in production. - Put direct credentials in a development-only profile and keep secrets out of Git, for example in a
.cdsrc-private.jsonfile that is in.gitignore.
{
"cds": {
"requires": {
"API_BUSINESS_PARTNER": {
"[development]": {
"credentials": {
"url": "https://my123456-api.s4hana.cloud.sap/sap/opu/odata/sap/API_BUSINESS_PARTNER",
"authentication": "BasicAuthentication",
"username": "BP_REVIEW_USER",
"password": "<set locally, never commit>"
}
}
}
}
}
}Hybrid mode is closer to production, so it catches problems with proxy type and authentication earlier.
Common errors
- "Destination not found" (404 from the Destination service). Check the exact name, which is case-sensitive. Also check that the app is bound to a Destination service instance in the right subaccount.
- 401 or 403 from S/4HANA. The destination was found, but the user lacks access. In S/4HANA Cloud, check that a communication arrangement exists for the communication scenario that includes the Business Partner API, such as SAPCOM0008, and that the communication user is linked to it.
- Timeouts with OnPremise. The Cloud Connector may be disconnected, or the virtual host and path are not enabled in its access control list.
- Certificate errors. Fix the trust chain. Do not set
TrustAll=trueto make the error disappear, because it disables server certificate validation. - Changes in the cockpit appear to be ignored. The runtime caches destination lookups for a short time. Wait a few minutes and check for a shadowing instance-level destination.
Exercise
A CAP app on Cloud Foundry must read business partners from an S/4HANA system in a customer data center that has no public endpoint. Name the ProxyType, the two BTP-side components involved in the connection besides the Destination service, and the property that decides which API path is called.
Show answer
Use ProxyType OnPremise. The connection goes through the Connectivity service and the Cloud Connector installed in the customer network. The API path is not part of the destination URL. It comes from the CAP setting credentials.path (for example /sap/opu/odata/sap/APIBUSINESSPARTNER), which is appended to the destination's URL.
Exercise
You corrected the password in the subaccount destination S4HC_BP, but the app still gets 401 errors. Give two plausible causes.
Show answer
A destination with the same name exists at the service instance level and shadows the subaccount one, so the old password is still used. Alternatively, the destination lookup is cached and the change has not been picked up yet. A third possibility is that the communication user in S/4HANA is locked or lacks a communication arrangement for the scenario.