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

# Lovable

> Production Lovable integration with Memvid REST API backend actions

Connect Lovable-generated apps to Memvid through backend actions that call the Memvid API.

<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 Lovable project with backend/server function support

## Setup

1. Add `MEMVID_API_KEY` to your environment variables.
2. Add `MEMVID_API_BASE` as `https://api.memvid.com` (optional, but recommended).
3. Keep API calls server-side to avoid exposing your key.

## Backend API Helper (`lib/memvid.ts`)

```typescript theme={null}
const MEMVID_API_BASE = process.env.MEMVID_API_BASE || "https://api.memvid.com";
const MEMVID_API_KEY = process.env.MEMVID_API_KEY!;
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));

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

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

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

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

## Backend Action Example (`actions/askMemory.ts`)

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

export async function askMemory(memoryId: string, question: string) {
  return memvidRequest(`/v1/memories/${memoryId}/ask`, {
    method: "POST",
    body: JSON.stringify({
      question,
      options: { includeSources: true, model: "gpt-4o-mini" },
    }),
  });
}
```

## Bootstrap Action Example (`actions/bootstrapMemory.ts`)

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

export async function bootstrapMemory() {
  const memory = await memvidRequest("/v1/memories", {
    method: "POST",
    body: JSON.stringify({
      name: "Lovable App Memory",
      description: "Knowledge for generated app",
    }),
  });

  await memvidRequest(`/v1/memories/${memory.id}/documents`, {
    method: "POST",
    body: JSON.stringify({
      documents: [
        {
          title: "FAQ",
          text: "Refunds are processed within 5 business days.",
          tags: ["billing"],
        },
      ],
    }),
  });

  return memory.id;
}
```

## Recommended Flow

* Provision or select a memory for your app/project.
* Ingest docs from your app data (JSON text, files, or URLs).
* Use `find` for retrieval cards/lists in UI.
* Use `ask` for synthesized answers with sources.

## Endpoints You Will Use Most

* `POST /v1/memories`
* `POST /v1/memories/:id/documents`
* `POST /v1/memories/:id/find`
* `POST /v1/memories/:id/ask`

## Smoke Test

1. Run your bootstrap action and save `memoryId`.
2. Call your ask action with:

```json theme={null}
{
  "memoryId": "your_memory_id",
  "question": "How long do refunds take?"
}
```

3. Verify answer text and at least one source snippet are returned.

## Related Docs

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