DEV Community

Alex Spinov
Alex Spinov

Posted on

tRPC Has a Free Framework for Building End-to-End Type-Safe APIs in TypeScript

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' })),
});
Enter fullscreen mode Exit fullscreen mode

Client

const user = await trpc.getUser.query({ id: '1' });
// user is fully typed: { id: string, name: string }
Enter fullscreen mode Exit fullscreen mode

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)