TypeScript SDK
The official TypeScript/JavaScript client for Alveare. Fully typed, tree-shakeable, and zero dependencies.
Installation
Works with Node.js 18+, Deno, and Bun.
bash
npm install @alveare-ai/sdk
Quick start
typescript
import Alveare from '@alveare-ai/sdk';
const client = new Alveare({ apiKey: 'alv_live_abc123...' });
const response = await client.infer({
specialist: 'summarise',
prompt: 'Quarterly revenue increased 23% year-over-year...',
maxTokens: 256,
});
console.log(response.result);
console.log(`Tokens: ${response.tokensUsed}`);
console.log(`Latency: ${response.latencyMs}ms`);
Client initialization
| Option | Env variable | Default |
|---|---|---|
| apiKey | ALVEARE_API_KEY | None (required) |
| baseUrl | ALVEARE_BASE_URL | https://api.alveare.ai |
| timeout | -- | 30000 ms |
| maxRetries | -- | 3 |
typescript
import Alveare from '@alveare-ai/sdk';
// Reads ALVEARE_API_KEY from process.env automatically
const client = new Alveare();
// Or configure everything explicitly
const client = new Alveare({
apiKey: process.env.MY_ALVEARE_KEY,
baseUrl: 'https://api.alveare.ai',
timeout: 60000,
maxRetries: 5,
});
client.infer()
Call the native Alveare inference endpoint.
typescript
// Classification
const result = await client.infer({
specialist: 'classify',
prompt: 'I need to cancel my subscription immediately',
temperature: 0.3,
});
console.log(result.result); // "cancellation_request"
// Extraction
const extracted = await client.infer({
specialist: 'extract',
prompt: `Extract contact info: Hi, I'm Jane Smith
from Acme Corp. Reach me at jane@acme.com or 555-0123.`,
maxTokens: 256,
});
console.log(extracted.result);
// {"name":"Jane Smith","company":"Acme Corp","email":"jane@acme.com","phone":"555-0123"}
client.chat.completions.create()
OpenAI-compatible chat completions.
typescript
const response = await client.chat.completions.create({
model: 'alveare-chat',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'What is a cognitive hive?' },
],
maxTokens: 512,
temperature: 0.7,
});
console.log(response.choices[0].message.content);
console.log(`Usage: ${response.usage.totalTokens} tokens`);
client.models.list()
typescript
const models = await client.models.list();
for (const model of models.data) {
console.log(`${model.id} — ${model.ownedBy}`);
}
client.usage()
typescript
const usage = await client.usage();
console.log(`Requests: ${usage.requestsUsed} / ${usage.requestsLimit}`);
console.log(`Tokens: ${usage.tokensUsed}`);
for (const [name, count] of Object.entries(usage.bySpecialist)) {
console.log(` ${name}: ${count}`);
}
Error handling
typescript
import Alveare, {
AlveareError,
AuthenticationError,
RateLimitError,
} from '@alveare-ai/sdk';
const client = new Alveare();
try {
const result = await client.infer({
specialist: 'summarise',
prompt: 'Some long text...',
});
} catch (err) {
if (err instanceof AuthenticationError) {
console.error('Check your API key:', err.message);
} else if (err instanceof RateLimitError) {
console.error(`Rate limited. Retry after ${err.retryAfter}s`);
} else if (err instanceof AlveareError) {
console.error(`API error: ${err.message} (${err.code})`);
}
}
TypeScript types
The SDK exports full type definitions for all request and response objects. Key types:
typescript
import type {
InferRequest, // { specialist, prompt, maxTokens?, temperature? }
InferResponse, // { id, specialist, result, tokensUsed, latencyMs }
ChatCompletionRequest, // { model, messages, maxTokens?, temperature?, stream? }
ChatCompletionResponse, // { id, object, created, model, choices[], usage }
ChatMessage, // { role: 'system'|'user'|'assistant', content: string }
Model, // { id, object, ownedBy }
Usage, // { requestsUsed, requestsLimit, tokensUsed, ... }
Specialist, // 'classify'|'summarise'|'extract'|'qa'|'chat'|'code'
} from '@alveare-ai/sdk';
// All methods are fully typed — your editor provides
// autocomplete and type checking out of the box.
const req: InferRequest = {
specialist: 'classify', // autocomplete suggests valid specialists
prompt: 'Hello world',
maxTokens: 64,
};
The SDK ships with source maps and JSDoc annotations. Hover over any method in VS Code or WebStorm to see parameter docs inline.