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 anchoraiOr with yarn:
yarn add anchoraiQuick 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); // trueSDK Structure
The SDK has 5 namespaces:
| Namespace | Purpose | Key Methods |
|---|---|---|
anchor.agents | Agent registry and lifecycle | create, get, list, update, delete, suspend, activate |
anchor.config | Agent configuration with versioning | get, update, versions, getVersion, rollback |
anchor.data | Governed key-value data storage | write, writeBatch, read, readFull, delete, deletePrefix, list, search |
anchor.checkpoints | State snapshots and rollback | create, list, get, restore, delete |
anchor.audit | Hash-chained audit trail | query, get, verify, export |
Framework Integrations
LangChain
import { AnchorMemory } from 'anchorai';
const memory = new AnchorMemory(anchor, agent.id);
// Use with LangChain chains/agentsCrewAI
import { AnchorCrewMemory } from 'anchorai';
const memory = new AnchorCrewMemory(anchor);
// Use with CrewAI crewsMem0
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 policiesError 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.