The tRPC server library
@trpc/server is the core server-side library for tRPC (TypeScript Remote Procedure Call), designed to build end-to-end typesafe APIs without schemas, code generation, or manual type declarations. It provides router and procedure builders that leverage TypeScript's inference to share types between server and client automatically, eliminating the need for REST contracts or GraphQL schemas in monorepo or shared-package setups.
The package centers around initTRPC, which creates routers defining queries and mutations as composable procedures. Input validation integrates seamlessly with Zod or other validators, while middleware enables cross-cutting concerns like authentication and logging. Types flow from server definitions to clients through exported router types, ensuring compile-time safety and IDE autocompletion without runtime overhead.
With over 2 million weekly downloads, @trpc/server has become the standard for TypeScript-first full-stack applications, particularly in Next.js, Remix, and Node.js ecosystems. It pairs with HTTP adapters for Express, standalone servers, or serverless functions, offering a lightweight alternative to GraphQL's complexity while maintaining the flexibility developers need for modern API architectures.
The library excels in teams that control both frontend and backend in TypeScript, where traditional REST API contracts create maintenance burden and GraphQL adds unnecessary complexity for simple data fetching patterns. It's production-ready, battle-tested in startups and enterprises requiring rapid iteration without sacrificing type safety.
import { initTRPC } from '@trpc/server';
import { createHTTPServer } from '@trpc/server/adapters/standalone';
import { z } from 'zod';
interface Context {
userId?: string;
}
const t = initTRPC.context<Context>().create();
const publicProcedure = t.procedure;
const protectedProcedure = t.procedure.use(({ ctx, next }) => {
if (!ctx.userId) throw new Error('Unauthorized');
return next({ ctx: { ...ctx, userId: ctx.userId } });
});
const appRouter = t.router({
getUser: publicProcedure
.input(z.object({ id: z.string() }))
.query(({ input }) => {
return { id: input.id, name: 'Alice', email: 'alice@example.com' };
}),
createPost: protectedProcedure
.input(z.object({ title: z.string(), content: z.string() }))
.mutation(({ input, ctx }) => {
return { id: '123', ...input, authorId: ctx.userId };
}),
});
export type AppRouter = typeof appRouter;
const server = createHTTPServer({
router: appRouter,
createContext: ({ req }): Context => ({
userId: req.headers.authorization?.replace('Bearer ', '')
}),
});
server.listen(3000);Full-Stack Next.js Applications: Build API routes with tRPC routers that share types directly with React components using @trpc/react-query, eliminating the need for separate API type definitions or fetch wrappers. Perfect for dashboards, SaaS products, and internal tools where both layers are TypeScript.
Monorepo Microservices: Create multiple tRPC routers as independent services, then merge them into a unified API gateway with full type safety across service boundaries. Each service exports its router type for consumption by other services or frontends.
Serverless Function APIs: Deploy tRPC procedures as AWS Lambda, Vercel, or Cloudflare Workers with minimal overhead. The standalone HTTP adapter creates lightweight handlers that work with serverless constraints while maintaining type inference.
Mobile Backend for TypeScript Apps: Serve React Native or Ionic apps with typesafe APIs that generate client code automatically. Changes to procedure signatures immediately surface type errors in mobile code during development.
Internal Developer Tools: Build CLI tools or admin panels where backend procedures need to be called from multiple TypeScript consumers. Export the router type as an npm package to share across tools without duplicating validation logic.
npm install @trpc/serverpnpm add @trpc/serverbun add @trpc/server