Docs/Quick Start

Quick Start

Install, initialize, record, and verify agent actions in 5 steps.

1. Install the SDK

bash
npm install @invariance/sdk

Or with pnpm:

bash
pnpm add @invariance/sdk

2. Generate Keys & Init

Generate an Ed25519 keypair for signing receipts, then initialize the SDK:

setup.tstypescript
import { Invariance } from '@invariance/sdk';

// Generate a signing keypair (do this once, store privateKey securely)
const { privateKey, publicKey } = Invariance.generateKeypair();

// Initialize the SDK
const inv = Invariance.init({
  apiKey: process.env.INVARIANCE_API_KEY!,
  privateKey: process.env.INVARIANCE_PRIVATE_KEY!,
});

3. Create Session & Record

Sessions group related actions into hash-chained receipt sequences:

record.tstypescript
// Create a session (lazy — returns immediately)
const session = inv.session({
  agent: 'my-agent',
  name: 'task-run-1',
});

// Record actions — each receipt is hash-chained to the previous
await session.record({
  action: 'search',
  input: { query: 'latest AI papers' },
  output: { resultCount: 15 },
});

await session.record({
  action: 'summarize',
  input: { paperId: 'arxiv-2024-001' },
  output: { summary: 'This paper proposes...' },
});

// End the session — seals the chain
session.end();

4. Wrap with Policy

Use wrap() for policy-checked execution with automatic receipt creation:

wrap.tstypescript
// Configure policies at init
const inv = Invariance.init({
  apiKey: process.env.INVARIANCE_API_KEY!,
  privateKey: process.env.INVARIANCE_PRIVATE_KEY!,
  policies: [
    { action: 'transfer', maxAmountUsd: 10000 },
    { action: 'swap', rateLimit: { max: 10, windowMs: 60000 } },
  ],
});

// wrap() checks policies → executes → records receipt
const { result, receipt } = await inv.wrap(
  {
    agent: 'trader',
    action: 'swap',
    input: { from: 'ETH', to: 'USDC', amount: '5' },
  },
  async () => {
    // Your actual swap logic
    return { txHash: '0xabc...', amountOut: '17500' };
  },
);

console.log(result.txHash);      // '0xabc...'
console.log(receipt.hash);       // SHA-256 hash of the action
console.log(receipt.signature);  // Ed25519 signature

5. Verify & Shutdown

Verify chain integrity and shut down gracefully:

verify.tstypescript
import { verifyChain } from '@invariance/sdk';

// Fetch receipts and verify the hash chain
const receipts = await inv.query({ sessionId: session.id });
const verification = verifyChain(receipts);

console.log(verification.valid);        // true
console.log(verification.receiptCount); // number of receipts

// Always shut down to flush remaining receipts
await inv.shutdown();

Next Steps

On this page
1. Install the SDK2. Generate Keys & Init3. Create Session & Record4. Wrap with Policy5. Verify & ShutdownNext Steps