Skip to content

Connect from Next.js

Call Lumbung only from server code: route handlers, server components and server actions. The token must never reach the browser.

Terminal window
pnpm add @lumbung/client

Make one client for the app:

lib/db.ts
import { createClient } from "@lumbung/client"
export const db = createClient({
url: process.env.LUMBUNG_URL!,
token: process.env.LUMBUNG_TOKEN!,
database: "app",
})

Use it in a route handler:

app/api/users/route.ts
import { db } from "@/lib/db"
export async function GET() {
const { rows } = await db.query("SELECT id, email, name FROM users ORDER BY id", [], { readOnly: true })
return Response.json(rows)
}

Set LUMBUNG_URL and LUMBUNG_TOKEN in .env.local (and in your host’s settings). Results are plain objects, so they serialize to server components’ props as they are; integers are numbers unless you set intMode (see the SDK reference).

For a preview deployment per pull request, point it at a branch: set branch in createClient from an environment variable. See A branch per pull request.