Guides

Next.js

The cleanest Next.js integration is a server-owned submission endpoint. Your page or component collects form input, while a route handler or server action forwards the payload to 1DB with the API key.

Using Claude Code or another agent?

Run npx @1db/cli setup once in your terminal, then ask your agent to “set up 1DB for acme.com”. It adds the website and writes DB_API_ENDPOINT, DB_API_KEY and DB_FORM_SLUG, the same variables this guide uses. See AI agents, MCP and CLI.

Route handler

app/api/lead/route.ts
export async function POST(request: Request) {
  const fields = await request.json(); // { name, email, message, ... } flat

  const response = await fetch(`${process.env.DB_API_ENDPOINT}/v1/leads`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "X-API-Key": process.env.DB_API_KEY!,
      "X-1DB-Client-IP": request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() ?? "",
      "X-1DB-Client-UA": request.headers.get("user-agent") ?? "",
    },
    body: JSON.stringify({ ...fields, formSlug: process.env.DB_FORM_SLUG }), // e.g. acme-contact
  });

  // { success, leadId, duplicate } or { error, code, hint }: error is a readable string
  return Response.json(await response.json(), { status: response.status });
}

Server action

app/actions.ts
"use server";

export async function submitLead(formData: FormData) {
  const payload = Object.fromEntries(formData.entries());

  const response = await fetch(`${process.env.DB_API_ENDPOINT}/v1/leads`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "X-API-Key": process.env.DB_API_KEY!,
    },
    body: JSON.stringify({ ...payload, formSlug: process.env.DB_FORM_SLUG }),
  });

  const body = await response.json();
  if (!response.ok) {
    return { ok: false, error: body.error as string, fields: body.fields };
  }
  return { ok: true, leadId: body.leadId as string };
}

Deployment checklist

  • Set DB_API_ENDPOINT, DB_API_KEY and DB_FORM_SLUG in the host's production env vars, then redeploy.
  • Posting from a route handler or server action keeps them unprefixed. Only if the browser posts directly do they need NEXT_PUBLIC_; the ingest key can only create leads, so that is safe.
  • Show body.error (a string) to the visitor, and body.fields next to each bad field. Codes are in the API reference.
  • Submit the live form once after deploying, and check the lead arrived.

React guide

Pair this route handler pattern with a typed React form component.

Webhooks guide

Trigger downstream automations after a lead is accepted.