TypeScript SDK

The Anchor TypeScript SDK provides a full-featured client for the Anchor API with complete type definitions, modern async/await support, and framework integrations.

Installation

npm install anchorai

Or with yarn:

yarn add anchorai

Quick Start

import { Anchor } from 'anchorai';

const anchor = new Anchor({ apiKey: 'your-api-key' });

// Create an agent
const agent = await anchor.agents.create('support-bot', { environment: 'production' });

// Configure policies
await anchor.config.update(agent.id, {
  policies: {
    block_pii: true,
    block_secrets: true,
    retention_days: 90
  }
});

// Store data (policy-checked, audit-logged)
const result = await anchor.data.write(agent.id, 'user:123:preference', 'dark_mode');
console.log(result.allowed);  // true

// PII is blocked automatically
const blocked = await anchor.data.write(agent.id, 'user:123:ssn', '123-45-6789');
console.log(blocked.allowed);     // false
console.log(blocked.blockedBy);   // "policy:block_pii"

// Verify audit chain integrity
const verification = await anchor.audit.verify(agent.id);
console.log(verification.valid);  // true

SDK Structure

The SDK has 5 namespaces:

NamespacePurposeKey Methods
anchor.agentsAgent registry and lifecyclecreate, get, list, update, delete, suspend, activate
anchor.configAgent configuration with versioningget, update, versions, getVersion, rollback
anchor.dataGoverned key-value data storagewrite, writeBatch, read, readFull, delete, deletePrefix, list, search
anchor.checkpointsState snapshots and rollbackcreate, list, get, restore, delete
anchor.auditHash-chained audit trailquery, get, verify, export

Framework Integrations

LangChain

import { AnchorMemory } from 'anchorai';

const memory = new AnchorMemory(anchor, agent.id);
// Use with LangChain chains/agents

CrewAI

import { AnchorCrewMemory } from 'anchorai';

const memory = new AnchorCrewMemory(anchor);
// Use with CrewAI crews

Mem0

Anchor wraps Mem0 to add policy enforcement, audit trails, and checkpoints. Mem0 handles semantic memory and retrieval; Anchor adds governance.

import { AnchorMem0 } from 'anchorai';

// Wrap Mem0 with Anchor governance
const wrapped = new AnchorMem0(anchor, agent.id, mem0Client);

// Mem0 handles "what does the agent remember?"
// Anchor handles "what is the agent allowed to remember?"
const result = await wrapped.add('User prefers dark mode', { userId: 'user_123' });
console.log(result.allowed);  // true or false based on policies

Error Handling

import {
  AnchorError,
  AuthenticationError,
  NotFoundError,
  ValidationError,
  PolicyViolationError,
  RateLimitError
} from 'anchorai';

try {
  const result = await anchor.data.write(agent.id, 'key', 'value');
} catch (error) {
  if (error instanceof PolicyViolationError) {
    console.log(`Blocked: ${error.message}`);
  } else if (error instanceof AuthenticationError) {
    console.log('Invalid API key');
  } else if (error instanceof NotFoundError) {
    console.log('Agent not found');
  } else if (error instanceof RateLimitError) {
    console.log(`Rate limited. Retry after ${error.retryAfter}s`);
  }
}

TypeScript Support

The SDK includes full TypeScript type definitions. All methods, parameters, and return types are fully typed for better IDE support and type safety.

Requirements

  • Node.js 18+
  • TypeScript 4.5+ (optional, but recommended)

Documentation

For complete API reference, see the API documentation. For guides and examples, see the Guides section.