In practice
I pick tRPC when the frontend and the backend share one TypeScript project and I do not want a separate API schema to maintain. It is in the stack of Gaia Lens and TPH Global, both in the Code & Pepper team. When clients use other languages, I pick GraphQL or REST.
What is tRPC?
tRPC is a library for building and consuming fully typesafe APIs with no schemas and no code generation. It rests on TypeScript type inference: the server exports the router type, and the client gets the input and output types of every procedure from it. A contract error shows up at build time, not in the browser of a visitor.
The server functions are procedures, queries and mutations, grouped into routers. To the client they look like a function call, and underneath they are plain HTTP requests. Adapters run them, among others for Next.js and Fastify.
- Contract
- Router type, no schema
- Code generation
- None
- Procedures
- Queries and mutations
- Language
- TypeScript on both sides
How I work with tRPC
I start by asking who will read this API. If only my own apps in one repository, tRPC makes the work simpler, because the contract is code, not a separate file to keep in step.
Router and procedures
I group procedures into routers and export the type of the main router for the client. The client then knows every procedure with its input and output, with no schema file and no generation step.
Validation at the input
I check a procedure input with a validator, such as Zod, because TypeScript types vanish at runtime and will not stop a bad request. The validator also gives the client the input type.
Adapter and caching
I pick the adapter to match the framework, for example Next.js. With HTTP caching I watch request batching, because personal data can end up in one batch.
tRPC or GraphQL?
Both make sure the client knows what the server returns. tRPC does it with TypeScript types, GraphQL with a schema that does not depend on a language.
| Criterion | tRPC | GraphQL |
|---|---|---|
| Where the contract lives | Router type on the server, no schema | A GraphQL schema, described separately |
| Languages on both sides | TypeScript on both server and client | Many languages, one shared schema |
| Client types | Inferred from the server, no code generation | Tools read the schema through introspection |
| Entry point | Procedures: queries and mutations | Usually one address for the whole API |
| Response shape | The procedure on the server sets it | The client picks fields in the query |
| HTTP caching | Queries are plain GET requests, headers work | The address names no object, ids are needed |
It usually comes down to team preference
The choice between tRPC and GraphQL usually comes down to what the team prefers to maintain, not to one right answer.
In newer Next.js
tRPC is really nice to work with, but in Next.js with cache components it may no longer be needed.
tRPC is in the stack of Gaia Lens and TPH Global, both in the Code & Pepper team, and GraphQL in Kuma.de and Little Birdie.
Frequently asked questions
GET requests and the standard HTTP cache headers work. Request batching, though, can mix personal data with public data, so I set headers carefully or leave them out for logged-in requests.Everything in one TypeScript project?
Tell me who will use the API and in which languages. We will decide together whether tRPC is enough or whether you need GraphQL or REST.