tRPC
TypeScript framework for building end-to-end type-safe APIs without code generation or schemas, ensuring client-server consistency.
Updated on January 8, 2026
tRPC (TypeScript Remote Procedure Call) revolutionizes API development by leveraging TypeScript's type system to create fully typed interfaces between client and server. Unlike traditional approaches requiring external schemas or code generation, tRPC automatically infers types from your backend procedures, eliminating duplication and synchronization errors. This approach radically transforms productivity for fullstack TypeScript teams by detecting API incompatibilities at compile time.
Fundamentals
- Automatic type inference from server procedures to client without additional configuration
- RPC (Remote Procedure Call) architecture enabling server function calls like local methods
- Built-in validation with Zod or other TypeScript-first validation libraries
- Native support for modern frameworks (Next.js, React, Vue, Solid) with dedicated adapters
Benefits
- Complete elimination of typing errors between client and server through automatic inference
- Exceptional DX with IDE autocomplete, safe refactoring, and instant error detection
- Zero overhead: no code generation, no extra build steps, native TypeScript types
- Optimal performance with batching support, SSE streaming, and React Server Components
- Rich ecosystem including authentication, middleware, caching, and third-party integrations
Practical Example
import { z } from 'zod';
import { router, publicProcedure } from '../trpc';
export const userRouter = router({
getById: publicProcedure
.input(z.object({ id: z.string() }))
.query(async ({ input, ctx }) => {
const user = await ctx.db.user.findUnique({
where: { id: input.id },
});
return user;
}),
create: publicProcedure
.input(z.object({
name: z.string().min(3),
email: z.string().email(),
}))
.mutation(async ({ input, ctx }) => {
return await ctx.db.user.create({
data: input,
});
}),
});import { trpc } from '../utils/trpc';
export function UserProfile({ userId }: { userId: string }) {
// Types automatically inferred from server
const { data: user, isLoading } = trpc.user.getById.useQuery({
id: userId
});
const createUser = trpc.user.create.useMutation({
onSuccess: () => {
// Automatic cache invalidation
trpc.useContext().user.getById.invalidate();
},
});
// TypeScript detects property errors
return <div>{user?.name}</div>;
}Implementation
- Install tRPC and dependencies: @trpc/server, @trpc/client, @trpc/react-query (for React)
- Create tRPC context with shared dependencies (DB, auth, logger)
- Define procedures with input validation (Zod) and business logic organized in routers
- Configure HTTP/Next.js adapter to expose API endpoints
- Initialize tRPC client with inferred types and connect to React Query
- Implement middleware for authentication, logging, and error handling
- Organize routers by business domain with merge for composition
Pro Tip
Use the createCallerFactory helper to test your procedures server-side without HTTP, and leverage transformers (superjson) to automatically serialize complex types like Date, Map, or Set. For Next.js applications, prefer App Router with Server Components to drastically reduce client bundle size.
Related Tools
- Zod - TypeScript-first schema validation for secure inputs
- TanStack Query (React Query) - Cache management and server-client synchronization
- Superjson - Advanced serialization for native JavaScript types
- Prisma - Type-safe ORM perfectly compatible with tRPC
- NextAuth.js - Integrated authentication for tRPC context
- tRPC-openapi - Automatic OpenAPI documentation generation
tRPC represents a paradigm shift for fullstack TypeScript architectures by eliminating traditional friction between frontend and backend. By drastically reducing production errors, accelerating development cycles, and improving maintainability, tRPC enables teams to focus on business value rather than API plumbing. For organizations invested in the TypeScript ecosystem, tRPC rapidly becomes an essential standard offering immediate ROI in productivity and quality.
