tRPC v11 + Next.js App Router
Build fully type-safe APIs that share types between your Next.js server and client — no schemas, no code generation. Uses the new @trpc/tanstack-react-query integration.
Create Next.js Project
Scaffold a new Next.js app with TypeScript and the App Router.
npx create-next-app@latest my-trpc-app --typescript --tailwind --app --src-dirInstall tRPC v11 and Dependencies
tRPC v11 uses @trpc/tanstack-react-query — the new TanStack Query-native client that replaces the legacy @trpc/react-query. Also install server-only and client-only to enforce module boundaries. See migration guide.
npm install @trpc/server @trpc/client @trpc/tanstack-react-query @tanstack/react-query zod server-only client-onlyInitialize the tRPC Instance
Create src/server/trpc.ts. initTRPC.create() returns the builder you use to define all procedures and routers. The server-only import prevents this from being accidentally imported in client components.
Create the App Router
Define your procedures in src/server/routers/app.ts. Queries return data, mutations change it. Zod validates all inputs at runtime — a mismatch throws a 400 before your resolver runs.
Create the Next.js API Route Handler
Mount the tRPC router on a Next.js catch-all Route Handler using the fetch adapter — required for the App Router since it uses Web-standard Request/Response.
Create the Typed tRPC Client
Create src/lib/trpc/client.ts. In tRPC v11, createTRPCContext produces a TRPCProvider and a useTRPC hook — all fully typed from your AppRouter. The client-only import prevents server components from accidentally calling this.
Create the Providers Component
Wrap your app with the TanStack Query client and the tRPC provider. httpBatchStreamLink batches multiple tRPC calls into a single HTTP request and supports streaming responses.
Wrap Root Layout with Providers
Import your Providers component in the root layout.
Use tRPC in a Client Component
In tRPC v11, useTRPC() returns the typed router. Pass the result to TanStack Query hooks via .queryOptions() and .mutationOptions(). Full autocomplete and inference — no manual types needed.
Start the Development Server
Run the dev server. Hover over any tRPC call to see the inferred input/output types. Rename a field in your router — TypeScript will flag every caller instantly.
npm run devFound this guide helpful? Check out more tools and guides.
Browse All GuidesPrerequisites
- Node.js 18+
- Next.js 14+ with App Router
- TypeScript knowledge