tRPC lets you build fully type-safe APIs without schemas or code generation. Define a function on the server — call it on the client with full autocompletion.
What You Get for Free
- End-to-end type safety — no codegen needed
- Auto-completion — IDE knows all your API routes
- Subscriptions — real-time with WebSocket
- Batching — automatic request batching
- Framework agnostic — Next.js, Express, Fastify
Server
import { initTRPC } from '@trpc/server';
import { z } from 'zod';
const t = initTRPC.create();
const appRouter = t.router({
getUser: t.procedure
.input(z.object({ id: z.string() }))
.query(({ input }) => ({ id: input.id, name: 'Alice' })),
});
Client
const user = await trpc.getUser.query({ id: '1' });
// user is fully typed: { id: string, name: string }
tRPC vs REST vs GraphQL
| Feature | tRPC | REST | GraphQL |
|---|---|---|---|
| Type safety | Full | Manual | Codegen |
| Boilerplate | Minimal | High | Medium |
Need TypeScript API help? Check my work on GitHub or email spinov001@gmail.com for consulting.
Top comments (0)