tRPC
tRPC is a TypeScript library for building end-to-end typesafe APIs without code generation or a schema definition language. It lets a frontend call backend procedures as if they were local function calls, with full type inference flowing…
Definition
tRPC is a TypeScript library for building end-to-end typesafe APIs without code generation or a schema definition language. It lets a frontend call backend procedures as if they were local function calls, with full type inference flowing automatically from server to client.
Overview
tRPC ('TypeScript RPC') addresses a common pain point in full-stack TypeScript applications: keeping API contracts in sync between frontend and backend. Traditional approaches — REST with manually maintained types, or GraphQL with a separate schema and codegen step — introduce friction and drift risk. tRPC instead relies purely on TypeScript's type inference: you define 'procedures' (queries, mutations, and subscriptions) on the server using a router, and the client imports the router's *type* (not its implementation) to get fully typed, autocompleted API calls with zero code generation and no runtime schema validation layer required by default. A typical tRPC setup defines a router with procedures composed via middleware (for auth, logging, etc.), often paired with a validation library like Zod to parse and type-check input at runtime while reusing the same schema for TypeScript inference. On the client, tRPC integrates tightly with React Query (via `@trpc/react-query`) to provide typed hooks for data fetching, caching, and mutations, and it works over HTTP, WebSockets (for subscriptions), or within the same process for full-stack frameworks. Because tRPC requires both frontend and backend to share a single TypeScript codebase (typically a monorepo), it's best suited to full-stack TypeScript projects rather than public APIs consumed by unknown third-party clients or non-TypeScript consumers — for those, REST, GraphQL, or OpenAPI-documented APIs remain more appropriate since they don't assume a shared type system. tRPC has become a popular choice in the modern TypeScript stack, often paired with Next.js, Prisma, and Zod, and is a foundational piece of stacks like the T3 Stack (Next.js, TypeScript, tRPC, Tailwind, Prisma).
Key Features
- End-to-end type safety with zero code generation — types flow via TypeScript inference
- Procedures organized into routers, composable with middleware for auth and logging
- Deep integration with React Query for typed data fetching and caching on the client
- Commonly paired with Zod for runtime input validation and shared schema inference
- Supports queries, mutations, and subscriptions (including over WebSockets)
- Requires frontend and backend to share a TypeScript codebase, typically a monorepo
- Core building block of the popular T3 Stack
- Works with any HTTP-based transport and integrates with frameworks like Next.js and Express