Skip to content

SDK Overview ​

The DialogueDB TypeScript SDK provides a high-level interface for managing AI conversations. It handles authentication, retries, pagination, and state tracking so you can focus on your application logic.

Installation ​

bash
npm install dialogue-db

Requirements: Node.js 18 or higher. The SDK is designed for server-side use only - do not use it directly in browser code. See the Troubleshooting FAQ for how to set up a backend proxy.

Project Setup

The SDK uses top-level await in examples. Add "type": "module" to your package.json, or wrap code in an async function.

Quick Start ​

typescript
import { DialogueDB } from 'dialogue-db';

const db = new DialogueDB({ apiKey: process.env.DIALOGUE_DB_API_KEY });

// Create a dialogue with an initial message
const dialogue = await db.createDialogue({
  messages: [{ role: 'user', content: 'Hello!' }]
});

// Add a response (saved immediately)
await dialogue.saveMessage({
  role: 'assistant',
  content: 'Hi there!'
});

// Update conversation state (saved on .save())
dialogue.state = { topic: 'greeting' };
await dialogue.save();

Methods ​

MethodDescriptionReference
createDialogue(input)Create a new dialogueDialogues
getDialogue(id, opts?)Get a dialogue by ID (returns null if not found)Dialogues
getOrCreateDialogue(input?)Get or create a dialogueDialogues
listDialogues(opts?)List dialogues with filtersDialogues
deleteDialogue(id, opts?)Delete a dialogueDialogues
createMemory(input)Create a memoryMemory & Search
getMemory(id, opts?)Get a memory by IDMemory & Search
listMemories(opts?)List memoriesMemory & Search
deleteMemory(id, opts?)Delete a memoryMemory & Search
searchDialogues(query, opts?)Semantic search over dialoguesMemory & Search
searchMessages(query, opts?)Semantic search over messagesMemory & Search
searchMemories(query, opts?)Semantic search over memoriesMemory & Search

Configuration ​

DialogueDB Constructor ​

The recommended way to configure the SDK - each instance carries its own settings:

typescript
import { DialogueDB } from 'dialogue-db';

const db = new DialogueDB({
  apiKey: process.env.DIALOGUE_DB_API_KEY,
  endpoint: 'https://api.dialoguedb.com', // default
  retries: 3,           // default, set to 0 to disable
  retryMinTimeout: 1000, // default, in ms
  retryMaxTimeout: 10000 // default, in ms
});

Global Configuration ​

For the Direct API pattern, configure a global singleton:

typescript
import { createConfig, setGlobalConfig } from 'dialogue-db';

// Create a new settings container (does not mutate global)
const settings = createConfig({ apiKey: process.env.DIALOGUE_DB_API_KEY });

// Or mutate the global singleton (used by `api.*` calls)
setGlobalConfig({ apiKey: process.env.DIALOGUE_DB_API_KEY });

Environment Variables ​

Set DIALOGUE_DB_API_KEY and the SDK picks it up automatically - no explicit configuration needed:

bash
export DIALOGUE_DB_API_KEY=your-api-key

Settings Reference ​

SettingTypeDefaultDescription
apiKeystringDIALOGUE_DB_API_KEY env varAPI key for authentication
endpointstringhttps://api.dialoguedb.comAPI base URL
retriesnumber3Max retry attempts for transient errors (0 to disable)
retryMinTimeoutnumber1000Minimum retry delay in milliseconds
retryMaxTimeoutnumber10000Maximum retry delay in milliseconds

Usage Patterns ​

Class Pattern ​

The DialogueDB class returns Dialogue, Message, and Memory instances with methods, state tracking, and pagination:

typescript
import { DialogueDB } from 'dialogue-db';

const db = new DialogueDB({ apiKey: process.env.DIALOGUE_DB_API_KEY });

const dialogue = await db.createDialogue({
  messages: [{ role: 'user', content: 'Hello' }]
});

// Class instances track dirty state
dialogue.state = { step: 1 };
console.log(dialogue.isDirty); // true
await dialogue.save();
console.log(dialogue.isDirty); // false

Direct API Pattern ​

The api object provides simple function calls that map 1:1 with REST endpoints and return plain data objects:

typescript
import { api, createConfig } from 'dialogue-db';

createConfig({ apiKey: process.env.DIALOGUE_DB_API_KEY });

const dialogue = await api.dialogue.create({
  messages: [{ role: 'user', content: 'Hello' }]
});

const messages = await api.messages.list({ dialogueId: dialogue.id });

Use the class pattern when you need stateful conversation management. Use the direct API when you want straightforward CRUD operations.

Error Handling ​

All SDK errors are instances of DialogueDBError:

typescript
import { DialogueDB, DialogueDBError } from 'dialogue-db';

try {
  // Note: getDialogue() returns null for not-found - it doesn't throw.
  // Methods that modify data (saveMessage, end, delete) throw on errors.
  const dialogue = await db.createDialogue({
    messages: [{ role: 'user', content: 'Hello' }]
  });
  await dialogue.saveMessage({ role: 'user', content: '' }); // empty content → error
} catch (error) {
  if (error instanceof DialogueDBError) {
    console.log(error.message);    // Human-readable description
    console.log(error.code);       // e.g., 'MESSAGE_CONTENT_EMPTY'
    console.log(error.type);       // e.g., 'validation_error'
    console.log(error.statusCode); // e.g., 400
    console.log(error.requestId);  // For support debugging
    console.log(error.details);    // Field-level validation errors
    console.log(error.retryable);  // true for 429, 5xx, network errors
  }
}

Error Properties ​

PropertyTypeDescription
messagestringHuman-readable error description
codestringMachine-readable error code
typeErrorTypeCategory: validation_error, authentication_error, authorization_error, not_found, conflict, rate_limit_exceeded, server_error, service_unavailable
statusCodenumberHTTP status code (0 for network errors)
requestIdstring?Request ID from API response
detailsErrorDetail[]?Field-level validation errors
retryablebooleantrue for network errors, 429, and 5xx

Automatic Retries ​

The SDK automatically retries transient errors (network failures, 429 rate limits, 5xx server errors) with exponential backoff. Configure via the retries, retryMinTimeout, and retryMaxTimeout settings.

Exported Types ​

typescript
// Classes
import { DialogueDB, Dialogue, Message, Memory, DialogueDBError } from 'dialogue-db';

// Types (use `import type`)
import type {
  IDialogue,
  IMessage,
  IMemory,
  CreateDialogueInput,
  UpdateDialogueInput,
  GetDialogueInput,
  CreateMessageInput,
  CreateMemoryInput,
  ListDialogueFilters,
  ListMessageFilters,
  ListMemoriesFilters,
  ListResponse,
  MessageContent,
  SearchOptions,
  SearchFilterOptions,
  Settings,
  ErrorType
} from 'dialogue-db';

Next Steps ​