---
title: "Rebuild your stock view from movements | Docs | Inventory by R&D Copilot"
description: "Keep your own copy of the balances in step with the ledger: read the history once, then follow the events."
canonical: "https://inventory.rdcopilot.com/docs/guides/rebuild-your-stock-view/"
language: en
updated: 2026-11-22
---

# Rebuild your stock view from movements

Keep your own copy of the balances in step with the ledger: read the history once, then follow the events.

Many systems keep a copy of the stock for their own screens or reports. The ledger makes the copy easy to check, because a balance is the sum of its movements and every movement and event carries the revision it produced.

## Read the history

Read the stock card of the item in pages of up to 500 movements, oldest first, until `nextCursor` is `null`. For each location, add up `onHandDelta` and `reservedDelta`; the revision of the last movement is the revision of the balance. See [Pagination](https://inventory.rdcopilot.com/docs/reference/pagination/).

Sum the movements of an item

```javascript
// Rebuild the balances of one item from its movements.
const balances = new Map(); // locationId -> {onHand, reserved, revision}
let cursor;
do {
  const url = new URL(`${BASE}/inventory/v1/items/${productId}/movements`);
  url.searchParams.set('limit', '500');
  if (cursor) url.searchParams.set('cursor', cursor);
  const page = await (await fetch(url, {headers: {Authorization: `Bearer ${token}`}})).json();
  for (const m of page.items) {
    const b = balances.get(m.locationId) ?? {onHand: 0n, reserved: 0n, revision: 0};
    b.onHand += toMicro(m.onHandDelta);
    b.reserved += toMicro(m.reservedDelta);
    b.revision = m.resultingRevision;
    balances.set(m.locationId, b);
  }
  cursor = page.nextCursor ?? undefined;
} while (cursor);

// toMicro('78.5') === 78500000n: six decimals, exact, no floating point.
function toMicro(text) {
  const negative = text.startsWith('-');
  const [whole, fraction = ''] = text.replace('-', '').split('.');
  const value = BigInt(whole) * 1000000n + BigInt(fraction.padEnd(6, '0'));
  return negative ? -value : value;
}
```

## Check against the ledger

Read the availability of a balance. If your sum and the ledger agree on `onHand` and `reserved` at the same `revision`, your copy is exact. If the ledger is at a higher revision, a movement was recorded while you read: read the pages after your cursor again.

curl

```bash
curl "$RDC_BASE_URL/inventory/v1/availability?productId=6cbfcba3-55fd-4cf0-9709-6e75c9717bb3&locationId=b006344d-2fae-4970-bb6f-c32877084b4f" \
  -H "Authorization: Bearer $RDC_TOKEN"
```

JavaScript

```javascript
const response = await fetch(`${process.env.RDC_BASE_URL}/inventory/v1/availability?productId=6cbfcba3-55fd-4cf0-9709-6e75c9717bb3&locationId=b006344d-2fae-4970-bb6f-c32877084b4f`, {
  method: 'GET',
  headers: {
    Authorization: `Bearer ${process.env.RDC_TOKEN}`,
  },
});
const result = await response.json();
```

Python

```python
import os
import requests

response = requests.request(
    'GET',
    f'{os.environ["RDC_BASE_URL"]}/inventory/v1/availability?productId=6cbfcba3-55fd-4cf0-9709-6e75c9717bb3&locationId=b006344d-2fae-4970-bb6f-c32877084b4f',
    headers={
        'Authorization': f'Bearer {os.environ["RDC_TOKEN"]}',
    },
)
result = response.json()
```

PHP

```php
<?php

$curl = curl_init(getenv('RDC_BASE_URL') . '/inventory/v1/availability?productId=6cbfcba3-55fd-4cf0-9709-6e75c9717bb3&locationId=b006344d-2fae-4970-bb6f-c32877084b4f');
curl_setopt_array($curl, [
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('RDC_TOKEN'),
    ],
]);
$result = json_decode(curl_exec($curl), true);
curl_close($curl);
```

C#

```csharp
using System.Net.Http.Headers;

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, $"{Environment.GetEnvironmentVariable("RDC_BASE_URL")}/inventory/v1/availability?productId=6cbfcba3-55fd-4cf0-9709-6e75c9717bb3&locationId=b006344d-2fae-4970-bb6f-c32877084b4f");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Environment.GetEnvironmentVariable("RDC_TOKEN"));
var response = await client.SendAsync(request);
var result = await response.Content.ReadAsStringAsync();
```

Response 200

```json
{
  "customerOrgId": "690fa0e3-547f-4f93-96b7-824475cc3d7c",
  "legalCompanyId": "d84ba982-ca9a-4e91-9566-21bd23f7227b",
  "productId": "6cbfcba3-55fd-4cf0-9709-6e75c9717bb3",
  "locationId": "b006344d-2fae-4970-bb6f-c32877084b4f",
  "onHand": "68",
  "reserved": "10",
  "available": "58",
  "revision": 5
}
```

## Follow the events

After the first read, keep the copy current from the events. There is one event per movement, sent to the endpoint of a subscription and signed: check the signature before you use an event. See [Event catalogue](https://inventory.rdcopilot.com/docs/events/event-catalogue/) and [Verify signatures](https://inventory.rdcopilot.com/docs/events/signatures/).

1.  Find the balance
    
    Use `data.productId` and `data.locationId`.
    
2.  Compare the revision
    
    If `aggregateRevision` is not newer than the revision you hold, the event is a repeat or arrived late: skip it.
    
3.  Check there is no gap
    
    If it is more than one above your revision, an event is missing or still on its way: read the stock card from your last movement, or replay a parked event (see [Replay](https://inventory.rdcopilot.com/docs/events/replay/)).
    
4.  Apply the deltas
    
    Add `data.onHandDelta` and `data.reservedDelta` to your balance and keep `aggregateRevision`.
    

The stock card is the source of truth. An event is a prompt to read it sooner, never a replacement for reading it.
