> ## 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.

# Replit

> Production Replit integration with Memvid REST API and reusable client

Use Memvid in Replit by calling the Memvid cloud API from your server code.

<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 Replit project (Node.js recommended)

## Configure Secrets

In Replit, add secrets:

* `MEMVID_API_KEY=mv2_YOUR_API_KEY`
* `MEMVID_API_BASE=https://api.memvid.com` (optional)

## Reusable API Client (`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}`);
    }
    if (res.status === 204) return null;
    return res.json();
  }

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

## Replit Server Example (`server.ts`)

```typescript theme={null}
import express from "express";
import { memvid } from "./memvid";

const app = express();
app.use(express.json());

app.post("/api/memories/bootstrap", async (_req, res) => {
  try {
    const memory = await memvid("/v1/memories", {
      method: "POST",
      body: JSON.stringify({
        name: "Replit Demo Memory",
        description: "Knowledge for Replit prototype",
      }),
    });

    await memvid(`/v1/memories/${memory.id}/documents`, {
      method: "POST",
      body: JSON.stringify({
        documents: [
          {
            title: "Runbook",
            text: "Restart worker first, then clear queue backlog.",
            tags: ["ops"],
          },
        ],
      }),
    });

    return res.json({ memoryId: memory.id, ok: true });
  } catch (error) {
    return res.status(500).json({ ok: false, error: String(error) });
  }
});

app.post("/api/memories/:memoryId/find", async (req, res) => {
  try {
    const data = await memvid(`/v1/memories/${req.params.memoryId}/find`, {
      method: "POST",
      body: JSON.stringify({
        query: req.body.query,
        topK: req.body.topK ?? 5,
      }),
    });
    return res.json(data);
  } catch (error) {
    return res.status(500).json({ error: String(error) });
  }
});

app.post("/api/memories/:memoryId/ask", async (req, res) => {
  try {
    const data = await memvid(`/v1/memories/${req.params.memoryId}/ask`, {
      method: "POST",
      body: JSON.stringify({
        question: req.body.question,
        options: { includeSources: true, model: "gpt-4o-mini" },
      }),
    });
    return res.json(data);
  } catch (error) {
    return res.status(500).json({ error: String(error) });
  }
});

app.listen(3000, () => {
  console.log("Server listening on :3000");
});
```

## Smoke Test

1. Call `POST /api/memories/bootstrap` and save `memoryId`.
2. Call `POST /api/memories/:memoryId/find`:

```json theme={null}
{ "query": "How do we recover worker issues?" }
```

3. Call `POST /api/memories/:memoryId/ask`:

```json theme={null}
{ "question": "What are the first two recovery steps?" }
```

## Production Tips

* Keep Memvid calls on the server side.
* Expose your own app endpoint for client requests.
* Reuse memory IDs per workspace, team, or project.
* Add retry/backoff around `429` and `5xx`.

## Related Docs

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