Skip to content

How to understand a tRPC codebase

tRPC lets a TypeScript client call server functions with shared types and no schema files. The server defines procedures grouped into routers, exports the router's type, and the client imports that type, so following a call from the UI to the server is mostly following names.

How to tell it's tRPC

  • @trpc/server and @trpc/client (often @trpc/react-query or @trpc/tanstack-react-query)
  • initTRPC.create() and procedures built with .input(...).query(...) or .mutation(...)
  • An exported type AppRouter = typeof appRouter

Where things usually live

server/trpc.ts
initTRPC, base procedures, middleware such as protectedProcedure.
server/context.ts
createContext: user session, database client per request.
server/routers/
Feature routers merged into appRouter.
server/routers/_app.ts
The root router and the exported AppRouter type.
pages/api/trpc/[trpc].ts or app/api/trpc/[trpc]/route.ts
The HTTP adapter that serves all procedures.
utils/trpc.ts
Client setup and links (batching, auth headers).

Conventions, not rules. Real projects rename and move things, so confirm against the repository.

Where to start reading

  1. The root router, to list every procedure
  2. trpc.ts, for middleware that protects procedures
  3. createContext, for what every procedure can access

How a request moves through the code

  1. 1A component calls trpc.post.byId.useQuery({ id }) or the equivalent.
  2. 2The client link batches the call and sends it to the tRPC HTTP endpoint.
  3. 3The adapter creates the context, including the session.
  4. 4Middleware runs (for example, rejecting unauthenticated calls), then input is validated, often with Zod.
  5. 5The procedure resolver runs and returns data with types the client already knows.

Questions to ask about the repository

  • Which procedures are public and which require a session?
  • What goes into the context for every request?
  • Trace the createPost mutation from the form to the database.

What trips people up

  • The URL of each call is the procedure path, such as /api/trpc/post.byId; search for that string to find it.
  • Procedure names are nested router keys, so renaming a router changes every client call.
  • Major versions changed client integration; check whether the app uses the v10 or v11 style.