Define Routers
Initialize tRPC​
tip
- 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. - It's good to limit the methods you export from the
t
object in order to constrain your team to use only a few base procedures
server/trpc.tsts
import {initTRPC } from '@trpc/server';Âconstt =initTRPC .create ();Â// We explicitly export the methods we use here// This allows us to create reusable & protected base proceduresexport constmiddleware =t .middleware ;export constrouter =t .router ;export constpublicProcedure =t .procedure ;
server/trpc.tsts
import {initTRPC } from '@trpc/server';Âconstt =initTRPC .create ();Â// We explicitly export the methods we use here// This allows us to create reusable & protected base proceduresexport constmiddleware =t .middleware ;export constrouter =t .router ;export constpublicProcedure =t .procedure ;
Defining a router​
server/_app.tsts
import * astrpc from '@trpc/server';import {publicProcedure ,router } from './trpc';ÂconstappRouter =router ({greeting :publicProcedure .query (() => 'hello tRPC v10!'),});Â// Export only the **type** of a router to avoid importing server code on the clientexport typeAppRouter = typeofappRouter ;
server/_app.tsts
import * astrpc from '@trpc/server';import {publicProcedure ,router } from './trpc';ÂconstappRouter =router ({greeting :publicProcedure .query (() => 'hello tRPC v10!'),});Â// Export only the **type** of a router to avoid importing server code on the clientexport typeAppRouter = typeofappRouter ;
initTRPC
options​
Use chaining to setup your t
-object, example:
ts
initTRPC().context<Context>().meta<Meta>().create({ /* [...] */})
ts
initTRPC().context<Context>().meta<Meta>().create({ /* [...] */})
.context<Context>()
​
Setup a request context.
.meta<Meta>()
​
Setup metadata for your procedures.
.create(opts: Partial<RuntimeConfig>)
​
RuntimeConfig
reference:
ts
export interface RuntimeConfig<TTypes extends RootConfigTypes> {/*** Use a data transformer* @link https://trpc.io/docs/data-transformers*/transformer: TTypes['transformer'];/*** Use custom error formatting* @link https://trpc.io/docs/error-formatting*/errorFormatter: ErrorFormatter<TTypes['ctx'], any>;/*** Allow `@trpc/server` to run in non-server environments* @warning **Use with caution**, this should likely mainly be used within testing.* @default false*/allowOutsideOfServer: boolean;/*** Is this a server environment?* @warning **Use with caution**, this should likely mainly be used within testing.* @default typeof window === 'undefined' || 'Deno' in window || process.env.NODE_ENV === 'test'*/isServer: boolean;/*** Is this development?* Will be used to decide if the API should return stack traces* @default process.env.NODE_ENV !== 'production'*/isDev: boolean;}
ts
export interface RuntimeConfig<TTypes extends RootConfigTypes> {/*** Use a data transformer* @link https://trpc.io/docs/data-transformers*/transformer: TTypes['transformer'];/*** Use custom error formatting* @link https://trpc.io/docs/error-formatting*/errorFormatter: ErrorFormatter<TTypes['ctx'], any>;/*** Allow `@trpc/server` to run in non-server environments* @warning **Use with caution**, this should likely mainly be used within testing.* @default false*/allowOutsideOfServer: boolean;/*** Is this a server environment?* @warning **Use with caution**, this should likely mainly be used within testing.* @default typeof window === 'undefined' || 'Deno' in window || process.env.NODE_ENV === 'test'*/isServer: boolean;/*** Is this development?* Will be used to decide if the API should return stack traces* @default process.env.NODE_ENV !== 'production'*/isDev: boolean;}