TypeScript SDK Quickstart
Get started with Memori in under 3 minutes. Since Memori BYODB is open source, you bring your own database — and for this quickstart, we'll use SQLite so there is nothing extra to install beyond the better-sqlite3 driver.
Want a zero-setup option? Try Memori Cloud at app.memorilabs.ai.
In this example we'll use Memori with OpenAI and SQLite. Check out the LLM providers & frameworks and database guides for other integrations.
Prerequisites
- Node.js 20 or higher
- An OpenAI API key
Step 1: Install Libraries
Install Memori, the OpenAI SDK, and the SQLite driver:
npm install @memorilabs/memori openai better-sqlite3
npm install --save-dev @types/better-sqlite3
Step 2: Set Environment Variables
Set your OpenAI API key as an environment variable:
export OPENAI_API_KEY="your-openai-api-key"
Step 3: Run Your First Memori Application
Create a new file quickstart.ts and add the following code:
Setup & Configuration
Import libraries, open a SQLite database, and initialize Memori with your OpenAI client.
connaccepts a factory function that returns the database connection (abetter-sqlite3Databaseinstance,pgPool, ormysql2Pool)llm.register()wraps your LLM client for automatic memory captureattribution()links memories to a specific user and processawait mem.config.storage.build()creates the Memori schema tables in your database
import 'dotenv/config';
import Database from 'better-sqlite3';
import OpenAI from 'openai';
import { Memori } from '@memorilabs/memori';
const db = new Database('memori.db');
const client = new OpenAI();
const mem = new Memori({ conn: () => db }).llm.register(client);
mem.attribution('user_123', 'test-ai-agent');
if (!mem.config.storage) {
throw new Error('Storage not initialized');
}
await mem.config.storage.build();
First Conversation
Tell the LLM a fact about yourself. Memori automatically captures the conversation and processes it through Advanced Augmentation.
Since augmentation runs asynchronously, call mem.augmentation.wait() in short-lived scripts to ensure memories are fully processed before continuing.
const response1 = await client.chat.completions.create({
model: 'gpt-4.1-mini',
messages: [{ role: 'user', content: 'My favorite color is blue.' }],
});
console.log(response1.choices[0].message.content + '\n');
// Wait for background augmentation to finish
await mem.augmentation.wait();
Memory Recall
Create a completely new client and Memori instance — no prior context carried over. When you ask the LLM what it remembers, Memori automatically injects the relevant facts via semantic search.
The second response should correctly recall your favorite color, proving memory persistence works across sessions.
const client2 = new OpenAI();
const db2 = new Database('memori.db');
const mem2 = new Memori({ conn: () => db2 }).llm.register(client2);
mem2.attribution('user_123', 'test-ai-agent');
const response2 = await client2.chat.completions.create({
model: 'gpt-4.1-mini',
messages: [{ role: 'user', content: "What's my favorite color?" }],
});
console.log(response2.choices[0].message.content + '\n');
Full Quickstart File
Here's everything together as a complete runnable file:
import 'dotenv/config';
import Database from 'better-sqlite3';
import OpenAI from 'openai';
import { Memori } from '@memorilabs/memori';
// --- Step 1: Teach the AI ---
const db = new Database('memori.db');
const client = new OpenAI();
const mem = new Memori({ conn: () => db }).llm.register(client);
mem.attribution('user_123', 'test-ai-agent');
if (!mem.config.storage) {
throw new Error('Storage not initialized');
}
await mem.config.storage.build();
const response1 = await client.chat.completions.create({
model: 'gpt-4.1-mini',
messages: [{ role: 'user', content: 'My favorite color is blue.' }],
});
console.log('AI:', response1.choices[0].message.content + '\n');
await mem.augmentation.wait();
// --- Step 2: Test Recall (fresh instance, no prior context) ---
const db2 = new Database('memori.db');
const client2 = new OpenAI();
const mem2 = new Memori({ conn: () => db2 }).llm.register(client2);
mem2.attribution('user_123', 'test-ai-agent');
const response2 = await client2.chat.completions.create({
model: 'gpt-4.1-mini',
messages: [{ role: 'user', content: "What's my favorite color?" }],
});
console.log('AI:', response2.choices[0].message.content + '\n');
db.close();
db2.close();
Step 4: Run the Application
Execute your TypeScript file:
npx tsx quickstart.ts
You should see the AI respond to both questions, with the second response correctly recalling that your favorite color is blue!
Step 5: Inspect Your Memories
Since you own the database, you can inspect what Memori stored directly:
sqlite3 memori.db "SELECT * FROM memori_conversation_message;"