# Export invoices to accounting or BI

Use this recipe to copy the invoices Omono raises into another system, such
as an accounting package or a business intelligence (BI) tool. You read
every invoice once, and then read only the new and changed ones every few
minutes.

Every request in this recipe sends your API key in an
`Authorization: Bearer <key>` header. Paths are relative to
`https://<host>/api/v1`, and the examples show the full path. This recipe
only reads, but `GET /invoices` is in beta, so it can change in any way, at
any time, until it's marked stable. Learn more about
[beta operations](/docs/compatibility#stable-and-beta-operations).

> **Note:** If your organisation uses the Xero integration, Omono already
> sends its invoices to Xero. Use this recipe for other systems. Each
> invoice's `xero` tells you where it stands in that sync.

You need the scope `sales:read`, and `reference:read` to look up your tax
rates with `GET /tax-rates`. To export the cost and margin of each
invoice, your key also needs `stock:read`.

## How invoices work in Omono

Omono raises an invoice when a dispatch ships. So each invoice is one
shipped dispatch of a sales order, and the invoice's `id`, like its
`dispatchId`, is the dispatch's ID. The invoice holds its number, dates
and totals. Its goods are the dispatch's line items, at the prices of the
order's line items. Learn more about
[dispatches and invoicing](/docs/workflow).

Each invoice also has:

- `exchangeRate`: the rate of its sales order, which is the rate the
  invoice was raised at, because the rate can't change once a dispatch is
  invoiced.
- `totalValueBase` and `totalTaxBase`: its totals in your base currency,
  as Omono saved them. Use them rather than converting the totals
  yourself, which can differ by 0.01. They're `null` on some old invoices.
- `xero`: the status of its sync to Xero, such as `ok` or `error`, and
  Xero's ID for the invoice in `id`. It's `null` when your organisation
  isn't connected to Xero.

## Load everything once

```http
GET /api/v1/invoices?updated_since=1970-01-01T00:00:00.000Z&sort=updated&limit=200
```

Follow `nextCursor` until `hasMore` is `false`. Save the highest `updated`
value you saw: it's the mark where your first check for changes begins.
Take the mark from the invoices you received, never from your own clock,
as [Load everything the first time](/docs/syncing#initial-load) explains.

To export one customer's invoices, still load all of them once, and keep
only the ones whose `companyId` is that customer's. Then add
`companyId=<id>` to every check for changes. A first load of every invoice
sorted by `updated` stays fast however many customers you have, while a
first load for one customer can time out when it's a small part of a long
history.

## Find one invoice

To find an invoice by its number, send the number without the "INV-"
prefix:

```http
GET /api/v1/invoices?invoiceNo=10234
```

Send up to 100 numbers, separated by commas. A number that no invoice has
is left out, so the list can be empty.

## Check for new and changed invoices

Every few minutes, ask for the invoices that changed since your mark, minus
60 seconds. For example, if the highest `updated` you saw was
`2026-10-03T09:55:30.123Z`, send:

```http
GET /api/v1/invoices?updated_since=2026-10-03T09:54:30.123Z&sort=updated&limit=200
```

Follow `nextCursor` until `hasMore` is `false`, then save the highest
`updated` value you saw as your new mark.
[Poll for changes](/docs/syncing#incremental-sync) explains each step.

The list holds every invoice raised or changed since then. An invoice's
amounts don't change once it's raised, but its dispatch can, for example
when your staff add a tracking number, and a sync to Xero changes its
`xero` and its `updated` time. A change to a line item of the order can
also bring back an invoice with nothing changed that you can see. Because
of the 60-second overlap, you'll also see some invoices twice. So match
each one to your copy by `id`, and add it or replace it.

## Get the goods on an invoice

```http
GET /api/v1/sales-orders/{salesOrderId}?include=lineItems,dispatches
```

Find the dispatch whose `id` is the invoice's `id`. Each of its
`lineItems` names a line item of the order in `salesOrderLineItemId`, and
gives the quantity on this invoice. Take the description, `unitPrice` and
`taxRateId` from that line item of the order. Read each order once for all
its invoices. To read many orders in one request, send up to 100 IDs in
`ids`: `GET /api/v1/sales-orders?ids=<id>,<id>&include=lineItems,dispatches`.

Use `unitPrice`, which is the price of one unit after any discount. Don't
use `price`: it's the list price, before the discount, so a discounted line
would be exported at too high a price.

Omono works out the invoice's totals one line at a time, in the same way as
it does for any [document's totals](/docs/conventions#money):

1. The line's value is its quantity on the invoice multiplied by
   `unitPrice`, rounded to 2 decimal places, with halves rounded up.
2. The line's tax is its rounded value multiplied by its tax rate, also
   rounded to 2 decimal places. Read the rate, as a percentage, from
   `GET /tax-rates`. A line for a tax-exempt stock item has no tax.
3. The invoice's `totalValue` is the sum of the line values, and its
   `totalTax` is the sum of the line taxes. `totalWithTax` is the two added
   together.

To match Omono's totals exactly, round each line before you add them up.

### Example

An invoice ships two line items of an order. Both line items have a 20%
tax rate, and your organisation prices to 2 decimal places:

| Line item | `price` | `discount` | `unitPrice` | Quantity on the invoice | Value                     | Tax                    |
| --------- | ------- | ---------- | ----------- | ----------------------- | ------------------------- | ---------------------- |
| Gearbox   | 50.00   | 10         | 45.00       | 3                       | 3 × 45.00 = 135.00        | 135.00 × 20% = 27.00   |
| Seal kit  | 19.99   | 15         | 16.99       | 7                       | 7 × 16.99 = 118.93        | 118.93 × 20% = 23.79   |
| **Total** |         |            |             |                         | **253.93** (`totalValue`) | **50.79** (`totalTax`) |

The invoice's `totalWithTax` is 253.93 + 50.79 = 304.72. If you used
`price` instead, the lines would add up to 289.93, which doesn't match the
invoice.

Credit notes are separate documents. Copy them with `GET /credit-notes`
in the same way.

## Export the cost and margin

If your key also has the `stock:read` scope, each invoice has three more
fields, in the invoice's currency:

- `costOfGoodsSold`: for each line item on the invoice, its quantity
  multiplied by the `lastCost` of the order's line item, added up.
- `grossProfit`: `totalValue` minus `costOfGoodsSold`.
- `grossMargin`: `grossProfit` as a percentage of `totalValue`.

`costOfGoodsSoldBase` and `grossProfitBase` give the first two in your
base currency. A line item with no cost counts as 0. An invoice whose
goods have no cost has a `costOfGoodsSold` of 0 and a `grossMargin` of
`null`, and one with some uncosted goods has a margin higher than it
should be. Check for that before you report it. The fields are `null` on some old invoices. Without
`stock:read`, they're left out of the response. Learn more about
[costs and margins](/docs/reading#costs).

## Get the invoice PDF

To keep a copy of the invoice that your customer received, download its
PDF with the invoice's `id`:

```http
GET /api/v1/invoices/{id}/pdf
```

The response is the PDF file itself, with `Content-Type: application/pdf`,
and the `Content-Disposition` header gives its file name, such as
`INV-1042.pdf`. Download each invoice's PDF once, when you first see the
invoice. Each PDF counts as 5 requests against your organisation's rate
limit, so in a first load, download the PDFs one or two at a time after you
have read the list. Learn more about [PDFs](/docs/reading#pdfs). Credit
notes have PDFs too: `GET /api/v1/credit-notes/{id}/pdf`.

## Errors to handle

| Status   | Code                                                                                                           | What to do                                                                                                                                                                                       |
| -------- | -------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| 422      | [`validation-failed`](/docs/errors/validation-failed)                                                          | A query parameter is wrong, for example an `updated_since` that isn't a date and time. Fix the request. If `errors[].field` is `cursor`, start the check again from your mark, without a cursor. |
| 429, 503 | [`rate-limited`](/docs/errors/rate-limited), [`temporarily-unavailable`](/docs/errors/temporarily-unavailable) | Retry after the number of seconds in `Retry-After`. Don't save a new mark until you've read the last page, so that you miss nothing.                                                             |
