# Keep a web shop's stock levels up to date

Use this recipe to show how many of each product you can sell, for
example in a web shop. You read every stock item once, and then read only
the items that changed 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.

You need the scope `stock:read`.

## Work out how many are free

Each stock item has three quantities, which Omono changes as your staff
work. You can't set them through the API.

- `inStock` is the quantity on your shelves. Goods leave `inStock` when
  your staff pick a dispatch, not when it ships, so a picked dispatch is
  already out of `inStock`.
- `allocated` is the quantity reserved for dispatches that are waiting to
  be picked.
- `onOrder` is the quantity you expect from issued purchase orders.

The quantity free to sell is `inStock` minus `allocated`. If that's less
than 0, use 0. The kind of item changes the rule:

- **A normal item** (`kind: normal`): use `inStock - allocated`.
- **A never-diminishing item** (`kind: never_diminishing`), such as a
  service, labour or carriage, has no stock level. It's always available.
- **An assembly** (`kind: assembly`) is built from the components in its
  `bom` when its dispatch is picked, so it has no stock of its own. For
  each component, divide the component's free quantity by the `quantity`
  in the BOM, and take the smallest result, rounded down. Skip the
  components that are never-diminishing, because they never run out. An
  assembly whose components are all never-diminishing is always available.

Only items whose `canSell` is `true` and whose `archived` is `false` are
for sale. Show any other item as not for sale, but keep it in your copy:
the components of an assembly are often items you can't sell on their own,
and you need their quantities.

### Example

A pump kit is an assembly with three components in its `bom`:

| Component      | `kind`              | BOM `quantity` | `inStock` | `allocated` | Free | Kits it allows |
| -------------- | ------------------- | -------------- | --------- | ----------- | ---- | -------------- |
| Pump body      | `normal`            | 1              | 12        | 2           | 10   | 10 ÷ 1 = 10    |
| Bolt           | `normal`            | 4              | 30        | 0           | 30   | 30 ÷ 4 = 7.5   |
| Fitting labour | `never_diminishing` | 0.5            |           |             |      | Skipped        |

The smallest result is 7.5, which rounds down to 7, so you can sell 7 pump
kits. If staff then allocate 4 bolts to another dispatch, the bolts allow
26 ÷ 4 = 6.5 kits, and you can sell 6.

## Load everything once

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

This reads every stock item, including the ones you can't sell and the
archived ones. Follow `nextCursor` until `hasMore` is `false`. Keep each
item's `id`, `partNo`, `kind`, `canSell`, `archived`, quantities and
`bom`. Save the highest `updated` value you saw: it's the mark where your
first check for changes begins. Take the mark from the items you received,
never from your own clock, as
[Load everything the first time](/docs/syncing#initial-load) explains.
Learn more about [pagination](/docs/reading#pagination).

Don't add `canSell=true` to the list. An item whose `canSell` changes to
`false` would then drop out of every later check for changes, and your
shop would keep selling it.

## Check for changes every few minutes

Ask for the items 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/stock-items?archived=any&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.

Every change to an item's stock level, including a pick, a receipt and a
stock take, gives the item a new `updated` time, so the list holds every
item whose quantities moved. Replace your copy of each one, and work out
its free quantity again. When a component changes, also work out the free
quantity again for each assembly whose `bom` names it. Because of the
60-second overlap, you'll see some items twice, so always replace your
copy rather than adding a second one.

Don't poll more often than every 2 minutes, as
[Plan your requests](/docs/limits#planning) explains. Each request counts
against your [rate limits](/docs/limits#the-limits). Start your polls at a random second
past the minute, not on the minute, as
[Spread your polls over time](/docs/limits#spread-polls) explains.

## 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.                                                             |
