Skip to main content
Version: 10.x

Define Routers

Initialize tRPC

  • If you don't like the variable name t, you can call it whatever you want
  • You should create your root t-variable exactly once per application
  • You can also create the t-variable with a context, metadata, a error formatter, or a data transformer.
server/trpc.ts
ts
import { initTRPC } from '@trpc/server';
 
export const t = initTRPC.create();
server/trpc.ts
ts
import { initTRPC } from '@trpc/server';
 
export const t = initTRPC.create();

Defining a router

server/_app.ts
ts
import * as trpc from '@trpc/server';
import { t } from './trpc';
 
const appRouter = t.router({
greeting: t.procedure.query(() => 'hello tRPC v10!'),
});
 
// Export only the **type** of a router to avoid importing server code on the client
export type AppRouter = typeof appRouter;
server/_app.ts
ts
import * as trpc from '@trpc/server';
import { t } from './trpc';
 
const appRouter = t.router({
greeting: t.procedure.query(() => 'hello tRPC v10!'),
});
 
// Export only the **type** of a router to avoid importing server code on the client
export type AppRouter = typeof appRouter;