Docs
Quickstart

Quickstart

Run a complete local example with a hard write ceiling and an outcome assertion.

Protect a small repair job

Install captar, save this as quickstart.mjs, and run node quickstart.mjs. It uses three local customer records; no external service is called.

import { run } from 'captar';
 
const customers = [{ id: 1 }, { id: 2 }, { id: 3 }];
const repaired = new Set();
const result = await run(
  'customer-repair',
  {
    limits: { resources: { 'db.writes': 3 } },
    outcome: { 'records.processed': { equals: customers.length } },
  },
  async (execution) => {
    for (const customer of customers) {
      const write = execution.reserve('db.writes', 1);
      // Replace this local operation with an awaited, idempotent database write.
      repaired.add(customer.id);
      execution.commit(write);
      execution.checkpoint('customer-id', customer.id);
    }
    execution.metric('records.processed', repaired.size);
  }
);
console.log(result.receipt);

Expected result: succeeded, three committed writes, checkpoint customer-id: 3, and metric records.processed: 3.

Change the write ceiling to 2: the third reservation throws ContractViolationError before its guarded operation starts. Change the expected outcome to 4: all three operations finish, but the outcome check fails.

count() counts resource usage. Outcome assertions read values reported with metric(); counting a resource does not report an outcome metric.

For durable receipts, use runStored() and a local store. For batching and restart, follow the recovery demo.