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.
On this page
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.
// 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
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
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
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
$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#
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(); {
"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 and Verify signatures.
Find the balance
Use
data.productIdanddata.locationId.Compare the revision
If
aggregateRevisionis not newer than the revision you hold, the event is a repeat or arrived late: skip it.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).
Apply the deltas
Add
data.onHandDeltaanddata.reservedDeltato your balance and keepaggregateRevision.