SAP Business One Service Layer: A Developer's Integration Guide
Back to blog
full stack·September 3, 2026·10 min read·By Yehonatan Saadia

SAP Business One Service Layer: A Developer's Integration Guide

How to actually integrate with the SAP Business One Service Layer - the Login handshake, the B1SESSION and ROUTEID cookies, the 30-minute timeout and error -5002, OData v4 vs the deprecated v3, and the batch pattern that keeps documents consistent.

Key takeaways

  • Authentication is a session, not a token. POST to /Login with username, password and CompanyDB, then carry the B1SESSION cookie - and the ROUTEID cookie too, or a load-balanced instance will reject you.
  • The session expires after 30 minutes of inactivity and returns error code -5002. Any integration that runs longer than a coffee break must catch that code and re-login automatically.
  • Create documents in a single call with their lines nested. A header created first and lines added after leaves a half-written document in the ERP when anything fails midway.
  • OData v3 is deprecated - v4 is the primary protocol from Feature Pack 2405. Code written against v3 examples found online will need reworking on any current installation.

SAP Business One exposes the Service Layer - an OData-based REST layer that is the supported way to read and write into the system from outside. It works well, but its login model is unlike any modern API, and that is where most first integrations break.

If you are choosing between systems, see also Priority vs SAP Business One.

Authentication: a session, not a token

There is no API key and no Bearer token. You log in:

POST /b1s/v1/Login
{
  "UserName": "...",
  "Password": "...",
  "CompanyDB": "..."
}

On success the Service Layer returns a B1Session entity carrying a valid session id in the response body, and sends the same id in a Set-Cookie header. You carry that cookie on every subsequent call.

Alongside B1SESSION, a ROUTEID cookie is also returned. When the Service Layer sits behind a load balancer - which it does in most serious installations - that cookie is what routes you back to the instance where your session was created.

If your HTTP client stores only B1SESSION and ignores ROUTEID, the result is intermittent authentication failures: some calls succeed and some are rejected, with no obvious pattern. Use a cookie jar that carries both rather than copying a single header by hand.

Expiry: 30 minutes and error -5002

The default is expiry after 30 minutes of inactivity. When the session lapses, the call fails with code -5002.

The practical consequence: any process running longer than half an hour must handle this. A nightly import, a scheduled sync, a queue worker - all will die midway unless something catches -5002, re-authenticates, and retries the failed call. This is normal behaviour, not an edge case.

The correct pattern is to wrap every call: on -5002, log in once and retry. If that also fails, stop rather than loop.

One operational point that is easy to miss: each user has a limit on concurrent open sessions. Code that logs in on every call, instead of holding one session, will exhaust that quota and start failing - and it will look like an entirely different problem.

OData v4, not v3

As of Feature Pack 2405, OData v3 is deprecated and v4 is the primary protocol.

This matters for a practical reason: a large share of the examples and blog posts you find by searching were written in the v3 era. Differences in query syntax, metadata and response format will make that code fail on a current installation in confusing ways. Check which version the installation offers before adopting an example from the web.

Creating documents - in one call

The most important rule when writing to an ERP: a document is created in a single request, with its lines nested in the body. Not a header followed by lines in separate calls.

The reason is integrity, not efficiency. If you created an order header and the second call adding lines failed, the system is left holding an empty order with a sequential number that somebody has to clean up by hand. On tax documents it is worse.

The same logic applies to cancellation: an issued document is not deleted. The correct route is an offsetting document or a built-in cancellation depending on the document type - and that decision belongs with the business's accountant, not the developer.

Permissions and what they hide

The Service Layer user's permissions apply to every call. If the user cannot see an object in the interface, they will not see it through the API either.

The trap: this usually manifests as missing records rather than a permission error. A query returns 200 with fewer results than you expected, and your code carries on as normal. Cross-check a count against the interface the first time rather than assuming the response is complete.

Decisions before you start

  • A test environment. As with any ERP, a document issued in production by mistake is an accounting problem. Confirm there is a separate CompanyDB for development, and keep the CompanyDB name in an environment variable rather than in code.
  • Custom fields. SAP B1 installations accumulate user-defined fields and custom objects. Two clients on the same version can require different code. Read the metadata of that specific installation instead of relying on generic documentation.
  • Hebrew and RTL. Hebrew text fields pass through the API normally, but rendering them in PDFs, emails and exports is a separate story. See Hebrew and RTL in business software.
  • Who owns the logic. If your system decides which document type to issue and when, that is an accounting decision requiring sign-off, not a technical choice.

A debugging order

  1. Confirm /Login returns 200 and that the cookie jar captured both B1SESSION and ROUTEID.
  2. Intermittent auth failures - almost always a missing ROUTEID.
  3. Failure after a pause - check whether the code is -5002, meaning an expired session.
  4. Missing records - check the API user's permissions against the interface.
  5. An example from the web that does not work - check whether it was written for OData v3.
#SAP Business One#Service Layer#OData#API integration#ERP

Frequently asked questions

How do you authenticate with the SAP Business One Service Layer?

By POSTing UserName, Password and CompanyDB to the /Login endpoint. The Service Layer returns a B1Session entity with a session id and sets a B1SESSION cookie, which you carry on every subsequent request. There is no API key or Bearer token. OAuth 2.0 with OpenID Connect is available from version 10.0 FP 2305 but requires an identity provider.

Why do SAP B1 Service Layer calls fail intermittently?

Usually because the ROUTEID cookie is being dropped. The Login response sets both B1SESSION and ROUTEID, and when the Service Layer runs behind a load balancer, ROUTEID is what routes you back to the instance holding your session. A client that stores only B1SESSION will succeed on some calls and be rejected on others with no clear pattern. Use a cookie jar carrying both.

What is SAP Business One error -5002?

It means the Service Layer session has expired. The default timeout is 30 minutes of inactivity, so any long-running import, scheduled sync or queue worker will hit it. The correct handling is to catch -5002, re-login once, and retry the failed call - while still holding a single session rather than logging in on every call, since each user has a concurrent session limit.

Does SAP Business One use OData v3 or v4?

OData v4 is the primary protocol as of Feature Pack 2405, and v3 is deprecated. This matters because many examples and blog posts online were written for v3, and differences in query syntax, metadata and response shape will make that code fail confusingly on a current installation. Check what the installation offers before copying an example.

Should an order header and its lines be created in separate API calls?

No. Create the document in one request with the lines nested in the body. If the header succeeds and a follow-up call adding lines fails, the ERP is left holding an empty document with a sequential number that a person must clean up manually - and on tax documents that is considerably worse than an inconvenience.

Keep reading

Related service

Integrations

Make the systems you already pay for talk to each other.

Learn more

About the author

Yehonatan Saadia

Freelance automation, web & MVP engineer

I'm Yehonatan Saadia, a senior engineer who builds business automation, custom websites, and MVPs for small and mid-sized companies across the US, Europe, and Israel. These guides come from real client work, not theory.

Work with me

Have a project like this?

Tell me what you're trying to automate or build and I'll tell you the fastest reliable way to ship it.