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/serverand@trpc/client(often@trpc/react-queryor@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 asprotectedProcedure.- 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
AppRoutertype. - 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
- The root router, to list every procedure
trpc.ts, for middleware that protects procedurescreateContext, for what every procedure can access
How a request moves through the code
- A component calls
trpc.post.byId.useQuery({ id })or the equivalent. - The client link batches the call and sends it to the tRPC HTTP endpoint.
- The adapter creates the context, including the session.
- Middleware runs (for example, rejecting unauthenticated calls), then input is validated, often with Zod.
- The 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
createPostmutation 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.