---
title: "Label stock conditions"
description: "Keep stock condition labels in your app and read the separate balance for each label at a place."
url: "https://agentpost.tools/docs/resourceshark/guides/conditions"
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"
---

# Label stock conditions

Use condition labels to keep samples separate from your other stock at the same place. Keep the labels your app uses in one constant so every write and read spells them the same way. [Scopes](/docs/resourceshark/concepts/scopes.md) explains how a label picks a balance.

## 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\. Add unlabeled stock at a place

Use `api` and the `dev` secret key `key` for a project that adopted ResourceShark. Create a place and a resource kept by place. Add six items with `placeId` and leave out `condition` to use the unlabeled balance. Run the blocks in order on your server.

```ts
const conditionExample = crypto.randomUUID();
const { place: sampleShelf } = await api('POST', '/v1/resources/places', key, {
  key: `sample-shelf-${conditionExample}`,
  name: 'Sample shelf',
});
const { resource: sampleStock } = await api(
  'POST',
  '/v1/resources/resources',
  key,
  {
    key: `sample-stock-${conditionExample}`,
    name: 'Coffee bags',
    unit: { code: 'bag', singular: 'bag', plural: 'bags', precision: 0 },
    settings: { time: 'none', identity: 'count', supplyExpiry: 'none' },
    scope: { places: true, holders: false },
    tracking: 'none',
    shortage: 'deny',
  },
);
const unlabeledStockScope = { placeId: sampleShelf.id };
await api(
  'POST',
  '/v1/resources/supply',
  key,
  {
    resourceId: sampleStock.id,
    scope: unlabeledStockScope,
    kind: 'added',
    quantity: 6,
    evidence: { kind: 'receipt', ref: conditionExample },
    sourceKey: `sample-receipt:${conditionExample}`,
  },
  { 'Idempotency-Key': `sample-supply-${conditionExample}` },
);
```

## 2\. Move stock into an app label

Define `sample` once in your app’s `stockConditions` constant. Use it as the destination `condition` in a transfer of two items. Give both scopes the same `placeId` to keep the stock at the same place.

```ts
const stockConditions = { sample: 'sample' } as const;
await api(
  'POST',
  '/v1/resources/transfers',
  key,
  {
    resourceId: sampleStock.id,
    from: unlabeledStockScope,
    to: { ...unlabeledStockScope, condition: stockConditions.sample },
    quantity: 2,
    sourceKey: `label-samples:${conditionExample}`,
  },
  { 'Idempotency-Key': `transfer-to-sample-${conditionExample}` },
);
```

## 3\. See what a misspelling does

In this example, send a second transfer with the misspelling `smaple`. ResourceShark accepts it because it fits the label format. Each spelling gets its own balance. A label with a space in the middle answers [`invalid_request`](/docs/resourceshark/errors.md#invalid_request).

```ts
const misspelledSample = 'smaple';
await api(
  'POST',
  '/v1/resources/transfers',
  key,
  {
    resourceId: sampleStock.id,
    from: unlabeledStockScope,
    to: { ...unlabeledStockScope, condition: misspelledSample },
    quantity: 1,
    sourceKey: `misspell-sample:${conditionExample}`,
  },
  { 'Idempotency-Key': `transfer-to-smaple-${conditionExample}` },
);
```

## Check it worked

Read the three balances at the same place. The unlabeled balance has three items, `sample` has two, and `smaple` has one.

```ts
async function stockAtCondition(condition?: string) {
  const query = new URLSearchParams(unlabeledStockScope);
  if (condition !== undefined) query.set('condition', condition);
  const { balance } = await api(
    'GET',
    `/v1/resources/resources/${sampleStock.id}/balance?${query}`,
    key,
  );
  return balance.onHand;
}

console.log(
  await stockAtCondition(),
  await stockAtCondition(stockConditions.sample),
  await stockAtCondition(misspelledSample),
); // 3 2 1
```

To fix a label on existing stock, transfer it from the misspelled condition to the intended one. The [stock reference](/docs/resourceshark/reference/stock.md) has the transfer request, and the [availability reference](/docs/resourceshark/reference/availability.md) has the balance read.
