Skip to main content
2026-08-28//6 MIN READ//BACKEND // ARCHITECTURE

CONTRACT-FIRST API WITH T RPC AND SHARED ZOD SCHEMAS

One source of truth for backend and frontend: schemas package, AppRouter types, and runtime validation at the data boundary.

[ ARTICLE_COVER_SIGNAL ]

Contract-First API

A tRPC contract is not a document — it is code that both applications import.

The layers

  • packages/schemas — zod schemas and z.infer types. The single source of truth.
  • packages/api — tRPC routers built on top of those schemas.
  • Frontend imports only type AppRouter and gets end-to-end inference.

Validation boundaries

  1. Input validation — zod schemas on every procedure.
  2. Data validation — db.json is parsed at startup, the server fails fast on invalid data.
  3. Transport — queries use GET, mutations use POST, superjson for serialization.

If the schema does not run anywhere, it is documentation, not a contract.

Responses without envelopes

articles.list returns Article[] directly. tRPC already namespaces procedures by path, so { items: [...] } wrappers only add noise on the client.

END OF SIGNALSHARE OR SAVE THIS KNOWLEDGE

MORE ARTICLES

UI DESIGN SYSTEM & MARKDOWN SYNTAX DEMO

Complete showcase of all typography, lists, code blocks, tables, and blockquotes supported by the markdown parser.

MONOREPO INFRASTRUCTURE WITH PNPM WORKSPACES

MONOREPO INFRASTRUCTURE WITH PNPM WORKSPACES

How to split Next.js frontend and Node.js backend into a pnpm monorepo with isolated Dockerfiles.

STATIC EXPORT DEPLOYMENT WITH DOCKER AND DOKPLOY

STATIC EXPORT DEPLOYMENT WITH DOCKER AND DOKPLOY

Shipping a Next.js static export as a static-web-server Docker image with build-args baked into the bundle.

ASK ABOUT MY WORK.

Ask about product fit, engineering decisions, or the stack behind a specific case — personal AI got you covered.

LET'S WORK TOGETHER

A new product, an unfinished system or something that needs fixing? Tell me about your project!

YOU CAN ALSO WRITE AN EMAIL ON LETNULL19A@GMAIL.COM