Skip to main content
Use Memvid in v0 projects by calling Memvid from Next.js route handlers or server actions.
API-based integration. This guide uses https://api.memvid.com directly (no SDK and no local .mv2 file).

Prerequisites

  • A Memvid API key (mv2_...)
  • A v0-generated Next.js app

Environment Variables

Add these to your environment:
MEMVID_API_KEY=mv2_YOUR_API_KEY
MEMVID_API_BASE=https://api.memvid.com
MEMVID_MEMORY_ID=your_memory_id

Shared Server Helper (lib/memvid.ts)

const API_BASE = process.env.MEMVID_API_BASE || "https://api.memvid.com";
const API_KEY = process.env.MEMVID_API_KEY!;
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));

export async function memvid(path: string, init: RequestInit = {}, retries = 2) {
  if (!API_KEY) throw new Error("Missing MEMVID_API_KEY");

  for (let attempt = 0; attempt <= retries; attempt++) {
    const res = await fetch(`${API_BASE}${path}`, {
      ...init,
      headers: {
        Authorization: `Bearer ${API_KEY}`,
        "Content-Type": "application/json",
        ...(init.headers || {}),
      },
    });

    if (!res.ok) {
      const body = await res.text();
      const retryable = res.status === 429 || res.status >= 500;
      if (retryable && attempt < retries) {
        await sleep(250 * Math.pow(2, attempt));
        continue;
      }
      throw new Error(`Memvid API error ${res.status}: ${body}`);
    }
    return res.status === 204 ? null : res.json();
  }

  throw new Error("Memvid request retry budget exhausted");
}

Route Handler Example (app/api/memvid/ask/route.ts)

import { NextResponse } from "next/server";
import { memvid } from "@/lib/memvid";

const MEMORY_ID = process.env.MEMVID_MEMORY_ID!;

export async function POST(req: Request) {
  try {
    const { question } = await req.json();
    const data = await memvid(`/v1/memories/${MEMORY_ID}/ask`, {
      method: "POST",
      body: JSON.stringify({
        question,
        options: { includeSources: true, model: "gpt-4o-mini" },
      }),
    });
    return NextResponse.json(data);
  } catch (error) {
    return NextResponse.json(
      { error: "Memvid request failed", details: String(error) },
      { status: 500 }
    );
  }
}

Route Handler Example (app/api/memvid/find/route.ts)

import { NextResponse } from "next/server";
import { memvid } from "@/lib/memvid";

const MEMORY_ID = process.env.MEMVID_MEMORY_ID!;

export async function POST(req: Request) {
  try {
    const { query, topK = 5 } = await req.json();
    const data = await memvid(`/v1/memories/${MEMORY_ID}/find`, {
      method: "POST",
      body: JSON.stringify({ query, topK }),
    });
    return NextResponse.json(data);
  } catch (error) {
    return NextResponse.json(
      { error: "Memvid request failed", details: String(error) },
      { status: 500 }
    );
  }
}

When to Use find vs ask

  • Use find to show matching chunks/snippets in UI.
  • Use ask when you need a synthesized answer grounded in retrieved context.
  • Browser UI calls your Next.js API route.
  • Route calls Memvid with server-side credentials.
  • Response is rendered with answer + sources.

Smoke Test

  1. POST /api/memvid/find:
{ "query": "What do we know about onboarding?" }
  1. POST /api/memvid/ask:
{ "question": "Summarize onboarding policy." }
  1. Verify both routes return JSON with non-empty content.