Skip to main content

SAP ERP Connection

Connect flAPI to SAP ERP systems (ECC, S/4HANA) with ERPL, a DuckDB extension that talks to SAP over RFC. Read tables and CDS views with sap_read_table(), and call function modules with sap_rfc_invoke().

Prerequisites​

  • The ERPL extension, which brings the SAP NetWeaver RFC SDK libraries with it (see below).
  • An SAP user with RFC authorizations (S_RFC) and read access to the tables you serve (S_TABU_NAM / S_TABU_DIS).
  • Network access from the flAPI host to the SAP application server.

The extension​

ERPL is published from its own repository, http://get.erpl.io, not from DuckDB's community repository, and its binaries are unsigned. flAPI always starts DuckDB with allow_unsigned_extensions enabled, so it loads.

erpl is a small "trampoline" extension. On first LOAD it extracts the SAP NetWeaver RFC SDK libraries (libsapnwrfc.so and its ICU libraries) and its sub-extensions (erpl_rfc, and in the Enterprise Edition erpl_bics, erpl_odp) into the DuckDB extension directory, then loads them. That directory must be writable.

Two consequences for deployment:

  • The INSTALL downloads ERPL on first start, so the host needs to reach get.erpl.io.
  • flapi extensions install --config and flapi pack --with-extensions do not pick up an INSTALL from a URL repository like this one. On a host without network, put the extension into the extension directory yourself.

Quick start​

flapi.yaml
project-name: sap-inventory-api
template:
path: ./sqls
environment-whitelist:
- '^SAP_(ASHOST|SYSNR|CLIENT|USER|PASSWORD)$'
connections:
sap:
init: |
INSTALL 'erpl' FROM 'http://get.erpl.io';
LOAD 'erpl';
CREATE OR REPLACE SECRET sap_erp (TYPE sap_rfc,
ASHOST '{{env.SAP_ASHOST}}', SYSNR '{{env.SAP_SYSNR}}', CLIENT '{{env.SAP_CLIENT}}',
USER '{{env.SAP_USER}}', PASSWD '{{env.SAP_PASSWORD}}', LANG 'EN');
export SAP_ASHOST=sap.internal SAP_SYSNR=00 SAP_CLIENT=100
export SAP_USER=RFC_API_USER SAP_PASSWORD='...'
flapi -c flapi.yaml
  • init runs once, at startup. It is not a template: only {{env.NAME}} is substituted inside it, and only for names the whitelist matches. ${SAP_PASSWORD} stays literal text.
  • CREATE SECRET keeps the password in memory for this process. Do not write CREATE PERSISTENT SECRET: it stores the password unencrypted in ~/.duckdb/stored_secrets, where every DuckDB-based process of that user loads it.
  • ERPL functions use the sap_rfc secret; with several systems, pick one per call with sap_read_table('MARA', SECRET = 'sap_erp').
  • Rotating the password needs a restart.

Secret parameters​

ParameterDescriptionExample
ASHOSTApplication server hostsap.company.com
SYSNRSystem number00
CLIENTClient100
USERSAP user{{env.SAP_USER}}
PASSWDPassword{{env.SAP_PASSWORD}}
LANGLogon languageEN

Reading SAP tables​

sqls/stock.yaml
url-path: /stock
request:
- field-name: plant
field-in: query
required: false
validators:
- type: string
regex: '^[A-Z0-9]{1,4}$'
template-source: stock.sql
connection: [sap]
sqls/stock.sql
SELECT
MATNR AS material_number,
WERKS AS plant,
LGORT AS storage_location,
LABST AS stock_quantity
FROM sap_read_table('MARD')
WHERE 1=1
{{#params.plant}}
AND WERKS = '{{{ params.plant }}}'
{{/params.plant}}
ORDER BY MATNR

plant is inside a section, so it is pasted as text; the strict regex validator is what keeps it safe. sap_read_table pushes the column list and simple filters down to SAP. It also takes named parameters such as COLUMNS, FILTER, THREADS and MAX_ROWS; see the ERPL API reference.

No LIMIT: GET endpoints are paginated by default, and ?limit= / ?offset= are reserved for the pagination. A live, paginated endpoint reads from SAP once per page and once more for total_count, which is why SAP endpoints should be cached.

Joining SAP tables​

SELECT h.VBELN AS sales_order, h.ERDAT AS created_on, h.KUNNR AS customer,
c.NAME1 AS customer_name, i.MATNR AS material, i.KWMENG AS quantity
FROM sap_read_table('VBAK') h
LEFT JOIN sap_read_table('KNA1') c ON h.KUNNR = c.KUNNR
LEFT JOIN sap_read_table('VBAP') i ON h.VBELN = i.VBELN
WHERE h.ERDAT >= '20240101'

Each sap_read_table is a separate RFC extraction, so a join of large tables reads all of them. Put joins like this in a cache populate template, not in a live endpoint.

Common SAP tables​

AreaTables
Materials managementMARA (material master), MAKT (descriptions), MARC (plant data), MARD (storage location stock), MBEW (valuation)
Sales and distributionVBAK / VBAP (sales document header / items), KNA1 (customer master), KNVV (customer sales data)
FinanceBKPF (document header), BSEG (line items), SKA1 (G/L accounts)
PurchasingEKKO / EKPO (purchase order header / items), LFA1 (vendor master)

Cache SAP data​

RFC extractions take seconds to minutes and put load on the SAP application server. Serve SAP endpoints from a DuckLake cache, refreshed on a schedule:

sqls/inventory.yaml
url-path: /inventory
request:
- field-name: plant
field-in: query
required: false
validators:
- type: string
regex: '^[A-Z0-9]{1,4}$'
- field-name: material_type
field-in: query
required: false
validators:
- type: enum
allowedValues: [FERT, HALB, ROH, HAWA]
template-source: inventory.sql
connection: [sap]
auth:
enabled: true
type: bearer
jwt-secret: '{{env.INVENTORY_JWT_SECRET}}'
cache:
enabled: true
table: sap_inventory
schedule: 1h
template-file: inventory_cache.sql

INVENTORY_JWT_SECRET must also match the whitelist, for example '^INVENTORY_JWT_SECRET$'.

sqls/inventory_cache.sql
CREATE OR REPLACE TABLE {{cache.catalog}}.{{cache.schema}}.{{cache.table}} AS
SELECT
m.MATNR AS material,
t.MAKTX AS description,
m.MTART AS material_type,
s.WERKS AS plant,
s.LGORT AS storage_location,
s.LABST AS unrestricted_stock,
s.INSME AS quality_inspection_stock,
s.SPEME AS blocked_stock
FROM sap_read_table('MARA') m
JOIN sap_read_table('MAKT') t ON m.MATNR = t.MATNR AND t.SPRAS = 'E'
JOIN sap_read_table('MARD') s ON m.MATNR = s.MATNR
WHERE COALESCE(m.LVORM, '') = '' -- not flagged for deletion
sqls/inventory.sql
SELECT *
FROM {{cache.catalog}}.{{cache.schema}}.{{cache.table}}
WHERE 1=1
{{#params.plant}}
AND plant = '{{{ params.plant }}}'
{{/params.plant}}
{{#params.material_type}}
AND material_type = '{{{ params.material_type }}}'
{{/params.material_type}}
ORDER BY unrestricted_stock DESC

The populate template runs all its statements in one transaction: if SAP is unreachable during a refresh, nothing changes and the endpoint keeps serving the last snapshot. The project needs a ducklake: block; see Caching.

Security​

  • A dedicated RFC user with only the authorizations the served tables need. ERPL can call function modules (sap_rfc_invoke), and any template of the process can use the secret.
  • Credentials only through {{env.NAME}} and a temporary CREATE SECRET; never written into the YAML, never in a PERSISTENT secret.
  • Authentication on every endpoint. type is basic, bearer (alias jwt) or oidc; see Authentication.
  • One process per SAP trust boundary. Two systems whose data must stay apart get two flAPI processes.

Troubleshooting​

  • The server does not start, error at INSTALL or LOAD 'erpl'. The host cannot reach get.erpl.io, or the extension directory is not writable, so the trampoline cannot extract the RFC libraries. The log names the statement.
  • RFC_ERROR_COMMUNICATION. Check ASHOST and SYSNR, and that the firewall allows the gateway port (33<SYSNR>).
  • RFC_ERROR_AUTHORITY or no rows. The user lacks S_RFC or table authorizations; ask your SAP Basis team.
  • 500 with only a request_id. The SAP error is in the server log under that id; see Observability.
  • Slow refreshes. Read fewer columns, filter early, and refresh no more often than the data changes.

Next steps​

🍪 Cookie Settings