Express.js chat API with persistent memory powered by RetainDB and the Vercel AI SDK.
Drop-in backend for any frontend using useChat — memories survive restarts and new sessions automatically.
- Express.js — HTTP server
- Vercel AI SDK —
streamText+ data stream protocol - OpenAI
gpt-4o-mini— chat completions - RetainDB — persistent memory via
withRetainDBadapter
# 1. Clone
git clone https://github.com/Alinxus/retaindb-express-starter
cd retaindb-express-starter
# 2. Install
npm install
# 3. Configure
cp .env.example .env
# Edit .env with your keys
# 4. Run (dev, with hot reload)
npm run devCompatible with Vercel AI SDK's useChat hook on the frontend.
curl -X POST http://localhost:3001/api/chat \
-H "Content-Type: application/json" \
-d '{
"userId": "alice",
"messages": [{"role": "user", "content": "My favourite language is TypeScript"}]
}'Response streams as Vercel AI data stream protocol (text/plain + X-Vercel-AI-Data-Stream: v1).
The withRetainDB adapter wraps the OpenAI model bidirectionally:
POST /api/chat
│
├─► withRetainDB intercepts doStream
│ ├─► Retrieves relevant memories from RetainDB
│ └─► Injects them into the system prompt
│
├─► gpt-4o-mini streams reply
│
└─► withRetainDB fires background remember() after streaming
└─► Stores the user turn in RetainDB for future sessions
Use useChat from ai/react with your Express server URL:
import { useChat } from 'ai/react';
export function Chat() {
const { messages, input, handleSubmit, handleInputChange } = useChat({
api: 'http://localhost:3001/api/chat',
body: { userId: 'alice' },
});
// ...
}| Variable | Description |
|---|---|
RETAINDB_API_KEY |
Get free at retaindb.com |
OPENAI_API_KEY |
Get at platform.openai.com |
PORT |
Server port (default 3001) |
- retaindb-nextjs-starter — Next.js + Vercel AI SDK (full-stack)
- retaindb-python-starter — FastAPI + OpenAI
- retaindb-langchain-starter — Python LangChain + LangGraph