Initialize the Invariance SDK, generate Ed25519 keypairs, configure transport and observability, and manage the client lifecycle.
The Invariance SDK is initialized through the static `Invariance.init()` factory method. This creates a configured client instance that manages transport (batching and flushing receipts to the API), observability (tracing, anomaly detection), and optional monitor polling.
Before initializing, you typically generate an Ed25519 keypair using `Invariance.generateKeypair()`. The private key is used to sign every receipt, creating a cryptographic audit trail. The public key is registered with the server so signatures can be verified independently.
The client uses a batched transport layer that queues receipts and flushes them periodically (default: every 5 seconds or 50 receipts). When you're done, call `inv.shutdown()` to flush remaining receipts and clean up timers.
The Invariance class is a thin orchestrator that owns a Transport instance (HTTP batching), an InvarianceTracer (observability), and optional monitor polling. Sessions and agents are created through it but manage their own receipt chains independently.
import { Invariance } from '@invariance/sdk';
// Generate a signing keypair (do this once, store securely)
const { privateKey, publicKey } = Invariance.generateKeypair();
// Initialize the SDK
const inv = Invariance.init({
apiKey: 'dev_your_api_key',
privateKey,
});
// ... use inv.session(), inv.wrap(), etc. ...
// Clean shutdown — flushes remaining receipts
await inv.shutdown();interface InvarianceConfig {
apiKey: string;
apiUrl?: string;
privateKey?: string;
policies?: PolicyRule[];
flushIntervalMs?: number;
maxBatchSize?: number;
maxQueueSize?: number;
onError?: ErrorHandler;
mode?: 'DEV' | 'PROD';
sampleRate?: number;
anomalyThreshold?: number;
onAnomaly?: (node: TraceEvent) => void;
devOutput?: 'ui' | 'console' | 'both';
captureReplaySnapshots?: boolean;
onMonitorTrigger?: (event: MonitorTriggerEvent) => void;
monitorPollIntervalMs?: number;
}npm install @invariance/sdkimport { Invariance } from '@invariance/sdk';
const { privateKey, publicKey } = Invariance.generateKeypair();
// Save privateKey to .env or secrets managerconst inv = Invariance.init({
apiKey: process.env.INVARIANCE_API_KEY!,
privateKey: process.env.INVARIANCE_PRIVATE_KEY!,
flushIntervalMs: 5000,
maxBatchSize: 50,
mode: 'PROD',
onError: (err) => console.error('[Invariance]', err),
});const healthy = await inv.healthCheck();
if (!healthy) throw new Error('Cannot reach Invariance API');process.on('SIGINT', async () => {
await inv.shutdown();
process.exit(0);
});| Option | Type | Default | Description |
|---|---|---|---|
| apiKey | string | (required) | API key for authentication (dev_, org_, inv_, or agent key) |
| apiUrl | string | "https://api.invariance.dev" | Base URL for the Invariance API |
| privateKey | string | undefined | Ed25519 private key (64-char hex) for signing receipts |
| policies | PolicyRule[] | [] | Local policy rules evaluated before recording |
| flushIntervalMs | number | 5000 | How often to flush batched receipts (ms) |
| maxBatchSize | number | 50 | Max receipts to batch before auto-flush |
| mode | 'DEV' | 'PROD' | 'PROD' | Observability mode — DEV captures everything, PROD samples |
import { InvarianceError } from '@invariance/sdk';
try {
const inv = Invariance.init({ apiKey: '' });
} catch (err) {
if (err instanceof InvarianceError) {
console.error(err.code); // 'INIT_FAILED'
console.error(err.message); // 'apiKey is required'
}
}