# SAP S/4HANA — Accounting API: connection

> Canonical URL: https://developers.apideck.com/apis/accounting/sap-s4hana-cloud/connection

# How to connect to SAP S/4HANA

The Apideck Accounting API connects to SAP S/4HANA through its standard OData APIs with **Basic Authentication**. How you set it up depends on your deployment:

- **SAP S/4HANA Cloud Public Edition**: follow the Public Edition setup below and enter your Fiori address as the **SAP System URL** in Vault.
- **SAP S/4HANA On-Premise or Private Cloud Edition**: follow the On-Premise and Private Cloud setup below and enter your gateway's address as the **SAP System URL** in Vault. Apideck calls your system from the internet, so the OData services must be reachable over HTTPS, through SAP Web Dispatcher or your own API gateway or middleware.

## SAP S/4HANA Cloud Public Edition

The Apideck Accounting API uses **Basic Authentication** with a dedicated SAP Communication User. Before you can connect, an SAP administrator must create a Communication Arrangement that grants the Communication User access to the OData services this connector reads from.

Before you start, you'll need: an SAP S/4HANA Cloud Public Edition tenant and administrator access to it (the `SAP_BR_ADMINISTRATOR` business role) so the arrangement and user can be created.

### Create a Communication Arrangement

1. Sign in to your SAP S/4HANA Cloud tenant as an administrator and open the **Communication Arrangements** app (Fiori launchpad → search "Communication Arrangements"). See SAP's [Maintaining Communication Arrangements](https://help.sap.com/docs/SAP_S4HANA_CLOUD/6aa39f1ac05441e5a23f484f31e477e7/7849aa40e01a4d7fac4bcaa9287657cd.html) for a walkthrough of the app.

2. Choose **New** and activate the Communication Scenarios that cover the resources you need. Only activate what you plan to use, but always include **SAP_COM_0008**: Apideck checks the connection by reading business partners, so without it the connection is marked invalid even when the other scenarios work.

   - **SAP_COM_0008** – Business Partner, Customer and Supplier Integration (customers, suppliers)
   - **SAP_COM_0057** – Supplier Invoice Integration (bills, bill credit notes)
   - **SAP_COM_0053** – Purchase Order Integration (purchase orders; when a bill or bill credit note is created against a purchase order, the order is also read, but only if a line gives a quantity without `unit_of_measure` or the bill names no `terms`)
   - **SAP_COM_0108** – Material Document Integration (goods receipts)
   - **SAP_COM_0009** – Product Integration (invoice items)
   - **SAP_COM_0164** – Material Stock Integration (stock `quantity` on invoice items; optional: without it `quantity` is `null`)
   - **SAP_COM_0120** – Billing Integration (item-level invoice lines for invoices created from billing documents; optional: without it invoice lines are G/L account lines)
   - **SAP_COM_0303** – Finance - Accounting Analytics Integration (reads for journal entries, invoices, payments, bill payments; also the `due_date` of bills as SAP keeps it on the accounting document: optional for bills, without it `due_date` is calculated from the bill's payment terms)
   - **SAP_COM_0002** – Finance - Posting Integration (required to _create_ journal entries, invoices, credit notes, payments or bill payments: all five post through this synchronous service). The scenario also provides **Journal Entry – Clearing (Asynchronous)**, which applies the `allocations` of payments and bill payments. Without it, a payment or bill payment with allocations returns `400` and nothing is posted.
   - **SAP_COM_0087** – SAP Analytics Cloud for Planning Integration (ledger accounts, subsidiaries/company codes, departments/cost centers, tracking categories/profit centers)

3. Bind a **Communication User** to the arrangement. If you do not already have one, create it from the **Maintain Communication Users** app ([SAP documentation](https://help.sap.com/docs/SAP_S4HANA_CLOUD/0f69f8fb28ac4bf48d2b57b9637e81fa/eef80dda3867461c92ac1273689ed36f.html)) and assign a strong password. Note the user's ID and password: you will paste these into Vault.

4. **Authentication method** on the arrangement must be **User ID and Password** (Basic Authentication). OAuth is rejected by several of the OData services used by this connector.

5. Save and activate the arrangement.

If a resource returns `403` with `You do not have start authorization for R3TR IWSV <service>`, the Communication User is not assigned to an arrangement that covers that resource. The credentials are correct; activate the matching scenario from the list above.

A missing optional scenario (**SAP_COM_0164**, **SAP_COM_0120**, or **SAP_COM_0303** when only bills are read) does not fail the request. When the affected field is requested, it comes back `null` (`quantity` on invoice items), the invoice lines stay G/L account lines, or the bill's `due_date` is calculated from its payment terms, and the response carries a warning that names the missing scenario.

### Find Your SAP System URL

Your SAP System URL is the address of your Fiori launchpad, for example `https://my123456.s4hana.cloud.sap`. You can paste it as it appears in your browser, including anything after it, or enter only the tenant ID (`my123456`). Apideck works out the API host (`my123456-api.s4hana.cloud.sap`) from either.

### Vault Connection Setup

Paste the values below into Vault:

- **SAP System URL** – Your Fiori launchpad address (e.g., `my123456.s4hana.cloud.sap`) or just the tenant ID (`my123456`).
- **SAP User** – The Communication User name bound to the Communication Arrangement (e.g., `APIDECK_UNIFY`).
- **SAP User Password** – The password generated for that Communication User.

The remaining fields are optional and only affect how customers and suppliers are created:

- **Default Business Partner Grouping** – The business partner grouping used for new customers and suppliers. Choose a grouping that assigns numbers automatically in your SAP system. Leave blank to use `BP01`.
- **Default Customer Reconciliation Account** – The G/L account used when a customer is created with a company code and the request does not name an account.
- **Default Supplier Reconciliation Account** – The same, for suppliers.

Click **Save**, then click **Authorize**.

Your SAP S/4HANA Cloud account is now connected.

## SAP S/4HANA On-Premise and Private Cloud Edition

Before you start, you'll need:

- SAP S/4HANA On-Premise or Private Cloud Edition. This guide was written against release **2023**; older releases may lack some of the services below.
- An SAP Basis administrator who can create users (`SU01`), maintain roles (`PFCG`) and activate OData services (`/IWFND/MAINT_SERVICE`, and `/IWFND/V4_ADMIN` for purchase orders)
- Whoever operates your HTTPS gateway or middleware
- Ideally a **non-production** system (QA or sandbox) for the first connection

### Step 1: Create a technical user

1. Log on to the client that holds your company data: the one you'll enter as **SAP Client** in Vault. Users and roles exist per client, so do Steps 1 and 3 in that client.
2. In transaction **SU01**, create a technical user of type **System**, for example `APIDECK_UNIFY`. System users can't log on to the SAP GUI, and the password-change requirement doesn't apply to them, so the connection won't break when a password policy runs. Don't use type _Communications_: its password can expire.
3. Set a strong password and note it. You'll enter it in Vault.

### Step 2: Activate the OData services

In transaction **/IWFND/MAINT_SERVICE**, choose **Add Service**, select your system alias (`LOCAL` for an embedded gateway), and add the services for the data you want to share. Only activate what you plan to use, but always include `API_BUSINESS_PARTNER`: Apideck checks the connection by reading business partners, so without it the connection is marked invalid even when the other services work.

| Service (technical name)                    | Used for                                                                   |
| ------------------------------------------- | -------------------------------------------------------------------------- |
| `API_BUSINESS_PARTNER`                      | Customers and suppliers (read, create, update)                             |
| `API_SUPPLIERINVOICE_PROCESS_SRV`           | Bills and bill credit notes (read, create, cancel)                         |
| `API_OPLACCTGDOCITEMCUBE_SRV`               | Journal entries, invoices, credit notes, payments and bill payments (read) |
| `API_GLACCOUNTINCHARTOFACCOUNTS_SRV`        | Ledger (G/L) accounts (read)                                               |
| `API_COMPANYCODE_SRV`                       | Subsidiaries (company codes) (read)                                        |
| `API_COSTCENTER_SRV`                        | Departments (cost centers) (read)                                          |
| `API_PROFITCENTER_SRV`                      | Tracking categories (profit centers) (read)                                |
| `API_PURCHASEORDER_2` (OData V4, see below) | Purchase orders (read)                                                     |
| `API_MATERIAL_DOCUMENT_SRV`                 | Goods receipts (read)                                                      |
| `API_BILLING_DOCUMENT_SRV`                  | Invoice line items for invoices posted from SD billing (read)              |
| `API_PRODUCT_SRV`, `API_MATERIAL_STOCK_SRV` | Invoice items (products) and their stock quantity (read)                   |

For an expense-management integration, activate at least **Business Partner, supplier invoices, G/L accounts, company codes and cost centers**.

**Purchase orders use an OData V4 service.** `API_PURCHASEORDER_2` does not appear in `/IWFND/MAINT_SERVICE`: publish it in transaction **/IWFND/V4_ADMIN** instead. Apideck calls it at `/sap/opu/odata4/sap/api_purchaseorder_2/srvd_a2x/sap/purchaseorder/0001/`. It is only available on releases that include this OData V4 service; SAP's note 3719250 describes the move to the OData V4 purchasing services for SAP S/4HANA 2023 and later. On a release without it, purchase orders can't be read through Apideck, while the other resources keep working. The older OData V2 purchase-order service is deprecated by SAP and Apideck no longer calls it.

> **Not yet available on On-Premise and Private Cloud:** creating journal entries, invoices, credit notes, payments and bill payments. On Public Edition these post through SAP's journal-entry SOAP service at a fixed path; On-Premise systems publish that service at a system-specific SOAMANAGER binding instead, so these creates are refused with a `501` on connections whose SAP System URL points at a gateway. Reading them works.

After activating, check in transaction **SICF** that the ICF nodes under `/sap/opu/odata/sap/` for these services are active, and the `/sap/opu/odata4/` node if you published the purchase-order service.

### Step 3: Grant the user access

In transaction **PFCG**, create a role for the technical user that contains:

1. **Start authorization for each OData service** activated in Step 2. On the role's **Menu** tab, add an **Authorization Default → TADIR Service** entry for each service. On an embedded gateway, add both object type **`IWSG`** (the service name with its version suffix, for example `API_BUSINESS_PARTNER_0001`) and **`IWSV`**. This generates the `S_SERVICE` authorization; don't maintain `S_SERVICE` by hand. The OData V4 purchase-order service is checked differently: SAP Gateway authorizes OData V4 per service group with the `S_START` authorization object, not `S_SERVICE`. For it, add an **Authorization Default → TADIR Service** entry with object type **`G4BA`** and, as its name, the service group you published in `/IWFND/V4_ADMIN`. If purchase orders return `403 Forbidden` while the other resources work, check this entry first.
2. **Business authorizations** for the data itself, limited to the company codes you want to share. For example: display for G/L accounts, company codes and cost centers; display, create and change for business partners; and display, create and reverse for supplier invoices. Objects such as `F_BKPF_BUK` (accounting documents per company code) and `M_RECH_BUK` (supplier invoices per company code) restrict access to the company codes you choose.

Assign the role to the user and generate the profile.

> If a list comes back empty while you know records exist, the user is usually missing a business authorization for that company code or authorization group. The technical user can't log on to SAP GUI, so it can't run **SU53** itself: have an administrator start an authorization trace in transaction **STAUTHTRACE**, filtered on the technical user, repeat the call, and read the failed check from the trace.

### Step 4: Expose the services over HTTPS

Make the OData services reachable from the internet at one HTTPS base URL, for example `https://sap-api.example.com`. Whatever sits in front of SAP (SAP Web Dispatcher, SAP API Management, Azure API Management, Apigee, Kong, or a reverse proxy) must:

- **Use a TLS certificate from a public certificate authority.** Self-signed or internal-CA certificates will be rejected.
- **Pass the standard SAP paths through.** Apideck calls paths such as `/sap/opu/odata/sap/API_BUSINESS_PARTNER/A_Supplier` and, for purchase orders, `/sap/opu/odata4/sap/api_purchaseorder_2/srvd_a2x/sap/purchaseorder/0001/PurchaseOrder`, so let both `/sap/opu/odata/` and `/sap/opu/odata4/` through. If your gateway publishes the services under an extra prefix (for example `https://gateway.example.com/sap-prod/sap/opu/odata/sap/...`), that's fine: enter the prefix as part of the SAP System URL in Vault.
- **Forward Basic Authentication** to SAP unchanged, or map it to the technical user.
- **Support SAP's CSRF protection for writes.** Before creating a record, Apideck fetches a token with the header `X-CSRF-Token: Fetch`. It then sends the token back on the write, together with all the cookies SAP returned (the token is bound to a cookie such as `SAP_SESSIONID_<SID>_<client>` or `sap-XSRF_<SID>_<client>`). The gateway must pass the `X-CSRF-Token` header and **all** `Set-Cookie`/`Cookie` headers through in both directions, without filtering by name. Otherwise every write fails with `403 CSRF token validation failed`.
- **Allow Apideck's IP addresses**, if you restrict inbound traffic. Apideck calls from these static IPs (EU Central 1):

  - `18.197.244.247`
  - `18.156.9.3`
  - `3.65.139.215`

  If you need a different region, contact us before connecting.

You don't need SAP Cloud Connector for this connection. It links your system to SAP Business Technology Platform, not to third parties such as Apideck.

Before moving on, test from outside your network:

```bash
curl -u 'APIDECK_UNIFY:<password>' \
  'https://sap-api.example.com/sap/opu/odata/sap/API_BUSINESS_PARTNER/A_Supplier?$top=1&$format=json'
```

A JSON response with one supplier means the connection will work.

### Vault Connection Setup

Paste the values below into Vault:

- **SAP System URL**: the address of your HTTPS gateway from Step 4, for example `sap-api.example.com`. If your gateway publishes the services under a path prefix, include it (for example `gateway.example.com/sap-prod`). Anything from the SAP service path on, such as `/sap/opu/odata/sap`, `/sap/opu/odata4/sap` or a launchpad path under `/sap/bc/`, is ignored. A prefix that is itself named `sap` (for example `gateway.example.com/sap`) is kept.
- **SAP Client** (optional): the three-digit SAP client (Mandant) that holds your company data, for example `100`. It is the client you enter on the SAP GUI logon screen, and transaction **SCC4** lists the clients on the system. Apideck sends it as the `sap-client` query parameter on unified API requests and as the `sap-client` header on Proxy API requests, unless the Proxy URL already carries `sap-client`. Leave it empty to use the system's default logon client. SAP users are defined per client, so the technical user from Step 1 must exist in the client you enter.
- **SAP User**: the technical user from Step 1 (for example `APIDECK_UNIFY`).
- **SAP User Password**: that user's password.

Click **Save**, then click **Authorize**.

Your SAP S/4HANA On-Premise or Private Cloud system is now connected.

## Notes

- **Company code is sent per request, not stored on the connection.** There is no company-code setting on this connection. The company code travels as the unified `company_id` field, which maps to SAP `CompanyCode` on each resource. It is required when creating journal entries, invoices, payments and bill payments: the integration sends it with every write.
- **Customers and suppliers are only set up in a company code when you ask for it.** A create request places the record in a company code when it carries `company_id` and a reconciliation account is available, either as `account.id` on the request or as the default account on this connection. Without both, the record is created in SAP but not in any company code, and it cannot be used on invoices, bills or payments until it is extended to one.
- **A bill line's `department_id` is posted as the SAP cost center.** SAP accepts a cost center only on a G/L account that is a cost element. On other accounts it rejects the bill with `Define a cost element category for account …`, so leave `department_id` empty on those lines. Some expense accounts need a cost center: a line on one of them without `department_id` is rejected with `Account … requires an account assignment relevant to cost accounting`.
- **Getting a single accounting document needs a composite ID.** SAP identifies these records by a compound key, so `GET` by id takes a colon-delimited value rather than a bare document number:

  - bills: `<SupplierInvoice>:<FiscalYear>` (e.g. `5105600102:2025`)
  - journal entries, invoices, payments, bill payments: `<CompanyCode>:<FiscalYear>:<AccountingDocument>` (e.g. `1010:2025:100000123`)

  The ids returned by the corresponding list calls are already in this form, so passing them straight through works.

- **Posted documents cannot be edited.** SAP treats posted accounting documents as immutable, so journal entries, invoices, credit notes, payments and bill payments support create only: there is no update. Correcting a mistaken post means reversing it, which creates a new offsetting document. For bills and bill credit notes, a delete request issues an SAP cancellation (a reversal) rather than removing the record; for the other five, do the reversal in SAP. The cancellation posts a reversal document of the opposite type, so cancelling a bill adds a record to the bill credit notes list, and cancelling a bill credit note adds one to the bills list. The cancelled document and its reversal both read as cancelled: `void` on bills, `voided` on bill credit notes.
- **Plan for password rotation.** On Public Edition, Communication User passwords expire according to your own tenant's password policy; SAP publishes no fixed lifetime. On-Premise technical users of type System are exempt from password expiry. Ask your SAP administrator what your policy is and update the credential in Vault before the password expires, otherwise calls start failing with `401`.

## Proxy writes and request timeouts

For OData writes through the Proxy API using this stored Basic Authentication connection, Apideck fetches a CSRF token and session cookies automatically. A CSRF rejection can trigger one retry with a fresh session. Token fetches and writes share a 28-second request budget within the 30-second gateway limit.

For sales orders, creating the header first and then posting items separately reduces the work in each request. Each call has its own budget. This sequence is not atomic: if an item fails, the header may already exist. The Communication User also needs the sales-order service arrangement (`SAP_COM_0109`).

A timeout does not confirm that SAP rejected or rolled back a write. Before repeating a create, check SAP using a unique reference supplied with the original request, such as `PurchaseOrderByCustomer` for a sales order. Reconcile the result before retrying to avoid duplicates. Large deep inserts that exceed the request budget need a workflow that fits the limit; increasing this timeout would exceed the gateway allowance.

## Troubleshooting

### Public Edition

- **`401 Unauthorized`** – The Communication User is not bound to the arrangement covering the service you called, the arrangement's authentication method is not **User ID and Password**, or the password has expired or been reset in SAP. Re-check the arrangement, then re-save the credentials in Vault.
- **Some resources fail while others work** – The Communication Scenario for the failing resource is not activated. Each scenario in the list above unlocks a different resource group; activate the missing one and re-try.
- **Every resource fails** – Check that the SAP System URL is your own tenant's address (`myNNNNNN.s4hana.cloud.sap`) and that the Communication User was created on that tenant.
- **An invoice created by SAP billing is missing** – Invoices come from the accounting document, so a billing document that SAP still shows as "to be posted" is not listed until it is posted to accounting. In SAP, open the billing document (Manage Billing Documents), resolve the posting issue it reports (typically a pricing or tax condition that could not be determined for the billing date) and post it; the invoice then appears with its item lines when **SAP_COM_0120** is active.
- **"id has invalid format"** – The `GET`-by-id value is not in the composite form described above.
- **A create is rejected for a missing company code** – Journal entry, invoice, payment and bill payment posts require `company_id`; there is no default to fall back on.
- **Creating a customer or supplier fails with `Grouping ... has external number assignment`** – The grouping used for new records expects a manually assigned number in your SAP system. Set **Default Business Partner Grouping** to a grouping that assigns numbers automatically.
- **A posting is rejected because the customer or supplier is not defined in the company code** – The record was created without a company code. Create customers and suppliers with `company_id` and a reconciliation account (`account.id`, or the default account on this connection), or extend the existing record to the company code in SAP.
- **A bill is rejected with `Baseline date for payment does not exist`** – The supplier has no payment terms in SAP, which is the case for suppliers created through this integration without `terms`. Send `due_date` on the bill, create suppliers with `terms` (an SAP payment terms key such as `0004`), or maintain payment terms on the supplier in SAP.

### On-Premise and Private Cloud

| Symptom                                      | Likely cause                                                                                                                     | Fix                                                                                                                      |
| -------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
| Connection times out                         | Gateway not reachable from the internet, or Apideck's IPs not allowed                                                            | Run the `curl` test from Step 4 from outside your network; check the firewall allow-list                                 |
| TLS or certificate error                     | Self-signed or internal-CA certificate                                                                                           | Install a certificate from a public CA on the gateway                                                                    |
| `401 Unauthorized`                           | Wrong username or password, the user doesn't exist in the SAP Client entered in Vault, or the gateway doesn't forward Basic Auth | Check the user in `SU01` and the **SAP Client** value in Vault; check the gateway's auth policy                          |
| Connected, but data is from the wrong client | SAP Client empty or set to a client other than the one holding your company data                                                 | Set **SAP Client** in Vault to the client that holds your company data                                                   |
| `403 Forbidden` on one resource              | Missing start authorization for that service: `S_SERVICE`, or `S_START` (object type `G4BA`) for purchase orders                 | Add the service to the role in `PFCG` and regenerate the profile                                                         |
| `404 Not Found` on one resource              | OData service not activated, or the SAP System URL is missing the gateway's path prefix                                          | Activate it in `/IWFND/MAINT_SERVICE` (purchase orders: publish in `/IWFND/V4_ADMIN`); check the SAP System URL in Vault |
| `403 CSRF token validation failed` on create | Gateway strips the `X-CSRF-Token` header or the session cookies                                                                  | Let both through in both directions                                                                                      |
| Empty list although records exist            | Missing business authorization for the company code                                                                              | Trace the user in `STAUTHTRACE`; extend the role                                                                         |

Still stuck? Contact [Apideck Support](https://help.apideck.com/en/articles/7774658).


[API reference](https://developers.apideck.com/apis/accounting/reference.md) · [Coverage matrix](https://developers.apideck.com/apis/accounting/coverage)
