Tutorial
Build a small planet API over the Fetch adapter. The runnable form of this walkthrough is examples/hello. The snippets match the root README happy path and are type-checked by npm run check:snippets.
Install
Section titled “Install”npm install @ts-pf/contract @ts-pf/server @ts-pf/server-http @ts-pf/client @ts-pf/client-http zodSchemas are any Standard Schema library (Zod, Valibot, ArkType) or TypeBox; zod here is one choice. Node.js 22+. TypeScript needs "moduleResolution": "node16", "nodenext", or "bundler".
1. Define a contract
Section titled “1. Define a contract”The contract is the one file both sides import. No HTTP, no handlers.
import { procedure, router } from '@ts-pf/contract'import { z } from 'zod'
export const contract = router({ planet: { find: procedure .input(z.object({ id: z.number() })) .output(z.object({ id: z.number(), name: z.string() })) .errors({ NOT_FOUND: { status: 404, data: z.object({ id: z.number() }) } }), },}).input() once, .output() optional (unknown if omitted), .errors() declares typed failures the handler throws and the client narrows on.
2. Implement it
Section titled “2. Implement it”createImplementer(contract) mirrors the contract keys. impl.router() is a type error if a procedure is missing, extra, or under the wrong key. Name the implemented router app.
import { createImplementer } from '@ts-pf/server'import { contract } from './contract.js'
export type AppCtx = { db: Db }
const impl = createImplementer(contract).$context<AppCtx>()export const app = impl.router({ planet: { find: impl.planet.find.handler(async ({ input, context, errors }) => { const row = await context.db.planets.get(input.id) if (!row) throw errors.NOT_FOUND({ id: input.id }) return row }), },})$context<C>() is the context the adapter supplies. A middleware that calls next({ context: { user } }) adds a typed user for handlers built after it (impl.use(requireUser).planet.create.handler(...)).
3. Serve it
Section titled “3. Serve it”FetchHandler speaks Fetch Request / Response only. It infers AppCtx from app.
import { FetchHandler } from '@ts-pf/server-http'import { app } from './server.js'
export const handler = new FetchHandler(app)
export async function rpc(request: Request): Promise<Response> { const result = await handler.handle(request, { prefix: '/rpc', context: { db }, }) if (!result.matched) return new Response('Not Found', { status: 404 }) return result.response}Any runtime that hands you a Request can host that function. ts-pf ships no Node IncomingMessage adapter. Serving notes and framework snippets: Serving FetchHandler.
4. Call it
Section titled “4. Call it”The client imports the contract type, never @ts-pf/server.
import { createClient } from '@ts-pf/client'import { FetchLink } from '@ts-pf/client-http'import { contract } from './contract.js'
const client = createClient<typeof contract>(new FetchLink({ url: '/rpc' }))await client.planet.find({ id: 1 })asResult(client.planet.find({ id: 1 })) turns a thrown PFError into { ok, error }; error.code === 'NOT_FOUND' narrows error.data.
What next
Section titled “What next”- Getting started — install, middleware, codecs, plugins, and every package.
- Concepts — contract vs implementer vs app, context, links, interceptors, validation defaults.
- Errors —
PFError, declared errors,asResult,statusper transport. - Testing — in-process
fetchstub orcreateLocalClient. - @ts-pf/contract and the other package READMEs — each one is that package’s API page.
examples/— smoke-tested apps;hellocan also listen withnpm run dev.