> ## Documentation Index
> Fetch the complete documentation index at: https://docs.memvid.com/llms.txt
> Use this file to discover all available pages before exploring further.

# v0

> Production v0 integration for Next.js apps using Memvid REST API

Use Memvid in v0 projects by calling Memvid from Next.js route handlers or server actions.

<Info>
  **API-based integration.** This guide uses `https://api.memvid.com` directly (no SDK and no local `.mv2` file).
</Info>

## Prerequisites

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

## Environment Variables

Add these to your environment:

```bash theme={null}
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`)

```typescript theme={null}
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`)

```typescript theme={null}
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`)

```typescript theme={null}
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.

## Recommended Architecture

* 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`:

```json theme={null}
{ "query": "What do we know about onboarding?" }
```

2. `POST /api/memvid/ask`:

```json theme={null}
{ "question": "Summarize onboarding policy." }
```

3. Verify both routes return JSON with non-empty content.

## Related Docs

* [API Integration Patterns](/frameworks/api-integration-patterns)
* [REST API Reference](/api-reference/rest-api)
* [API Overview](/api-reference/index)
