Browse documentation

On this page

Multi-Tenant Accounting for SaaS: A Separate Ledger per Customer

How do you embed accounting in a SaaS and keep each customer's books separate? Give every customer business its own Paprel company. A company is a complete, separate set of books: its own chart of accounts, invoices, payments, journals and reports. Your SaaS adds a company when a customer signs up, stores the company id next to your customer record, and gets a token that works in that one company whenever it posts or reads. One organization pays for all of them, at a price per company.

This page has a script that runs the whole pattern in the sandbox with one command, for two example customers:

For each customerAPI call
Add a companyPOST /v1/company/organization-companies
Get a token for that company onlyApp Connect token with member_company_id
Create a client, an item and an invoicePOST /v1/clients, POST /v1/items, POST /v1/invoices
Post the invoice to the ledgerPUT /v1/invoices/{id}/state-transition
Record its paymentPOST /v1/invoices/{id}/payment
Read the booksGET /v1/reports/trial-balance, GET /v1/reports/income-statement

Then it checks the separation: each customer's token asks for the other customer's invoice and must not get it, and neither trial balance may show the other company's revenue account.

Using an AI coding agent? Skip to the prompt for it.

Sandbox only. A run creates two real sandbox companies and their example records. It does not change your plan or company count, charge a card, send invoice email or move money. Nothing is rolled back automatically.

Validation: run end to end against sandbox.paprel.com on 19 November 2026, in a newly signed-up organization. The output below is from that run. The script is also tested against the published request schemas, permissions and mocked responses.

1. Prepare credentials

You need Node.js 20 or newer and a company in sandbox.paprel.com. Signing up is free, and the company you sign up with is your organization: Settings → Organization lists it with an Organization badge. A company that shows "No organization yet" there can't add companies; sign up again for a new one. Create an App Connect client in it and grant these permissions:

PermissionUsed for
organization:company-listChecking the plan has room for two more companies
company:addAdding a company per customer
organization:company-tokenGetting a token for each customer's company
accounting:account-list, accounting:account-addReading each chart of accounts and adding a revenue account
client:add, item:addThe customer's client and the service it sells
invoice:add, invoice:edit, invoice:viewCreating, posting and reading the invoice
invoice:paymentRecording the payment
accounting:journal-listReading the invoice's journal
report:trial-balance, report:income-statementReading each company's reports

Every token issued for a customer's company carries these permissions and no more. A customer company's token cannot add companies or get tokens for other companies. Don't grant an admin role to make the example pass.

Shell
export APP_CONNECT_CLIENT_ID="<your sandbox client ID>"
export APP_CONNECT_CLIENT_SECRET="<your sandbox client secret>"
export PARTNER_DOMAIN="sandbox.paprel.com"

2. Download and check

Download the SaaS ledger example, or from the terminal:

Shell
curl --fail --silent --show-error https://paprel.com/examples/saas-ledger.mjs --output saas-ledger.mjs

Read the file before running it. It has no npm dependencies, talks only to the sandbox API, refuses redirects, and never prints a token or the secret.

Shell
node saas-ledger.mjs --discover

This step only reads. It confirms the credentials work and that the plan has room for two companies. During the trial there is no limit on the number of companies.

3. Run it

Shell
export CONTACT_EMAIL="<an email address you control>"
node saas-ledger.mjs --run

CONTACT_EMAIL becomes the contact address of both new companies, so use a mailbox you control. COMPANY_CURRENCY (default USD), COMPANY_TIMEZONE (default UTC) and ACCOUNTING_DATE (default today) are optional.

The script picks what each new company posts to by fixed rules, and prints every choice: the first top-level cash account (cashEquivalents; in a new company, primaryOperatingAccount), the main sales account as the parent of a new revenue account (salesRevenue, never a contra account such as discounts or returns), and a bank-transfer payment mode where one exists. To choose yourself, set DEPOSIT_ACCOUNT_ID, REVENUE_PARENT_ACCOUNT_ID or PAYMENT_MODE_ID.

To post the invoice, it uses the company's invoice workflow if one is configured. A company added moments earlier usually has none yet, so the script issues the invoice (status sent), which posts it. Either way it then checks that the invoice has a journal before it records the payment.

The output ends with one row per customer, the accounts it chose and the isolation check. This is the sandbox run of 19 November 2026, with company ids left out:

JSON
{
  "currency": "USD",
  "customers": [
    { "company": "Harbor Lettings",  "invoiced": "1200.00", "payment_recorded": "1200.00", "payment_approved": true, "net_income": "1200.000000", "revenue_on_own_trial_balance": true },
    { "company": "Northwind Studio", "invoiced": "480.00",  "payment_recorded": "480.00",  "payment_approved": true, "net_income": "480.000000",  "revenue_on_own_trial_balance": true }
  ],
  "chosen": {
    "harbor":    { "cash_account": "primaryOperatingAccount", "revenue_parent": "salesRevenue", "payment_mode": "Bank Transfer" },
    "northwind": { "cash_account": "primaryOperatingAccount", "revenue_parent": "salesRevenue", "payment_mode": "Bank Transfer" }
  },
  "isolation": {
    "harbor_reading_northwind_invoice": "not returned (data: null)",
    "northwind_reading_harbor_invoice": "not returned (data: null)"
  }
}

The amounts differ on purpose, so the two sets of books are easy to tell apart. If tax settings apply, invoiced includes tax and net_income does not.

When Harbor's token asks for Northwind's invoice, the API answers {"data": null}, and the invoice's journal lookup returns an empty list. That is the same answer it gives for an invoice id that exists nowhere: a company cannot read another company's records, or learn that they exist. If a token is ever given the other company's invoice, the script stops with Isolation check failed.

To see both sets of books, sign in at sandbox.paprel.com again and open each company from the workspace switcher at the top of the sidebar.

If a run stops

The script saves every id it receives to paprel-saas-ledger-<run id>.json as it goes. After fixing the cause, continue from there:

Shell
CHECKPOINT=paprel-saas-ledger-<run id>.json node saas-ledger.mjs --resume

A resumed run never adds a company, client, item or invoice again once its id is saved, and the payment carries an idempotency key. If a request timed out before its response arrived, check the sandbox before resuming: that record may exist without an id in the checkpoint.

  • 401: check the client ID, the secret, and that PARTNER_DOMAIN is sandbox.paprel.com.
  • 403 ORGANIZATION_REQUIRED: the client's company is not an organization. Check Settings → Organization: if it says "No organization yet", sign up again and create the client in the new company. A client created in a member company gets the same error.
  • Other 403: the client is missing a permission from the table above.
  • 409 ORGANIZATION_CAPACITY_FULL or a "free company slot" message: the paid plan has no room. Raise the company count first, as the Organization API guide explains; the script never changes billing.
  • "No … found in this company": the new company's chart of accounts or settings didn't match the rules. Set the override variable it names.

What this means for your product

  • Onboarding: when a customer signs up, your backend adds their company and stores its id with your customer record.
  • Every request: get a token with that customer's member_company_id. It is short-lived, works in that company only, and is recorded in the activity log of both the organization and the company.
  • One secret: the organization's App Connect client is the only credential your backend keeps.
  • Reports: trial balance, income statement, balance sheet and general ledger are per company, so you can show each customer their own books in your product, through the API or the Embedded UI components.

When to move to the Partner API

The script uses the self-serve Organization API, so it runs on a free sandbox signup. Platforms that need their own domain and brand, user accounts for their customers' staff, or signed-in sessions use the Partner API on Enterprise. The steps stay the same: POST /v1/partner/companies adds the company, a minted user session replaces the member_company_id token, and everything after that uses the same Accounting API calls.

Hand this to your AI agent

Paste this into Claude Code, Cursor or another coding agent. It stays in the sandbox and asks before it creates anything.

Prompt for your AI agent
Show me how Paprel gives each customer of a SaaS its own ledger.

Read first:
https://paprel.com/documentation/guides/multi-tenant-accounting-api
https://paprel.com/examples/saas-ledger.mjs
Download that script (Node.js 20 or newer), read it, and run
it for the steps below. Do not write your own API calls.

Rules:
- The script only talks to the sandbox. Do not edit it.
- Use APP_CONNECT_CLIENT_ID and APP_CONNECT_CLIENT_SECRET from
  my environment and PARTNER_DOMAIN=sandbox.paprel.com. If a
  credential is missing, stop and tell me how to set it.
- Never print, log or commit the client secret.
- Never change the company count or the plan.
- Do not delete or suspend anything.
- If a run fails, do not start a new run. Show me the error and
  the checkpoint file, and offer --resume after the cause is fixed.

Steps:
1. Run: node saas-ledger.mjs --discover
   If "ready" is false, stop and tell me why.
2. Ask me for a contact email I control. Pass it as CONTACT_EMAIL.
3. Tell me the next step creates two real sandbox companies with
   an invoice and a payment in each, and is not rolled back.
   After my yes, run: node saas-ledger.mjs --run
4. Show me the customers table, the accounts the script chose,
   and the "isolation" lines. Both isolation lines must say
   "refused" or "not returned"; if not, stop and tell me.
5. Tell me how to open each company: sign in at
   sandbox.paprel.com again and use the workspace switcher.

Next steps

Evaluate Paprel

Build in sandbox, launch with a production trial

Use sandbox for developer testing with no billing. When you are ready for real workflows, start production on a monthly plan with a 14-day free trial.

API-First Delivery
Audit-Ready Controls
Sandbox And Guided Rollout
We use cookies to improve your experience.

We use cookies and local storage for essential settings and, with your choices, analytics and advertising. Online identifiers and activity can be personal data. You can reject optional categories or change your choices.

These cookies are necessary for the basic functionality of the website, ensuring a seamless user experience. They cannot be disabled.