Set up with Next.js Pages Router
This guide is for the Next.js Pages Router. If you are using the Next.js App Router, see the App Router setup guide instead.
Recommended file structure
We recommend a file structure like this one, although it is not enforced by tRPC. This is what you'll see in our examples. The rest of this page will take you through the process of adding tRPC in to this structure.
graphql.├── prisma # <-- if prisma is added│ └── [..]├── src│ ├── pages│ │ ├── _app.tsx # <-- add `withTRPC()`-HOC here│ │ ├── api│ │ │ └── trpc│ │ │ └── [trpc].ts # <-- tRPC HTTP handler│ │ └── [..]│ ├── server│ │ ├── routers│ │ │ ├── _app.ts # <-- main app router│ │ │ ├── post.ts # <-- sub routers│ │ │ └── [..]│ │ ├── context.ts # <-- create app context│ │ └── trpc.ts # <-- procedure helpers│ └── utils│ └── trpc.ts # <-- your typesafe tRPC hooks└── [..]
graphql.├── prisma # <-- if prisma is added│ └── [..]├── src│ ├── pages│ │ ├── _app.tsx # <-- add `withTRPC()`-HOC here│ │ ├── api│ │ │ └── trpc│ │ │ └── [trpc].ts # <-- tRPC HTTP handler│ │ └── [..]│ ├── server│ │ ├── routers│ │ │ ├── _app.ts # <-- main app router│ │ │ ├── post.ts # <-- sub routers│ │ │ └── [..]│ │ ├── context.ts # <-- create app context│ │ └── trpc.ts # <-- procedure helpers│ └── utils│ └── trpc.ts # <-- your typesafe tRPC hooks└── [..]
Add tRPC to existing Next.js project
1. Install deps
- npm
- yarn
- pnpm
- bun
- deno
npm install @trpc/server @trpc/client @trpc/react-query @trpc/next @tanstack/react-query@latest zod
yarn add @trpc/server @trpc/client @trpc/react-query @trpc/next @tanstack/react-query@latest zod
pnpm add @trpc/server @trpc/client @trpc/react-query @trpc/next @tanstack/react-query@latest zod
bun add @trpc/server @trpc/client @trpc/react-query @trpc/next @tanstack/react-query@latest zod
deno add npm:@trpc/server npm:@trpc/client npm:@trpc/react-query npm:@trpc/next npm:@tanstack/react-query@latest npm:zod
The Next.js integration is actually a combination of our React Query Integration and some Next.js specific integrations.
2. Enable strict mode
If you want to use Zod for input validation, make sure you have enabled strict mode in your tsconfig.json:
tsconfig.jsondiff"compilerOptions": {+ "strict": true}
tsconfig.jsondiff"compilerOptions": {+ "strict": true}
If strict mode is too harsh, you'll at least want to enable strictNullChecks:
tsconfig.jsondiff"compilerOptions": {+ "strictNullChecks": true}
tsconfig.jsondiff"compilerOptions": {+ "strictNullChecks": true}
3. Create a tRPC router
Initialize your tRPC backend in src/server/trpc.ts using the initTRPC function, and create your first router. We're going to make a simple "hello world" router and procedure here - but for deeper information on creating your tRPC API you should refer to:
- the Quickstart guide and Backend usage docs for tRPC information
- the Next.js Adapter docs for mounting tRPC within your Next.js server.
View sample backend
server/trpc.tstsimport {initTRPC } from '@trpc/server';// Avoid exporting the entire t-object// since it's not very descriptive.// For instance, the use of a t variable// is common in i18n libraries.constt =initTRPC .create ();// Base router and procedure helpersexport constrouter =t .router ;export constprocedure =t .procedure ;
server/trpc.tstsimport {initTRPC } from '@trpc/server';// Avoid exporting the entire t-object// since it's not very descriptive.// For instance, the use of a t variable// is common in i18n libraries.constt =initTRPC .create ();// Base router and procedure helpersexport constrouter =t .router ;export constprocedure =t .procedure ;
server/routers/_app.tstsimport {z } from 'zod';import {procedure ,router } from '../trpc';export constappRouter =router ({hello :procedure .input (z .object ({text :z .string (),}),).query ((opts ) => {return {greeting : `hello ${opts .input .text }`,};}),});// export type definition of APIexport typeAppRouter = typeofappRouter ;
server/routers/_app.tstsimport {z } from 'zod';import {procedure ,router } from '../trpc';export constappRouter =router ({hello :procedure .input (z .object ({text :z .string (),}),).query ((opts ) => {return {greeting : `hello ${opts .input .text }`,};}),});// export type definition of APIexport typeAppRouter = typeofappRouter ;
pages/api/trpc/[trpc].tstsimport * astrpcNext from '@trpc/server/adapters/next';import {appRouter } from '../../../server/routers/_app';// export API handler// @link https://trpc.io/docs/server/adaptersexport defaulttrpcNext .createNextApiHandler ({router :appRouter ,createContext : () => ({}),});
pages/api/trpc/[trpc].tstsimport * astrpcNext from '@trpc/server/adapters/next';import {appRouter } from '../../../server/routers/_app';// export API handler// @link https://trpc.io/docs/server/adaptersexport defaulttrpcNext .createNextApiHandler ({router :appRouter ,createContext : () => ({}),});
The backend above is using the recommended file structure, but you can keep it simple and put everything in an API handler directly if you prefer.
4. Create tRPC hooks
Use the createTRPCNext function to create a set of strongly-typed hooks from your API's type signature.
utils/trpc.tstsximport {httpBatchLink } from '@trpc/client';import {createTRPCNext } from '@trpc/next';import type {AppRouter } from '../server/routers/_app';functiongetBaseUrl () {if (typeofwindow !== 'undefined')// browser should use relative pathreturn '';if (process .env .VERCEL_URL )// reference for vercel.comreturn `https://${process .env .VERCEL_URL }`;if (process .env .RENDER_INTERNAL_HOSTNAME )// reference for render.comreturn `http://${process .env .RENDER_INTERNAL_HOSTNAME }:${process .env .PORT }`;// assume localhostreturn `http://localhost:${process .env .PORT ?? 3000}`;}export consttrpc =createTRPCNext <AppRouter >({config (config ) {return {links : [httpBatchLink ({/*** If you want to use SSR, you need to use the server's full URL* @see https://trpc.io/docs/client/nextjs/pages-router/ssr**/url : `${getBaseUrl ()}/api/trpc`,// You can pass any HTTP headers you wish hereasyncheaders () {return {// authorization: getAuthCookie(),};},}),],};},/*** @see https://trpc.io/docs/client/nextjs/pages-router/ssr**/ssr : false,});
utils/trpc.tstsximport {httpBatchLink } from '@trpc/client';import {createTRPCNext } from '@trpc/next';import type {AppRouter } from '../server/routers/_app';functiongetBaseUrl () {if (typeofwindow !== 'undefined')// browser should use relative pathreturn '';if (process .env .VERCEL_URL )// reference for vercel.comreturn `https://${process .env .VERCEL_URL }`;if (process .env .RENDER_INTERNAL_HOSTNAME )// reference for render.comreturn `http://${process .env .RENDER_INTERNAL_HOSTNAME }:${process .env .PORT }`;// assume localhostreturn `http://localhost:${process .env .PORT ?? 3000}`;}export consttrpc =createTRPCNext <AppRouter >({config (config ) {return {links : [httpBatchLink ({/*** If you want to use SSR, you need to use the server's full URL* @see https://trpc.io/docs/client/nextjs/pages-router/ssr**/url : `${getBaseUrl ()}/api/trpc`,// You can pass any HTTP headers you wish hereasyncheaders () {return {// authorization: getAuthCookie(),};},}),],};},/*** @see https://trpc.io/docs/client/nextjs/pages-router/ssr**/ssr : false,});
createTRPCNext does not work with the tRPC-v9 interop mode. If you are migrating from v9 using interop, you should continue using the old way of initializing tRPC.
5. Configure _app.tsx
Wrap your root app page in the trpc.withTRPC HOC, similar to this:
pages/_app.tsxtsximport type {AppType } from 'next/app';import {trpc } from '../utils/trpc';constMyApp :AppType = ({Component ,pageProps }) => {return <Component {...pageProps } />;};export defaulttrpc .withTRPC (MyApp );
pages/_app.tsxtsximport type {AppType } from 'next/app';import {trpc } from '../utils/trpc';constMyApp :AppType = ({Component ,pageProps }) => {return <Component {...pageProps } />;};export defaulttrpc .withTRPC (MyApp );
6. Make an API request
You're all set!
You can now use the React hooks you have just created to invoke your API. For more detail see the React Query Integration
pages/index.tsxtsximport {trpc } from '../utils/trpc';export default functionIndexPage () {consthello =trpc .hello .useQuery ({text : 'client' });if (!hello .data ) {return <div >Loading...</div >;}return (<div ><p >{hello .data .greeting }</p ></div >);}
pages/index.tsxtsximport {trpc } from '../utils/trpc';export default functionIndexPage () {consthello =trpc .hello .useQuery ({text : 'client' });if (!hello .data ) {return <div >Loading...</div >;}return (<div ><p >{hello .data .greeting }</p ></div >);}
createTRPCNext() options
config-callback
The config-argument is a function that returns an object that configures the tRPC and React Query clients. This function receives an object with an optional ctx property (of type NextPageContext) that gives you access to the Next.js req object during server-side rendering. The returned value can contain the following properties:
- Required:
linksto customize the flow of data between tRPC Client and the tRPC Server. Read more.- Optional:
queryClientConfig: a configuration object for the React QueryQueryClientused internally by the tRPC React hooks: QueryClient docsqueryClient: a React Query QueryClient instance- Note: You can only provide either a
queryClientor aqueryClientConfig.
- Note: You can only provide either a
transformer: a transformer applied to outgoing payloads. Read more about Data TransformersabortOnUnmount: determines if in-flight requests will be cancelled on component unmount. This defaults tofalse.
overrides: (default: undefined)
Configure overrides for React Query's hooks.
ssr-boolean (default: false)
Whether tRPC should await queries when server-side rendering a page. Defaults to false.
responseMeta-callback
Ability to set request headers and HTTP status when server-side rendering.
Example
utils/trpc.tstsximport {createTRPCNext } from '@trpc/next';import type {AppRouter } from '../server/routers/_app';export consttrpc =createTRPCNext <AppRouter >({config (config ) {return {links : [/* [...] */],};},});
utils/trpc.tstsximport {createTRPCNext } from '@trpc/next';import type {AppRouter } from '../server/routers/_app';export consttrpc =createTRPCNext <AppRouter >({config (config ) {return {links : [/* [...] */],};},});
Next steps
Browse the rest of the docs to learn more about things like authorization, middlewares, and error handling.
You can also find information about queries and mutations now that you're using @trpc/react-query.