Skip to content
Agentpost robot in sunglasses on a red circleAgentpost

ResourceShark quickstart

Add 10,000 grams of roasted coffee at a roastery, hold 500 grams for an order, and commit the hold. Run these TypeScript blocks in order in the same Node 24 script as the Platform quickstart. Every request uses its api helper.

Before you start

This page's code continues the script of the pages above: run its blocks after theirs, in the same file. To run them as a script of their own, first copy each helper from the page that defines it, and set each other value, such as a key or an ID, to one you already have.

1. Adopt ResourceShark

Add ResourceShark to the project you created in the Platform quickstart. Keep using its dev secret key for the ResourceShark calls that follow. Adopt a service has the request and who may call it.

const resourceSharkAdoption = await api(
  'POST',
  `/v1/projects/${project.id}/services`,
  token,
  { service: 'resourceshark' },
);
console.log(resourceSharkAdoption.project.services.includes('resourceshark')); // true

const stockRun = crypto.randomUUID();
const stockPath = '/v1/resources';
const stockWrite = (path: string, attempt: string, body: unknown) =>
  api('POST', `${stockPath}${path}`, key, body, {
    'Idempotency-Key': `${stockRun}:${attempt}`,
  });

2. Create the roastery and the coffee resource

Create a place called Roastery and a resource measured in whole grams. Its scope.places setting keeps a separate balance at each place. Its shortage policy refuses a hold or consume that asks for more than is free.

const { place: roastery } = await api('POST', `${stockPath}/places`, key, {
  key: `roastery-${stockRun}`,
  name: 'Roastery',
});
const { resource: roastedCoffee } = await api(
  'POST',
  `${stockPath}/resources`,
  key,
  {
    key: `ethiopia-natural-roasted-${stockRun}`,
    name: 'Ethiopia Natural Roasted',
    unit: { code: 'g', singular: 'gram', plural: 'grams', precision: 0 },
    settings: { time: 'none', identity: 'count', supplyExpiry: 'none' },
    scope: { places: true, holders: false },
    tracking: 'none',
    shortage: 'deny',
    tags: ['roasted'],
    attributes: { origin: 'Guji', process: 'natural' },
  },
);
const coffeeLine = {
  resourceId: roastedCoffee.id,
  scope: { placeId: roastery.id },
  quantity: 500,
};

3. Add a roast and check availability

Record the roast as added supply. Its evidence points to your roast record. Its source key identifies this delivery of supply. Then check how much is free at the roastery.

await stockWrite('/supply', 'add-roast', {
  resourceId: roastedCoffee.id,
  scope: { placeId: roastery.id },
  kind: 'added',
  quantity: 10_000,
  evidence: { kind: 'roast', ref: `batch-${stockRun}` },
  sourceKey: `roast:${stockRun}`,
});
const { lines: coffeeAvailability } = await api(
  'POST',
  `${stockPath}/availability`,
  key,
  { lines: [coffeeLine] },
);
console.log(coffeeAvailability[0].available); // 10000

4. Hold 500 grams for an order

Place a hold for 15 minutes. The line names the place the coffee comes from. The top-level holderRef says who the hold is for.

const { hold: coffeeHold } = await stockWrite('/holds', 'hold-coffee', {
  lines: [coffeeLine],
  ttl: '15m',
  holderRef: 'customer:budi',
  sourceKey: `hold:${stockRun}:budi`,
});
console.log(coffeeHold.status, coffeeHold.lines[0].quantity); // open 500

5. See a second hold refused

Ask for 9,600 grams for another customer. Only 9,500 grams are free, so ResourceShark refuses the whole request with HTTP 409 and insufficient_supply. The answer’s error.class is exhausted. The Platform helper throws with the error code in its message.

let coffeeRefusal: string | undefined;
try {
  await stockWrite('/holds', 'hold-more-coffee', {
    lines: [{ ...coffeeLine, quantity: 9_600 }],
    ttl: '15m',
    holderRef: 'customer:second',
    sourceKey: `hold:${stockRun}:second`,
  });
} catch (error) {
  if (!(error instanceof Error)) throw error;
  const match = /^POST \/v1\/resources\/holds: (\w+) /.exec(error.message);
  if (!match) throw error;
  coffeeRefusal = match[1];
}
if (coffeeRefusal === undefined) throw new Error('The second hold succeeded');
console.log(coffeeRefusal); // insufficient_supply

6. Extend and commit the first hold

Extend the first hold to 30 minutes from the request. Commit it with the order as evidence. The commit takes all 500 grams from stock.

const { hold: longerCoffeeHold } = await stockWrite(
  `/holds/${coffeeHold.id}/extend`,
  'extend-coffee',
  { ttl: '30m' },
);
console.log(
  Date.parse(longerCoffeeHold.expiresAt) > Date.parse(coffeeHold.expiresAt),
); // true

const { hold: committedCoffee, movements: coffeeMovements } = await stockWrite(
  `/holds/${coffeeHold.id}/commit`,
  'commit-coffee',
  {
    evidence: { kind: 'order', ref: `order-${stockRun}` },
    sourceKey: `order:${stockRun}`,
  },
);
console.log(committedCoffee.status, coffeeMovements[0].quantity); // committed -500

Check it worked

Read the balance at the roastery. It has 9,500 grams on hand, none held, and 9,500 free to take.

const roasteryQuery = new URLSearchParams({ placeId: roastery.id });
const { balance: roasteryBalance } = await api(
  'GET',
  `${stockPath}/resources/${roastedCoffee.id}/balance?${roasteryQuery}`,
  key,
);
console.log(
  roasteryBalance.onHand,
  roasteryBalance.held,
  roasteryBalance.available,
); // 9500 0 9500

Next, Change ResourceShark’s config shows how to set retention for change feed entries and resolved holds.