Skip to content
Home / Migrations / REST API (Express/Fastify)tRPC

REST API (Express/Fastify) tRPC

medium

tRPC gives end-to-end type safety between client and server with zero codegen. Best for full-stack TypeScript monorepos using Next.js or similar.

Estimated: 4-12h · 6 steps
Progress0%
Step 1: Install tRPC
npm install @trpc/server @trpc/client @trpc/react-query @tanstack/react-query
Step 2: Create router

Convert REST endpoints to tRPC procedures. GET → query, POST/PUT/DELETE → mutation.

import { initTRPC } from '@trpc/server'
const t = initTRPC.create()
export const appRouter = t.router({ hello: t.procedure.query(() => 'world') })
Step 3: Add API handler

Mount tRPC handler in your Next.js app/api/trpc/[trpc]/route.ts.

import { fetchRequestHandler } from '@trpc/server/adapters/fetch'
export const GET = (req) => fetchRequestHandler({ req, router: appRouter, endpoint: '/api/trpc' })
Step 4: Set up React client

Create trpc.ts with typed hooks. Replace fetch calls with useMutation/useQuery hooks.

import { createTRPCReact } from '@trpc/react-query'
export const trpc = createTRPCReact<AppRouter>()
Step 5: Add Zod validation

Input schemas replace express-validator or manual parsing. Types are inferred automatically.

t.procedure.input(z.object({ name: z.string() })).query(({ input }) => input.name)
Step 6: Remove old endpoints
npm uninstall express
All client calls type-check and return correct data

What this migration involves

Moving from REST API (Express/Fastify) to tRPC is rated medium and takes about 4-12h on a typical project. The guide breaks it into 6 steps, 6 of which ship with runnable commands and 1 with a verification check.

FAQ

How long does migrating from REST API (Express/Fastify) to tRPC take?

Around 4-12h for a typical project — 6 steps, difficulty rated medium, 6 of them with copy-paste commands.

Is moving from REST API (Express/Fastify) to tRPC reversible?

No reverse guide is tracked yet, so treat this as a one-way move and keep a full export of your REST API (Express/Fastify) data before you start.

What does tRPC cost compared to REST API (Express/Fastify)?

tRPC: open-source from $0/mo, free plan available.