Connect Lovable-generated apps to Memvid through backend actions that call the Memvid API.
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 Lovable project with backend/server function support
Setup
- Add
MEMVID_API_KEY to your environment variables.
- Add
MEMVID_API_BASE as https://api.memvid.com (optional, but recommended).
- Keep API calls server-side to avoid exposing your key.
Backend API Helper (lib/memvid.ts)
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)
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)
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
- Run your bootstrap action and save
memoryId.
- Call your ask action with:
{
"memoryId": "your_memory_id",
"question": "How long do refunds take?"
}
- Verify answer text and at least one source snippet are returned.