---
title: "Cap a holder's open holds"
description: "Give each holder a fixed number of units and reserve one in every hold to enforce an open-hold cap."
url: "https://agentpost.tools/docs/resourceshark/guides/hold-cap"
kind: guide
needs:
  - what: "The `api` helper"
    from: "https://agentpost.tools/docs/platform/quickstart.md"
  - what: "A `dev` secret key, `key`, for a project that adopted ResourceShark"
    from: "https://agentpost.tools/docs/platform/guides/keys.md"
---

# Cap a holder’s open holds

Give each holder a balance with one unit per open hold your app allows. Reserve a unit in the same request as the stock, so two requests racing for the last unit can place only one hold. [Scopes](/docs/resourceshark/concepts/scopes.md) explains a holder’s balance, and [Holds](/docs/resourceshark/concepts/holds.md) explains how a hold reserves its lines together.

## Before you start

-   The `api` helper, from [Platform quickstart](/docs/platform/quickstart.md).
-   A `dev` secret key, `key`, for a project that adopted ResourceShark, from [Create a project and issue a key](/docs/platform/guides/keys.md).

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\. Give a holder one cap unit

Use `api` and the `dev` secret key `key` for a project that adopted ResourceShark. Create a resource kept per holder and give one holder a unit. Create a stock resource with three items so the example can reach the cap while stock is still free. Run the blocks in order on your server.

```ts
const capExample = crypto.randomUUID();
const cappedHolder = `customer:${capExample}`;
const capResourceSettings = {
  settings: { time: 'none', identity: 'count', supplyExpiry: 'none' },
  tracking: 'none',
  shortage: 'deny',
};
const { resource: holdCap } = await api(
  'POST',
  '/v1/resources/resources',
  key,
  {
    ...capResourceSettings,
    key: `open-hold-cap-${capExample}`,
    name: 'Open holds',
    unit: { code: 'hold', singular: 'hold', plural: 'holds', precision: 0 },
    scope: { places: false, holders: true },
  },
);
const { resource: cappedStock } = await api(
  'POST',
  '/v1/resources/resources',
  key,
  {
    ...capResourceSettings,
    key: `capped-stock-${capExample}`,
    name: 'Trial stock',
    unit: { code: 'item', singular: 'item', plural: 'items', precision: 0 },
    scope: { places: false, holders: false },
  },
);

await api(
  'POST',
  '/v1/resources/supply',
  key,
  {
    resourceId: holdCap.id,
    scope: { holderRef: cappedHolder },
    kind: 'added',
    quantity: 1,
    evidence: { kind: 'account', ref: cappedHolder },
    sourceKey: `hold-cap:${cappedHolder}`,
  },
  { 'Idempotency-Key': `hold-cap-supply-${capExample}` },
);
await api(
  'POST',
  '/v1/resources/supply',
  key,
  {
    resourceId: cappedStock.id,
    kind: 'added',
    quantity: 3,
    evidence: { kind: 'receipt', ref: capExample },
    sourceKey: `stock-receipt:${capExample}`,
  },
  { 'Idempotency-Key': `cap-stock-supply-${capExample}` },
);
```

## 2\. Hold stock and a cap unit together

Have your server add the cap line to every hold it places for the holder. Put `holderRef` in that line’s `scope` to select the holder’s cap balance. A single hold request sets aside both lines together.

```ts
const cappedHoldLines = [
  { resourceId: cappedStock.id, quantity: 1 },
  { resourceId: holdCap.id, scope: { holderRef: cappedHolder }, quantity: 1 },
];
const cappedHoldRequest = {
  lines: cappedHoldLines,
  holderRef: cappedHolder,
  ttl: '15m',
};
const { hold: firstCappedHold } = await api(
  'POST',
  '/v1/resources/holds',
  key,
  {
    ...cappedHoldRequest,
    sourceKey: `capped-checkout:${capExample}:1`,
  },
  { 'Idempotency-Key': `capped-hold-${capExample}-1` },
);
```

## 3\. Read what is free when the cap is reached

Try holding another item for the same holder, and read `details` from the error `api` throws.

```ts
let capError;
try {
  await api(
    'POST',
    '/v1/resources/holds',
    key,
    { ...cappedHoldRequest, sourceKey: `capped-checkout:${capExample}:2` },
    { 'Idempotency-Key': `capped-hold-${capExample}-2` },
  );
} catch (thrown) {
  capError = (thrown as { error?: any }).error;
}
if (capError?.code !== 'insufficient_supply') {
  throw new Error('Expected a short hold');
}
const [shortCapLine] = capError.details.lines;
console.log(capError.code, shortCapLine.quantity, shortCapLine.available); // insufficient_supply 1 0
```

The refusal names the cap resource in `details.lines` and says the line asked for one unit and had zero free. ResourceShark refuses the whole hold, so the second checkout takes no stock. A repeat of the same request with the same `Idempotency-Key` [repeats the error](/docs/resourceshark/reference.md#retrying-a-write). To try the same checkout again, use a new `Idempotency-Key` and keep the checkout’s `sourceKey`. This also applies when you change the request to ask for less. [`insufficient_supply`](/docs/resourceshark/errors.md#insufficient_supply) lists what the error carries.

## 4\. Give the cap unit back when the hold ends

On a commit, send every claim ID and take zero from the cap line. This commits the stock and releases the cap unit in one write. Releasing the hold or letting it expire also frees the cap unit.

```ts
const [cappedStockClaim, capUnitClaim] = firstCappedHold.lines;
await api(
  'POST',
  `/v1/resources/holds/${firstCappedHold.id}/commit`,
  key,
  {
    lines: [
      { claimId: cappedStockClaim.claimId, quantity: 1 },
      { claimId: capUnitClaim.claimId, quantity: 0 },
    ],
    evidence: { kind: 'order', ref: capExample },
    sourceKey: `capped-order:${capExample}`,
  },
  { 'Idempotency-Key': `capped-commit-${capExample}` },
);
```

## Check it worked

Read the holder’s cap balance and the stock balance. One cap unit is free again and two items are still available.

```ts
const capBalanceQuery = new URLSearchParams({ holderRef: cappedHolder });
const { balance: savedCapBalance } = await api(
  'GET',
  `/v1/resources/resources/${holdCap.id}/balance?${capBalanceQuery}`,
  key,
);
const { balance: savedCappedStock } = await api(
  'GET',
  `/v1/resources/resources/${cappedStock.id}/balance`,
  key,
);
console.log(savedCapBalance.available, savedCappedStock.available); // 1 2
```

The [holds reference](/docs/resourceshark/reference/holds.md) has the commit and release requests.
