tRPC
End-to-end type-safe RPC for TypeScript with shared routers, input validation, client inference, and batched HTTP transport.
Official Documentation ↗tRPC 11.x; the v10 client and router APIs differ. Check the official docs for the latest API surface.Installation
npm install @trpc/server @trpc/client zodpnpm add @trpc/server @trpc/client zodyarn add @trpc/server @trpc/client zodMethods
No methods match this filter.
tRPC shares TypeScript types between a server router and its clients without code generation. Procedures define inputs, outputs, and handlers; the client infers the types from the router. The trade-off is coupling: tRPC is best when both ends are TypeScript in the same repository.
Validate at the boundary
TypeScript types disappear at runtime. Every procedure should validate its input with a schema such as Zod. The inferred type is a developer convenience; the schema is the actual contract.
Keep procedures thin
A procedure should parse input, call a service, and map the result. Business logic belongs in a service layer that can be tested without the RPC context. Use middleware for authentication and logging, and throw TRPCError with safe messages.
Deploy types with the server
The client imports the router type, so a breaking change is a compile error in the same repository. For separately deployed TypeScript clients, version the API and treat the router type as a contract. tRPC is not a good fit for unknown clients in other languages.
FAQ
- Is tRPC a replacement for REST or GraphQL?
- It is an RPC layer optimized for TypeScript-to-TypeScript applications. It is a poor fit for public APIs or clients in other languages that need a stable, language-neutral contract.
- Does tRPC validate input at runtime?
- It runs the input schema on each request. TypeScript types alone are erased at runtime; use Zod or another schema for real validation.
- How should errors be handled?
- Throw TRPCError with a code and a safe message. Map unknown errors to INTERNAL_SERVER_ERROR, log the details server-side, and avoid leaking stack traces to clients.
Report a problem with this API docOpens a pre-filled issue on GitHub.