Skip to content

Quick start

This walks through generating a typed API server and client SDK from an existing Postgres database. For every option, see the Configuration reference; for every command, see the CLI reference.

Terminal window
bunx postgresdk@latest init

This writes a postgresdk.config.ts with all options documented.

The only required field is connectionString:

import type { Config } from "postgresdk";
export default {
connectionString: process.env.DATABASE_URL!,
// outDir defaults to { client: "./api/client", server: "./api/server" }
} satisfies Config;
Terminal window
bunx postgresdk@latest generate # alias: gen

postgresdk introspects the schema and writes the server + client code (and a CONTRACT.md describing everything it emitted — see a real example).

import { Hono } from "hono";
import { serve } from "@hono/node-server";
import { Client } from "pg";
import { createRouter } from "./api/server/router"; // path depends on your outDir
const app = new Hono();
const pg = new Client({ connectionString: process.env.DATABASE_URL });
await pg.connect();
app.route("/", createRouter({ pg }));
serve({ fetch: app.fetch, port: 3000 });

See Server setup for database drivers, the onRequest hook, and auth.

import { SDK } from "./api/client"; // path depends on your outDir
const sdk = new SDK({ baseUrl: "http://localhost:3000" });
const user = await sdk.users.create({ name: "Alice", email: "alice@example.com" });
const { data } = await sdk.users.list({ where: { status: "active" }, include: { posts: true } });
await sdk.users.update(user.id, { name: "Alice Smith" });

See Client SDK usage for CRUD, includes, transactions, and Filtering & WHERE operators for queries.

  • Node.js ≥ 18.17 (or Bun)
  • A reachable PostgreSQL database for introspection
  • Currently generates Hono server code (see serverFramework)