Skip to main content

TrpcModule.forRootAsync()

Use forRootAsync() when your tRPC configuration depends on injected providers like ConfigService.

Basic Usage

import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TrpcModule } from 'nest-trpc-native';
import { join } from 'node:path';

@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
TrpcModule.forRootAsync({
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
path: config.get('TRPC_PATH') ?? '/trpc',
autoSchemaFile: join(process.cwd(), 'src/@generated/server.ts'),
createContext: ({ req }) => ({
requestId: String(
req.headers['x-request-id'] ?? crypto.randomUUID(),
),
}),
}),
}),
],
})
export class AppModule {}

Options

forRootAsync() accepts the same async options pattern used throughout NestJS:

OptionTypeDescription
useFactory(...args) => TrpcModuleOptionsFactory function receiving injected providers
injectany[]Tokens to inject into the factory
importsany[]Modules to import for provider resolution

With Middleware

You can combine forRootAsync() with NestJS middleware:

export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(RequestTraceMiddleware).forRoutes(
{ path: 'trpc', method: RequestMethod.ALL },
{ path: 'trpc/(.*)', method: RequestMethod.ALL },
);
}
}
Sample 09

forrootasync-config-middleware is a full working example.