Consuming a remote API from the BO (SCM, SCM2, HTTP client)
A behavior pool can call a remote API with the usual ABAP Cloud code: the HTTP client (cl_web_http_client_manager) for a REST API, the OData client proxy of a service consumption model for an OData V2 or V4 service. The generated application runs this code as is; only the destination is configured differently from SAP (decision D31).
1. What to export with abapGit
| On SAP | In the abapGit repository | What Open RAP Runtime does with it |
|---|---|---|
| Service consumption model 2 (OData V4) | the generated consumption model class (INHERITING FROM /iwbep/cl_v4_abs_pm_model_prov) | keeps its types (tys_…, tyt_…) and constants (gcs_entity_set…); the define method is removed |
| Service consumption model (OData V2/V4, first generation) | the generated abstract entities and the service definition of the proxy | the abstract entities type the data; the service definition without a binding is not built (info SRVD007) |
| REST API | nothing |
The .srvc file of the service consumption model itself is not needed: the proxy reads the $metadata of the remote service on its first call.
2. The code of the behavior pool
The same as on SAP, for example in an action:
METHOD importRemote.
DATA remote_travels TYPE zscm_rt_travel=>tyt_travel_type.
DATA travel_ids TYPE RANGE OF zscm_rt_travel=>tys_travel_type-travel_id.
READ ENTITIES OF zrt_r_travel IN LOCAL MODE
ENTITY Travel FIELDS ( TravelID ) WITH CORRESPONDING #( keys )
RESULT DATA(travels).
travel_ids = VALUE #( FOR t IN travels ( sign = 'I' option = 'EQ' low = t-TravelID ) ).
TRY.
DATA(destination) = cl_http_destination_provider=>create_by_comm_arrangement(
comm_scenario = 'ZRT_TRAVEL_REMOTE'
service_id = 'ZRT_TRAVEL_REMOTE_REST' ).
DATA(proxy) = /iwbep/cl_cp_factory_remote=>create_v4_remote_proxy(
is_proxy_model_key = VALUE #( repository_id = 'DEFAULT'
proxy_model_id = 'ZSCM_RT_TRAVEL'
proxy_model_version = '0001' )
io_http_client = cl_web_http_client_manager=>create_by_http_destination( destination )
iv_relative_service_root = '/sap/opu/odata4/sap/zrt_ui_travel_o4/srvd/sap/zrt_ui_travel/0001/' ).
DATA(request) = proxy->create_resource_for_entity_set( zscm_rt_travel=>gcs_entity_set-travel )->create_request_for_read( ).
DATA(filter_factory) = request->create_filter_factory( ).
request->set_filter( filter_factory->create_by_range( iv_property_path = 'TRAVEL_ID' it_range = travel_ids ) ).
request->set_top( 50 ).
DATA(response) = request->execute( ).
response->get_business_data( IMPORTING et_business_data = remote_travels ).
CATCH /iwbep/cx_cp_remote INTO DATA(remote_error).
" remote_error->http_status_code, remote_error->get_text( )
CATCH /iwbep/cx_gateway cx_web_http_client_error cx_http_dest_provider_error INTO DATA(error).
" error->get_text( )
ENDTRY.
ENDMETHOD.
The internal names of the class (TRAVEL_ID, 'TRAVEL') are matched with the names of the service (TravelID, Travel) ignoring case and _. Supported: list read (create_by_range, and/or/not, set_select_properties, set_orderby, set_top, set_skip, request_count, request_no_business_data), read by key (navigate_with_key), create, update (PATCH or PUT, it_provided_property, set_if_match), delete. The CSRF token and the session cookies of SAP Gateway are handled by the proxy.
A REST API:
DATA(client) = cl_web_http_client_manager=>create_by_http_destination(
cl_http_destination_provider=>create_by_cloud_destination( i_name = 'RATINGS'
i_authn_mode = if_a4c_cp_service=>service_specific ) ).
DATA(request) = client->get_http_request( ).
request->set_uri_path( |/api/ratings/{ travel-TravelID }| ).
request->set_header_field( i_name = 'Accept' i_value = 'application/json' ).
DATA(response) = client->execute( if_web_http_client=>get ).
DATA(status) = response->get_status( ).
IF status-code = 200.
DATA(json) = response->get_text( ).
ENDIF.
3. The destination
On SAP, the URL and the credentials come from a communication arrangement or from the destination service of BTP. In the generated application, they are environment variables:
| Variable | Content |
|---|---|
DESTINATION_<NAME>_URL | base URL (mandatory) |
DESTINATION_<NAME>_USER, DESTINATION_<NAME>_PASSWORD | basic authentication |
DESTINATION_<NAME>_TOKEN | bearer token (Authorization: Bearer) |
DESTINATION_<NAME>_HEADERS | other headers, JSON object: {"APIKey":"…"} |
DESTINATION_<NAME>_TOKEN_URL | token endpoint of an OAuth 2.0 authorization server: the client credentials grant (D122) |
DESTINATION_<NAME>_CLIENT_ID, DESTINATION_<NAME>_CLIENT_SECRET | the OAuth client (the ID is mandatory with _TOKEN_URL) |
DESTINATION_<NAME>_CLIENT_AUTH | how the client authenticates: basic (HTTP basic, client_secret_basic, the default), post (in the body, client_secret_post), none (its ID only), mtls (its certificate, D124) or jwt (a JWT signed with its key, D124) |
DESTINATION_<NAME>_SCOPE | scopes of the token request, separated by spaces |
DESTINATION_<NAME>_TOKEN_PARAMS | further parameters of the token request, JSON object: {"audience":"…"}, {"resource":"…"} |
DESTINATION_<NAME>_REFRESH_TOKEN | a refresh token: the refresh token grant instead of client credentials (D123) |
DESTINATION_<NAME>_CLIENT_CERT, DESTINATION_<NAME>_CLIENT_KEY, DESTINATION_<NAME>_CLIENT_KEY_PASSPHRASE | X.509 client certificate and its private key (D124), PEM: the PEM itself or the path of a file |
DESTINATION_<NAME>_CA | certificate authorities trusted for the server of the destination, besides the ones of Node.js (PEM or file) |
DESTINATION_<NAME>_CLIENT_KEY_ID | kid of the client assertion of _CLIENT_AUTH=jwt |
DESTINATION_<NAME>_OAUTH1_CONSUMER_KEY, _OAUTH1_CONSUMER_SECRET, _OAUTH1_TOKEN, _OAUTH1_TOKEN_SECRET, _OAUTH1_SIGNATURE_METHOD, _OAUTH1_REALM | OAuth 1.0 (D124) |
<NAME> is the name of create_by_cloud_destination (RATINGS), or for create_by_comm_arrangement: <SCENARIO>_<SERVICE_ID>, otherwise <SCENARIO> (ZRT_TRAVEL_REMOTE_ZRT_TRAVEL_REMOTE_REST, then ZRT_TRAVEL_REMOTE), in upper case, the characters other than letters and digits replaced by _. rapt build lists the destinations written as literals in the .env.example and the README of the application. The credentials stay on the server: the ABAP only sees the URL.
OAuth 2.0 client credentials
With _TOKEN_URL, the application asks the authorization server for an access token (grant_type=client_credentials, RFC 6749 section 4.4) and sends it as Authorization: Bearer; it replaces the authorization of _USER/_PASSWORD and _TOKEN. This is the OAuth 2.0 Client Credentials authentication of an outbound communication user on SAP, and of the destinations of type OAuth2ClientCredentials of SAP BTP (for SAP BTP: the url of the XSUAA or IAS instance followed by /oauth/token, its clientid and clientsecret).
- The token is kept in the memory of the server, shared by the requests, until 30 seconds before the end of its lifetime (
expires_in; without it, until the API refuses it). - A
401answer to a kept token (revoked early) gets one more try with a new token. - A request of the ABAP code that sets its own
Authorizationheader is sent as is. - An error of the authorization server is the text of the exception (
cx_web_http_client_errorfor the HTTP client,/iwbep/cx_cp_remotefor the client proxy):the token request to https://… failed: 401 invalid_client: Bad credentials.
DESTINATION_ENERGY_URL=https://api.energy.example.com
DESTINATION_ENERGY_TOKEN_URL=https://auth.energy.example.com/oauth/token
DESTINATION_ENERGY_CLIENT_ID=my-client
DESTINATION_ENERGY_CLIENT_SECRET=...
DESTINATION_ENERGY_SCOPE=meter.read
OAuth 2.0 refresh token
Many APIs (energy, home automation, the consumer APIs of the cloud) give no client credentials: the owner of the account consents once (authorization code grant, in a browser), and the application gets a refresh token, from which it asks for access tokens. With _REFRESH_TOKEN, the application uses the refresh token grant (grant_type=refresh_token, RFC 6749 section 6) with the same _TOKEN_URL, _CLIENT_ID, _CLIENT_SECRET (none for a public client: its ID is sent in the body), _CLIENT_AUTH, _SCOPE and _TOKEN_PARAMS:
DESTINATION_ENERGY_URL=https://api.energy.example.com
DESTINATION_ENERGY_TOKEN_URL=https://auth.energy.example.com/oauth/token
DESTINATION_ENERGY_CLIENT_ID=my-client
DESTINATION_ENERGY_CLIENT_SECRET=...
DESTINATION_ENERGY_REFRESH_TOKEN=... # from the consent of the owner of the account
- Rotation: many providers give a new refresh token with each access token and invalidate the previous one. The application uses the new one at once and keeps it in its database (table
rapt_oauth_refresh_token, created at the first use), so that it survives a restart and is shared by the instances of the application. It is written when the LUW that asked for it ends, committed or rolled back: the provider has invalidated the previous one in any case. - A new consent: a new
_REFRESH_TOKENin the environment replaces the one kept (the row is kept per destination and SHA-256 of the refresh token of the environment). - Several instances: when the provider refuses the refresh token (
invalid_grant), the application reads the database once more, in case another instance replaced it. - Revoked: the text of the exception says to set a new refresh token (
… 400 invalid_grant; the refresh token is no longer valid, set a new one in DESTINATION_ENERGY_REFRESH_TOKEN). - The refresh tokens are kept encrypted in the database (AES-256-GCM, with a key derived from
_REFRESH_TOKEN): the database or a backup alone does not give them. A value that cannot be decrypted is ignored with a warning, and the refresh token of the environment is used.
The other authentication methods of ABAP Cloud (D124)
The users of outbound communication of a communication system of ABAP Cloud authenticate with a user and a password, an X.509 client certificate, OAuth 1.0 or OAuth 2.0, whose client authenticates with a secret (Basic), mTLS, a JWT or nothing (None). All of them are destinations of the environment:
| ABAP Cloud | Variables |
|---|---|
| User name and password | _USER, _PASSWORD |
| SSL client certificate | _CLIENT_CERT, _CLIENT_KEY (_CLIENT_KEY_PASSPHRASE) |
| OAuth 2.0, client authentication Basic | _TOKEN_URL, _CLIENT_ID, _CLIENT_SECRET (_CLIENT_AUTH=basic or post) |
| OAuth 2.0, client authentication mTLS | _TOKEN_URL (the mTLS endpoint: certurl + /oauth/token for XSUAA), _CLIENT_ID, _CLIENT_AUTH=mtls, _CLIENT_CERT, _CLIENT_KEY |
| OAuth 2.0, client authentication JWT | _TOKEN_URL, _CLIENT_ID, _CLIENT_AUTH=jwt, _CLIENT_KEY, _CLIENT_CERT (the certificate registered at the authorization server) or _CLIENT_KEY_ID |
| OAuth 2.0, client authentication None | _TOKEN_URL, _CLIENT_ID, _CLIENT_AUTH=none |
| OAuth 1.0 | _OAUTH1_CONSUMER_KEY, _OAUTH1_CONSUMER_SECRET, _OAUTH1_TOKEN, _OAUTH1_TOKEN_SECRET (_OAUTH1_SIGNATURE_METHOD, _OAUTH1_REALM) |
- Certificates: PEM, given as the PEM itself (a
\nwritten as two characters is a new line, for the environment variables that hold one line) or as the path of a file (a volume of the container, a file of the platform). From a PKCS#12 file of SAP:openssl pkcs12 -in client.p12 -out client.pem -nodes(certificate and key in one file, given to both variables). A server signed by an authority of its own:_CA. - Client certificate: shown to the API, unless
_CLIENT_AUTHismtlsorjwt: then it authenticates the OAuth client at the token endpoint only, and the API gets the access token. - JWT client authentication (
private_key_jwt, RFC 7523): a JWT signed with the key (RSA: RS256, EC: ES256 to ES512, Ed25519: EdDSA), issuer and subject the client ID, audience the token endpoint, valid 5 minutes, with the thumbprints of the certificate in its header (x5t, the SHA-1 Microsoft Entra ID reads, andx5t#S256) orkid. - OAuth 1.0 (RFC 5849): each request is signed (HMAC-SHA1 by default, or HMAC-SHA256), the query and a form body in the signature;
_OAUTH1_REALMfor the APIs that want it (NetSuite). It excludes OAuth 2.0 (_TOKEN_URL). - A destination with a certificate or
_CAis called withnode:https: redirects are followed on the same origin only, and the outgoing proxy (HTTPS_PROXY) is not used.
Authorization code
The authorization code grant (with PKCE) needs a user in a browser: it has no place in a call of the ABAP code. It gives the first refresh token: do the consent once with the tools of the provider (its developer portal, an OAuth client such as oauth2c or Postman) and set the refresh token it gives in _REFRESH_TOKEN. Not supported: the password grant (deprecated by the OAuth 2.0 Security BCP), user propagation (SAML bearer, token exchange, a token per user).
With docker compose, in the app service:
environment:
DESTINATION_ZRT_TRAVEL_REMOTE_URL: https://my-system.example.com
DESTINATION_ZRT_TRAVEL_REMOTE_USER: ${REMOTE_USER}
DESTINATION_ZRT_TRAVEL_REMOTE_PASSWORD: ${REMOTE_PASSWORD}
Behind an outgoing proxy: HTTPS_PROXY=… and NODE_USE_ENV_PROXY=1.
4. Limits
See KNOWN_GAPS.md: no $batch, $expand, deep insert, navigation by association nor actions of the remote service in the client proxy; value controls not filled; no user propagation, no OAuth grant other than client credentials and refresh token; no communication targets, SOAP nor RFC. As on SAP, a remote call made during the interaction is not undone by a rollback of the LUW.
Complete tested example: packages/runtime/test/variants.ts (remoteVariant) and packages/runtime/test/rap-variant-remote.test.ts.