attest Docs

SDK

prepareWitness()

Wrap an operation so attest observes it before and after it runs.

Usage #

attest.prepareWitness(event) builds the assembly observation synchronously, with no network call, and returns a prepared pair. Calling prepared.execute(operation) records the assembly, runs your operation once, records the delivery, and finalizes the pair.

Because the identity exists before anything runs, you can persist it first and recover after a crash.

Request · JavaScript (SDK)
import { Attest } from "@attestinfra/sdk";

const attest = new Attest({ apiKey: process.env.ATTEST_API_KEY });

// Your operation. Replace with the real call.
async function transferFunds() {
  return { transferId: "tr_82" };
}

const prepared = attest.prepareWitness({
  eventType: "transfer.requested",
  source: "checkout",
  destination: "payments",
  action: "transfer",
  evidence: { actionId: "transfer_82", amount: 500, currency: "USD" },
});

// Persist before executing.
console.log(prepared.pairId, prepared.assemblyIdentity.timestamp);

const { result, witness, currentInvocationOperationExecuted } =
  await prepared.execute(() => transferFunds());
console.log(result, witness.reconciliationStatus, currentInvocationOperationExecuted);
Output · Console
oN4T9mJ0Xq2Lr8vYb3KcWd7Pz1Ae6HsF 2026-10-19T14:03:07.120Z
{ transferId: 'tr_82' } matched true

Parameters #

NameTypeRequiredDescription
eventTypestringRequiredClassification of the operation. Compared across stages.
actionstringRequiredThe operation. Compared across stages.
evidenceobjectRequiredPlain JSON object of boundary values. Compared across stages. Validated locally against the evidence limits and frozen as a snapshot.
pairIdstringOptionalDefaults to 24 random bytes, base64url-encoded (32 characters).
eventIdstringOptionalDefaults to a random UUID.
timestampDate | string | numberOptionalDefaults to the time prepareWitness is called.
sourcestringOptionalWhere the operation starts.
destinationstringOptionalWhere the operation is headed. The delivery observation uses it as its source.
outcomestringOptionalSent on the assembly observation. The delivery outcome is set by execute.

Returns #

prepareWitness returns { pairId, assemblyIdentity, execute }.

FieldDescription
pairIdThe pair identifier. Persist it.
assemblyIdentityThe complete assembly observation that will be sent, including timestamp and eventId. Persist it.
execute(operation)Runs the round-trip. Resolves with { result, witness, currentInvocationOperationExecuted: true }, where result is your operation's return value and witness is the finalized pair state.

What execute does #

  1. Sends the assembly observation. If the pair already has this assembly, it throws AttestWitnessAssemblyExistsError without running your operation.
  2. Calls operation() once. It never retries it.
  3. If the operation throws: sends a delivery with outcome: "error" and finalizes, both best-effort, then rethrows your error unchanged.
  4. If the operation succeeds: sends a delivery with outcome: "ok" and finalizes. If either call fails, it throws AttestWitnessDeliveryError carrying pairId and deliveryObservation. Your operation has already run.