Browse the docs

Build a Postgres API in TypeScript

You will build an HTTP API in TypeScript that stores items in Postgres, test it against a real database, and run it as a Docker image.

Steps 2 to 5 are the persistence quick start of @putnami/database: a table, its repository, its migration, the SQL plugin and the routes that use them. This page ships in the package, at node_modules/@putnami/database/doc/build-a-postgres-api.md, and putnami.dev publishes it.

Where the docs are

Steps

1) Create the workspace and the API project

putnami init sets up the workspace in the current directory:

mkdir shop && cd shop
putnami init api --template server

The typescript-server template creates the project in api/: src/main.ts composes the application, src/api/ holds one file per route, and test/api.test.ts tests them. putnami serve api runs it.

2) Add the database package

putnami deps add @putnami/database --projects api

3) Declare the table and its migration

Create api/src/db/items.ts:

import { Column, Key, Repository, Table, Uuid, sqlSourceInline } from '@putnami/database';

export const ItemsTable = Table('items', {
  id: Key(Uuid),
  name: Column(String),
});

export class ItemRepository extends Repository<typeof ItemsTable> {
  constructor() {
    super(ItemsTable);
  }
}

export const itemsMigrations = {
  name: 'items-migrations',
  migrationSources: () => [
    sqlSourceInline({
      namespace: 'items',
      datasource: { name: 'default', schema: 'public' },
      definitions: [
        {
          name: '20260101000000-create-items',
          sql: 'CREATE TABLE items (id UUID PRIMARY KEY, name TEXT NOT NULL);',
          down: 'DROP TABLE items;',
        },
      ],
    }),
  ],
};

A table definition creates no SQL: the migration owns the table. TypeScript declares migrations inline; it has no loader for .sql files.

4) Connect the service

Replace api/src/main.ts. With autoApply, the service applies its migrations at startup, before it listens:

import { api, application, http, logger } from '@putnami/application';
import { sql } from '@putnami/database';
import { itemsMigrations } from './db/items';

export const app = () =>
  application().use(http()).use(logger()).use(sql({ autoApply: true })).use(itemsMigrations).use(api());

The service reads its connection from DATABASE_URL, or from database.default.url in a conf/ file such as api/conf/.env.local.yaml. A binding that a deploy target or provision() injects wins over both. Add ?sslmode=disable for a server without TLS other than localhost. Once infra/requirements.json declares the database (step 6), putnami serve api creates a database for the service on the PostgreSQL server the tests use, the shared Docker server by default, and hands the service its connection, ahead of DATABASE_URL and conf/. The database is kept for the worktree: the next putnami serve finds its data again as long as that server lives, and putnami serve api --reset-databases starts from an empty one. With PUTNAMI_TEST_PG_URL set, the service uses the database of that server instead, nothing is created or dropped, and --reset-databases is refused.

5) Add the routes

Create api/src/api/items/post.ts:

import { endpoint, json, MinLength } from '@putnami/application';
import { ItemRepository } from '../../db/items';

export default endpoint()
  .body({ name: MinLength(1) })
  .handle(async (ctx) => {
    const { name } = await ctx.body();
    const item = await new ItemRepository().save({ id: crypto.randomUUID(), name });
    return json(item, { status: 201 });
  });

Create api/src/api/items/[id]/get.ts:

import { endpoint, NotFoundException, Uuid } from '@putnami/application';
import { ItemRepository } from '../../../db/items';

export default endpoint()
  .params({ id: Uuid })
  .handle(async (ctx) => {
    const item = await new ItemRepository().get({ id: ctx.params.id });
    if (!item) throw new NotFoundException(`item ${ctx.params.id}`);
    return item;
  });

Create api/src/api/items/[id]/delete.ts. A handler that returns nothing answers 204 No Content:

import { endpoint, Uuid } from '@putnami/application';
import { ItemRepository } from '../../../db/items';

export default endpoint()
  .params({ id: Uuid })
  .handle(async (ctx) => {
    await new ItemRepository().delete({ id: ctx.params.id });
  });

6) Build

putnami build --projects api

The build writes api/infra/requirements.json, which says the service needs a Postgres database, and the migration bundle under api/.gen/migration-bundle/. Commit infra/requirements.json: putnami test reads it to give the tests a database. putnami test and bun run putnami-generate write both too, so the first test run already gets its database. Build again after you add or change a migration.

7) Test against Postgres

putnami test starts a throwaway Postgres, in Docker or from a local install. provision() creates an isolated database for the suite and applies the migrations the build bundled. createTestApp({ app }) then starts the application of src/main.ts on an ephemeral port, as in production: its autoApply finds the migrations applied. Create api/test/items.test.ts:

import { afterAll, beforeAll, expect, it } from 'bun:test';
import { createTestApp, type TestApp } from '@putnami/application/testing';
import { provision } from '@putnami/database';
import { app } from '../src/main';

let db: Awaited<ReturnType<typeof provision>>;
let testApp: TestApp;

beforeAll(async () => {
  db = await provision();
  testApp = await createTestApp({ app });
});

afterAll(async () => {
  await testApp?.stop();
  await db?.cleanup();
});

it('creates, reads and deletes an item', async () => {
  const created = await testApp.fetch('/items', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ name: 'apple' }),
  });
  expect(created.status).toBe(201);
  const { id } = await created.json();

  expect((await testApp.fetch(`/items/${id}`)).status).toBe(200);
  expect((await testApp.fetch(`/items/${id}`, { method: 'DELETE' })).status).toBe(204);
  expect((await testApp.fetch(`/items/${id}`)).status).toBe(404);
});
putnami test --projects api

A passing run prints its counts and the database the suite used, such as api test~test done 1.2s 1/1 passed, database default (postgres).

Without the CLI, point DATABASE_URL at a Postgres server whose role may create databases: provision() builds its binding from it, with the datasources the project declares, when no test binding is set. bun run putnami-generate writes the migration bundle and the infra/requirements.json the build writes.

cd api && bun run putnami-generate && DATABASE_URL=postgres://postgres:postgres@localhost:5432/postgres bun test

8) Package the image

putnami package builds only the channels you turn on, and fails for a named project with none. Turn the image channel on in api/putnami.json, beside what the template wrote:

{
  "options": {
    "package": {
      "docker": true,
      "port": 8080
    }
  }
}
putnami package --projects api --docker-load --docker-tag local

The image is tagged api:c-<content hash>, and --docker-tag local adds api:local; --docker-load loads it into Docker. Add --platform linux/arm64 on Apple silicon. The image sets PORT to the package port, 3000 when none is set, and the service listens on it.

9) Run the image

Run Postgres and the service on one Docker network, and pass the connection in DATABASE_URL. Start the service once Postgres accepts connections: it connects and migrates before it listens, and stops when the database does not answer.

docker network create shop
docker run -d --name db --network shop -e POSTGRES_PASSWORD=postgres postgres:17
until docker exec db pg_isready -h 127.0.0.1 -U postgres; do sleep 1; done
docker run --rm --network shop -p 8080:8080 \
  -e DATABASE_URL='postgres://postgres:postgres@db:5432/postgres?sslmode=disable' \
  api:local

The service applies its migration at startup. In another shell:

curl -s -X POST localhost:8080/items -H 'Content-Type: application/json' -d '{"name":"apple"}'

Result

You now have a TypeScript API that stores its data in Postgres, applies its migrations, passes its tests against a real database and runs as a Docker image.