Node.js
Node.js SDK
The official Node.js/TypeScript SDK for integrating Attest into your AI agents.
Installation
terminal
npm install @attest/sdk # or yarn add @attest/sdk # or pnpm add @attest/sdk
Quick Start
agent.ts
import { Attest } from '@attest/sdk'; // Initialize the client const client = new Attest({ apiKey: 'sk_live_...' }); // Validate an action const decision = await client.validate({ action: 'send_email', payload: { to: 'customer@example.com', subject: 'Your order has shipped', body: emailContent } }); if (decision.approved) { await sendEmail(decision.payload); } else if (decision.escalated) { console.log(`Pending review: ${decision.reviewId}`); } else { console.log(`Blocked: ${decision.reason}`); }
TypeScript Support
Full TypeScript support with complete type definitions:
types.ts
import { Attest, ValidationDecision, ValidateParams } from '@attest/sdk'; interface EmailPayload { to: string; subject: string; body: string; } const params: ValidateParams<EmailPayload> = { action: 'send_email', payload: { to: 'user@example.com', subject: 'Hello', body: '...' } }; const decision: ValidationDecision<EmailPayload> = await client.validate(params);
Configuration
| Option | Type | Default | Description |
|---|---|---|---|
apiKey | string | Required | Your Attest API key |
timeout | number | 30000 | Request timeout in ms |
maxRetries | number | 3 | Max retry attempts |
baseUrl | string | api.attest.ai | API base URL |
OpenAI Integration
Wrap OpenAI function calls with validation:
openai_example.ts
import { Attest } from '@attest/sdk'; import OpenAI from 'openai'; const attest = new Attest({ apiKey: 'sk_live_...' }); const openai = new OpenAI(); // Wrap tool execution with validation async function executeTool(name: string, args: any) { const decision = await attest.validate({ action: name, payload: args }); if (!decision.approved) { throw new Error(decision.reason); } return tools[name](decision.payload); }
Error Handling
errors.ts
import { Attest, AttestError, RateLimitError, AuthenticationError } from '@attest/sdk'; try { const decision = await client.validate({...}); } catch (error) { if (error instanceof RateLimitError) { console.log(`Retry after ${error.retryAfter}s`); } else if (error instanceof AuthenticationError) { console.log('Invalid API key'); } else if (error instanceof AttestError) { console.log(`Attest error: ${error.message}`); } }
Requirements
- Node.js 18+
- TypeScript 4.7+ (optional)
GitHub: github.com/attest-ai/attest-node