Integrations

Browse docs

Integrations

Tap to expand
IntegrationsUpdated 2026-05-25

Next.js Integration

Use RetainDB from a server-side Next.js route.

Applies to: Next.js

Use RetainDB from server routes, server actions, or background jobs. Do not call it directly from client components.

API route example

ts
import OpenAI from "openai";
import { RetainDB } from "@retaindb/sdk";

const openai = new OpenAI();
const db = new RetainDB({
  apiKey: process.env.RETAINDB_API_KEY,
  project: "production"
});

export async function POST(req: Request) {
  const { userId, message } = await req.json();
  const user = db.user(userId);
  const { context } = await user.getContext(message);

  const response = await openai.chat.completions.create({
    model: "gpt-4o-mini",
    messages: [
      { role: "system", content: `User memory:\n${context}` },
      { role: "user", content: message }
    ]
  });

  await user.remember(message);

  return Response.json({
    text: response.choices[0]?.message?.content ?? ""
  });
}

Environment

bash
RETAINDB_API_KEY="rdb_..."
OPENAI_API_KEY="sk_..."

Next: OpenAI-compatible integration.

Was this page helpful?

Your feedback helps us prioritize docs improvements weekly.