Skip to main content

SAP BW / BW/4HANA Connection

Connect flAPI to SAP BW and BW/4HANA with ERPL, the same DuckDB extension the SAP ERP guide uses. The connection and its secret are identical; what differs is which tables you read and how much you must cache.

Prerequisites​

  • The ERPL extension from http://get.erpl.io. It is unsigned (flAPI always enables allow_unsigned_extensions) and brings the SAP NetWeaver RFC SDK libraries with it. Details in SAP ERP → The extension.
  • A BW user with RFC authorizations (S_RFC) and read access to the tables you serve.
  • Network access from the flAPI host to the BW application server.

Quick start​

flapi.yaml
project-name: sap-bw-reporting-api
template:
path: ./sqls
environment-whitelist:
- '^SAP_BW_(HOST|SYSNR|CLIENT|USER|PASSWORD)$'
connections:
sap-bw:
init: |
INSTALL 'erpl' FROM 'http://get.erpl.io';
LOAD 'erpl';
CREATE OR REPLACE SECRET sap_bw (TYPE sap_rfc,
ASHOST '{{env.SAP_BW_HOST}}', SYSNR '{{env.SAP_BW_SYSNR}}', CLIENT '{{env.SAP_BW_CLIENT}}',
USER '{{env.SAP_BW_USER}}', PASSWD '{{env.SAP_BW_PASSWORD}}', LANG 'EN');
  • init runs once, at startup, and is not a template: only whitelisted {{env.NAME}} references are substituted. ${VAR} is not supported.
  • CREATE SECRET keeps the password in memory. Do not use CREATE PERSISTENT SECRET: it writes the password unencrypted to ~/.duckdb/stored_secrets.

The secret parameters (ASHOST, SYSNR, CLIENT, USER, PASSWD, LANG) are the same as for SAP ERP.

Which BW tables to read​

sap_read_table() reads any transparent table the user may see. In BW that means the tables behind the modelling objects:

ObjectTables (naming convention)
Advanced DSO (BW/4HANA)active data in /BIC/A<name>2
Classic DSOactive data in /BIC/A<name>00
InfoCubefact tables /BIC/F<name> and /BIC/E<name> (keys point to dimension tables)
InfoObject master dataattributes /BIC/P<name>, texts /BIC/T<name>
SAP-delivered objectsthe same patterns with /BI0/ instead of /BIC/

Check the exact names in transaction RSA1 or SE11 before you write a template; sap_show_tables() and sap_describe_fields('<table>') list them from SQL.

ERPL's Enterprise Edition also ships erpl_bics, which runs BEx queries (sap_bics_show_queries(), sap_bics_begin(...), sap_bics_result(...)). It is a stateful, multi-step API and has not been tested inside flAPI templates; read the ERPL documentation before you use it.

A live endpoint​

sqls/sales.yaml
url-path: /sales
request:
- field-name: year
field-in: query
required: true
validators:
- type: string
regex: '^[0-9]{4}$'
- field-name: region
field-in: query
required: false
validators:
- type: enum
allowedValues: [EMEA, AMER, APAC]
template-source: sales.sql
connection: [sap-bw]
sqls/sales.sql
SELECT CALYEAR AS year, REGION AS region, PRODUCT AS product,
REVENUE AS revenue, QUANTITY AS quantity
FROM sap_read_table('/BIC/ASALES2')
WHERE CALYEAR = '{{{ params.year }}}'
{{#params.region}}
AND REGION = '{{{ params.region }}}'
{{/params.region}}
ORDER BY revenue DESC

year is outside any section, so it is bound as a parameter; region is inside one, so it is text behind an enum validator. No LIMIT: the endpoint is paginated, and ?limit= / ?offset= belong to the pagination. The column names here are placeholders for your own ADSO's fields.

Cache BW data​

BW extractions are slow, and a paginated live endpoint reads twice per full page (the page and its COUNT(*)). Serve BW endpoints from a DuckLake cache, refreshed after your BW loads finish:

sqls/revenue.yaml
url-path: /reports/revenue
request:
- field-name: year
field-in: query
required: false
validators:
- type: string
regex: '^[0-9]{4}$'
- field-name: region
field-in: query
required: false
validators:
- type: enum
allowedValues: [EMEA, AMER, APAC]
template-source: revenue.sql
connection: [sap-bw]
auth:
enabled: true
type: bearer
jwt-secret: '{{env.REPORTING_JWT_SECRET}}' # whitelist '^REPORTING_JWT_SECRET$' too
cache:
enabled: true
table: bw_revenue
schedule: 24h
template-file: revenue_cache.sql
sqls/revenue_cache.sql
CREATE OR REPLACE TABLE {{cache.catalog}}.{{cache.schema}}.{{cache.table}} AS
SELECT
strptime(f.CALMONTH || '01', '%Y%m%d') AS month_date,
substr(f.CALMONTH, 1, 4) AS year,
r.TXTMD AS region,
SUM(f.NET_REVENUE) AS net_revenue,
SUM(f.QUANTITY) AS quantity_sold
FROM sap_read_table('/BIC/AREVENUE2') f
LEFT JOIN sap_read_table('/BIC/TREGION') r
ON f.REGION = r.REGION AND r.LANGU = 'E'
WHERE substr(f.CALMONTH, 1, 4) >= '2020'
GROUP BY ALL
sqls/revenue.sql
SELECT *
FROM {{cache.catalog}}.{{cache.schema}}.{{cache.table}}
WHERE ({{ params.year }} IS NULL OR year = {{ params.year }})
{{#params.region}}
AND region = '{{{ params.region }}}'
{{/params.region}}
ORDER BY month_date DESC, net_revenue DESC

year is optional but stays bound: it is outside the section, and an absent value binds NULL, which switches the filter off. The populate template runs in one transaction, so a failed BW extraction changes nothing and the last snapshot keeps being served. The project needs a ducklake: block; see Caching.

BW-specific considerations​

  • Dates are strings. CALMONTH is YYYYMM, CALDAY is YYYYMMDD. Convert in the populate template: strptime(CALDAY, '%Y%m%d').
  • Texts are language-dependent. Join text tables on LANGU.
  • Technical names leak. Rename columns in the populate template so the API does not expose /BIC/ field names.
  • Refresh after the load chain. A schedule that runs before the nightly process chain finishes caches yesterday's data for another day. A pipeline can trigger the refresh instead: flapii cache refresh <path> against a server started with --config-service (both sides read the token from FLAPI_CONFIG_SERVICE_TOKEN), or flapi cache refresh -c flapi.yaml --endpoint <path>, which refreshes once and exits without a server.

Best practices​

  • Cache every BW endpoint.
  • Pre-aggregate in the populate template to the grain the API needs.
  • Filter by date range in every populate template.
  • Read active-data tables, not inbound or change-log tables.
  • Use a dedicated RFC user with read access to the served tables only.

Troubleshooting​

  • No rows. Wrong table name (check RSA1/SE11), the ADSO is not activated, or the user lacks table authorizations.
  • RFC_ERROR_AUTHORITY. The user needs S_RFC and table authorizations; for erpl_bics, also BW analysis authorizations (S_RS_COMP).
  • Slow refreshes. Read fewer columns, filter early, and read from aggregated objects where you can.
  • 500 with only a request_id. The RFC error is in the server log under that id; see Observability.

Next steps​

🍪 Cookie Settings