Usage with Next.js
Next.js makes it easy for you to build your client and server together in one codebase. tRPC makes it easy to share types between them, ensuring typesafety for your application's data fetching.
If you're using tRPC in a new project, consider using one of the example projects as a starting point or for reference: tRPC Example Projects
Recommended file structure
We recommend a file structure like this one, although it is not enforced by tRPC. This is what you'll get when starting from the examples.
graphql
graphql
Add tRPC to existing Next.js project
1. Install deps
- npm
- yarn
- pnpm
sh
sh
sh
sh
sh
sh
Two packages of note:
- @tanstack/react-query: @trpc/react-queryprovides a thin wrapper around react-query. It is required as a peer dependency as an idiomatic way to handle client-side caching in React applications.
- Zod: We will use Zod for input validation to ensure that our backend only processes requests that fit our API. You can use other validation libraries like Yup, Superstruct, io-ts if you prefer.
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
tsconfig.jsondiff
If strict mode is too harsh, you'll at least want to enable strictNullChecks:
tsconfig.jsondiff
tsconfig.jsondiff
3. Create a tRPC router
Initialize your tRPC backend using the initTRPC function and create your first router. We're going to make a simple "hello world" router and procedure here - but be sure to browse the rest of the docs for information about creating more complex APIs.
View sample backend
server/trpc.tsts
server/trpc.tsts
server/routers/_app.tsts
server/routers/_app.tsts
pages/api/trpc/[trpc].tsts
pages/api/trpc/[trpc].tsts
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
Create a set of strongly-typed hooks using your API's type signature.
utils/trpc.tstsx
utils/trpc.tstsx
createTRPCNext does not work with interop mode. If you are migrating from v9 using interop, keep using the old way of initializing tRPC.
5. Configure _app.tsx
pages/_app.tsxtsx
pages/_app.tsxtsx
6. Make an API request
pages/index.tsxtsx
pages/index.tsxtsx
createTRPCNext() options
config-callback
The config-argument is a function that returns an object that configures the tRPC and React Query clients. This function has a ctx input that gives you access to the Next.js req object, among other things. 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 Query- QueryClientused internally by the tRPC React hooks: QueryClient docs
- queryClient: 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 Transformers
- abortOnUnmount: determines if in-flight requests will be cancelled on component unmount. This defaults to- false.
unstable_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.tstsx
utils/trpc.tstsx
Next steps
Browse the rest of the docs to learn more about things like authorization, middlewares, and error handling.
You can also find usual information about queries and mutations now that you're using @trpc/react-query.