End-to-End Type-Safe APIs for NestJS

The missing bridge between NestJS and tRPC. Decorators you already know, types that write themselves, powered by a Rust CLI that generates in milliseconds.

5K weekly downloads
1K GitHub stars
20 contributors
MIT Licensed
NestJS tRPC demo showing end-to-end type-safe API development with decorators and automatic type generation

The client above is not importing any code from the server, only its type declarations.

Why NestJS tRPC?

Everything you need to build type-safe APIs in NestJS.

End-to-End Type Safety

Full static typesafety and autocompletion for inputs, outputs, and errors — from server to client. Zero runtime overhead.

Rust-Powered CLI

Type generation in milliseconds, not seconds. 10-50x faster than TypeScript-based tools with rich error messages.

NestJS Dependency Injection

Routers, middlewares, and error handlers are first-class NestJS providers.

Zero Boilerplate

@Router, @Query, @Mutation decorators replace manual router wiring.

Real-Time Subscriptions

Stream data with SSE using async generators on tRPC v11.

Express & Fastify

Auto-detected HTTP adapter. Works out of the box with both.

Typed Middlewares & Context

Global and per-route middlewares with full DI support and auto-typed context.

Watch Mode

Automatic type regeneration during development.

Get Started in 3 Steps

From install to type-safe client in under 5 minutes.

1

Install & Configure

Add nestjs-trpc to your NestJS app and configure the module with your preferred settings.

app.module.ts
import { Module } from '@nestjs/common';
import { TRPCModule } from 'nestjs-trpc';
@Module({
imports: [
TRPCModule.forRoot(),
],
})
export class AppModule {}
2

Define Your Router

Use familiar NestJS decorators with full dependency injection and Zod schema validation.

user.router.ts
import { Router, Query } from 'nestjs-trpc';
import { Inject } from '@nestjs/common';
import { z } from 'zod';
import { UserService } from './user.service';
@Router({ alias: 'users' })
export class UserRouter {
constructor(
@Inject(UserService)
private readonly userService: UserService,
) {}
@Query({
input: z.object({ id: z.string() }),
output: z.object({
id: z.string(),
name: z.string(),
}),
})
getUserById(input: { id: string }) {
return this.userService.findById(input.id);
}
}
3

Generate & Consume

Run the CLI to generate types, then enjoy full autocompletion on the client.

client.ts
// Run: npx nestjs-trpc generate
// Client-side usage with full type safety
import { createTRPCClient } from '@trpc/client';
import type { AppRouter } from './server/@generated';
const client = createTRPCClient<AppRouter>({
links: [/* ... */],
});
// Full autocompletion & type checking
const user = await client.users.getUserById.query({
id: '1',
});
// ^? { id: string; name: string }

Built for the NestJS Ecosystem

NestJS tRPC works the way you'd expect a NestJS library to work.

Without NestJS tRPC

Manual router wiring with nested function calls

With NestJS tRPC

Declarative @Router, @Query, @Mutation decorators

Without NestJS tRPC

Separate dependency injection system or none at all

With NestJS tRPC

Native NestJS DI — inject services directly into routers

Without NestJS tRPC

Manual type generation scripts or no generation at all

With NestJS tRPC

Rust CLI generates types in milliseconds with watch mode

Frequently Asked Questions

Is NestJS tRPC production-ready?
Yes. NestJS tRPC v2.0.0 is stable and used in production by multiple companies. The library has comprehensive test coverage, supports tRPC v11, and follows semantic versioning for safe upgrades.
How does it compare to using tRPC directly?
NestJS tRPC wraps tRPC with NestJS-native decorators (@Router, @Query, @Mutation) and integrates with NestJS dependency injection. You get the same end-to-end typesafety as vanilla tRPC, but with the conventions and DI system you already use in NestJS.
What versions of tRPC and Zod are supported?
NestJS tRPC v2.0.0 supports tRPC v11 and both Zod 3 and Zod 4 schemas for input/output validation. The library tracks tRPC releases closely and supports the latest stable versions.
How fast is the type generation CLI?
The CLI is written in Rust and generates types 10-50x faster than equivalent TypeScript-based tools. For most projects, generation completes in single-digit milliseconds. Watch mode keeps types in sync during development with near-zero latency.
Can I use this with my existing NestJS project?
Yes. NestJS tRPC is designed for incremental adoption. Install the package, configure TRPCModule.forRoot() in your app module, and start adding @Router classes alongside your existing controllers. It works with both Express and Fastify adapters.
Does it support real-time subscriptions?
Yes. NestJS tRPC supports real-time subscriptions using Server-Sent Events (SSE) with async generators on tRPC v11. You can stream data from the server to clients with full type safety.