SDKs
JavaScript SDK
Official Sazabi SDK for JavaScript and TypeScript.
The Sazabi JavaScript SDK provides a type-safe client for the Sazabi API. It works in Node.js, Deno, Bun, and edge runtimes like Cloudflare Workers and Vercel Edge Functions.
Installation
npm install @sazabi/sdkpnpm add @sazabi/sdkyarn add @sazabi/sdkbun add @sazabi/sdkQuick start
import { Sazabi } from "@sazabi/sdk";
const client = new Sazabi({
secretKey: process.env.SAZABI_SECRET_KEY,
});
// List threads
const threads = await client.threads.list();
console.log(threads);
// Create a thread and send a message
const thread = await client.threads.create({
title: "Investigating API latency",
});
const message = await client.messages.create(thread.id, {
content: "Show me p99 latency for the last hour",
});
console.log(message);Configuration
Client options
const client = new Sazabi({
// Required: Your secret API key
secretKey: process.env.SAZABI_SECRET_KEY,
// Optional: Override the base URL (for testing or self-hosted)
baseUrl: "https://api.sazabi.com",
// Optional: Request timeout in milliseconds
timeout: 30000,
// Optional: Custom fetch implementation
fetch: customFetch,
});Environment variables
The SDK reads from environment variables if options are not provided:
| Variable | Description |
|---|---|
SAZABI_SECRET_KEY | Default secret key |
SAZABI_BASE_URL | Override API base URL |
Threads
List threads
// List all threads
const threads = await client.threads.list();
// With pagination
const threads = await client.threads.list({
limit: 20,
cursor: "eyJpZCI6InRocl9hYmMxMjMifQ",
});
// Filter by status
const archivedThreads = await client.threads.list({
status: "archived",
});Get a thread
const thread = await client.threads.get("thr_abc123");
console.log(thread.title);
console.log(thread.createdAt);Create a thread
const thread = await client.threads.create({
title: "Production errors investigation",
});Update a thread
const updated = await client.threads.update("thr_abc123", {
title: "Resolved: Production errors",
});Archive a thread
await client.threads.archive("thr_abc123");Messages
Send a message
// Send to existing thread
const message = await client.messages.create("thr_abc123", {
content: "What errors are happening in production?",
});
// Create thread and send message in one call
const { thread, message } = await client.messages.send({
content: "What errors are happening in production?",
});List messages
const messages = await client.messages.list("thr_abc123", {
limit: 50,
});Wait for assistant response
// Send message and wait for response
const message = await client.messages.create("thr_abc123", {
content: "Analyze the error patterns",
});
// Poll for completion
const response = await client.runs.waitForCompletion(message.runId, {
timeout: 60000, // 60 seconds
pollInterval: 2000, // Check every 2 seconds
});
console.log(response.content);Logs
Send logs
// Single log
await client.logs.send({
message: "User signed in",
level: "info",
service: "auth-service",
attributes: {
userId: "usr_123",
},
});
// Batch logs
await client.logs.sendBatch([
{ message: "Request started", level: "info" },
{ message: "Request completed", level: "info" },
]);Log ingestion uses a public key. Configure it separately:
const logClient = new Sazabi({
publicKey: process.env.SAZABI_PUBLIC_KEY,
});Error handling
The SDK throws typed errors that you can catch and handle:
import { Sazabi, SazabiError, RateLimitError, AuthenticationError } from "@sazabi/sdk";
try {
const thread = await client.threads.get("thr_invalid");
} catch (error) {
if (error instanceof AuthenticationError) {
console.error("Invalid API key");
} else if (error instanceof RateLimitError) {
console.error(`Rate limited. Retry after ${error.retryAfter} seconds`);
} else if (error instanceof SazabiError) {
console.error(`API error: ${error.code} - ${error.message}`);
} else {
throw error;
}
}Error types
| Error | Description |
|---|---|
SazabiError | Base error class for all API errors |
AuthenticationError | Invalid or missing API key |
PermissionError | Insufficient permissions |
NotFoundError | Resource not found |
ValidationError | Invalid request parameters |
RateLimitError | Rate limit exceeded |
TypeScript support
The SDK is written in TypeScript and provides full type definitions:
import type { Thread, Message, LogEntry } from "@sazabi/sdk";
// All responses are fully typed
const thread: Thread = await client.threads.get("thr_abc123");
// Type inference works with async/await
const messages = await client.messages.list(thread.id);
// messages is typed as Message[]Edge runtime support
The SDK works in edge runtimes like Cloudflare Workers and Vercel Edge Functions:
// Cloudflare Worker
export default {
async fetch(request: Request, env: Env) {
const client = new Sazabi({
secretKey: env.SAZABI_SECRET_KEY,
});
const threads = await client.threads.list();
return Response.json(threads);
},
};