nestjs-trpc
What it does
It guides the AI through defining tRPC routers as NestJS classes, adding Zod schemas and generating the client type. It is for developers who want end-to-end type safety in NestJS.
When to use it
- βWhen you want tRPC procedures inside a NestJS app.
- βWhen you use @Router, @Query or @Mutation decorators in a NestJS project.
- βWhen your client has no types or the AppRouter type is out of date.
Skill link
https://github.com/trycompai/crm/blob/release/.agents/skills/nestjs-trpc/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agentβs skills folder.
Example of what to ask your AI
βCan you add a tRPC query in my NestJS app that returns a user by id with a Zod input schema?β
5 new skills a week. 1 minute to read.
How it works
The AI sets up the decorated router class, registers it with the NestJS module and runs the CLI so the client gets the matching AppRouter type.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
NestJS-tRPC
nestjs-trpc lets you define tRPC procedures as decorated NestJS classes instead of
chaining t.procedure builders. Routers are DI-aware classes that look like controllers;
a separate CLI reads those decorators and emits an AppRouter type for the client.
Verified against v2.13.0. The published docs at nestjs-trpc.io still contain v1-era
examples in several places. Where this skill and the docs disagree, this skill matches the
shipped .d.ts files and the official examples/ in the repo β see
Docs errata for the specific traps.
The one thing to internalize
There are two separate systems that both need to be correct, and they fail independently:
| Runtime | Type generation | |
|---|---|---|
| Driven by | Nest DI container at boot | Rust CLI doing static analysis of your source |
| Needs | classes listed in providers |
decorators it can read literally |
| Fails as | 404 / "no procedures" / undefined injection | stale or missing AppRouter |
A router that works at runtime but was never picked up by the CLI produces a client with no types, and vice versa. When something is wrong, first ask which of the two is broken.
Setup workflow
1. Install
npm install nestjs-trpc zod @trpc/server
Peer deps (v2.13.0): @nestjs/common & @nestjs/core ^9.3.8 || ^10 || ^11,
@trpc/server ^11, zod ^3.14 || ^4, rxjs 7.8.1, reflect-metadata ^0.1.13 || ^0.2.
tRPC v11 is required β v10 will not work.
Standard NestJS tsconfig.json requirements apply: experimentalDecorators: true,
emitDecoratorMetadata: true. tsc must be resolvable, because the CLI shells out to it
to typecheck what it generates.
2. Register the module
// app.module.ts
import { Module } from '@nestjs/common';
import { TRPCModule } from 'nestjs-trpc';
import { UserRouter } from './user.router';
import { UserService } from './user.service';
import { ProtectedMiddleware } from './protected.middleware';
import { AppContext } from './app.context';
@Module({
imports: [
TRPCModule.forRoot({
context: AppContext,
}),
],
providers: [UserRouter, AppContext, UserService, ProtectedMiddleware],
})
export class AppModule {}
Routers, middlewares, and the context class are all ordinary providers. Listing them in
providers is what makes DI work β forgetting a router here is the single most common
reason procedures silently don't exist at runtime.
Default mount point is /trpc; override with basePath. Full option table in
references/module-config.md.
3. Write a router
// user.router.ts
import { Inject } from '@nestjs/common';
import { Router, Query, Mutation, UseMiddlewares, Input, Ctx } from 'nestjs-trpc';
import { TRPCError } from '@trpc/server';
import { z } from 'zod';
import { UserService } from './user.service';
import { ProtectedMiddleware } from './protected.middleware';
import { userSchema, type User } from './user.schema';
@Router({ alias: 'users' })
export class UserRouter {
constructor(@Inject(UserService) private readonly userService: UserService) {}
@Query({
input: z.object({ userId: z.string() }),
output: userSchema,
})
@UseMiddlewares(ProtectedMiddleware)
async getUserById(@Input('userId') userId: string): Promise<User> {
const user = await this.userService.getUser(userId);
if (user == null) {
throw new TRPCError({ message: 'Could not find user.', code: 'NOT_FOUND' });
}
return user;
}
}
Client call site: trpc.users.getUserById.query({ userId }). The alias sets that first
segment β without it the class name is used.
4. Generate the client type
npx nestjs-trpc generate # one-shot
npx nestjs-trpc watch # regenerate on change during development
Writes server.ts (containing export type AppRouter = typeof appRouter) to
./src/@generated by default; --output <dir> moves it. This is a build step, not a
runtime import β the generated file is types only, and your procedures still execute
through the Nest-managed router at runtime.
Add it to your scripts so it can't drift:
{
"scripts": {
"dev": "nestjs-trpc watch & nest start --watch",
"build": "nestjs-trpc generate && nest build"
}
}
Run nestjs-trpc generate --dry-run in CI to fail the build when the committed types are
stale.
Writing procedures
Three procedure decorators, all taking `{ input?, output?,
FAQ
Why might the client have no types?
The CLI reads the decorators to build the AppRouter type, so a missing or stale type usually means the CLI did not pick up the router.
Which validation library does it use?
It uses Zod for input and output schemas.
What does it need installed?
It needs the nestjs-trpc package, tRPC v11 and Zod, plus the module registration in NestJS.
Details
Original description by the author
Build end-to-end type-safe tRPC APIs inside NestJS using the nestjs-trpc adapter β @Router/@Query/@Mutation/@Subscription decorators, TRPCModule.forRoot, injectable middlewares and context, Zod input/output schemas, and the Rust CLI that generates the AppRouter type for clients. Use this skill whenever the user mentions nestjs-trpc, tRPC in a NestJS app, @Router or @Query decorators, TRPCModule, AppRouterHost, "typed API between my Nest backend and frontend", generating AppRouter types, or is migrating a NestJS GraphQL/REST API to tRPC β even if they don't name the library explicitly.
repo: trycompai/crm
Popularity
- GitHub stars
- 12k
- Ranking position
- #11521
- License
- MIT
- Updated
- 11/1/2026
Comments
- β¦
More skills from this repository
ai-elements
12kHelps your agent build AI chat interfaces with ready-made components for conversations, messages, tool displays and prompt inputs.
ai-sdk
12kHelps your agent build AI features with the Vercel AI SDK, including text generation, streaming, tool calling and agents.
better-accessibility
12kHelps your agent make interfaces usable with a keyboard and screen readers, covering focus, labels, ARIA and forms.
better-auth-best-practices
12kHelps your agent set up Better Auth for login, sessions, OAuth and plugins in a TypeScript app, following its documented steps.
Similar skills
api-connector-builder
280kHas your agent build new API connections following the existing pattern. Use when you need to add one more integration without creating a new way of doing things.
api-design
280kHelps your agent design REST APIs following best practices. It's useful for defining how APIs communicate, how errors are shown, and how to organize data.
backend-patterns
280kHelps your agent with server-side architecture patterns and database optimization. It's useful for building or reviewing code that runs on the server.