trpc
Assists with building end-to-end type-safe APIs using tRPC without code generation. Use when creating type-safe client-server bridges, defining procedures with Zod validation, integrating with Next.js or React Query, or setting up WebSocket subscriptions. Trigger words: trpc, type-safe api, procedures, react query, end-to-end types.
Usage
Getting Started
- Install the skill using the command above
- Open your AI coding agent (Claude Code, Codex, Gemini CLI, or Cursor)
- Reference the skill in your prompt
- The AI will use the skill's capabilities automatically
Example Prompts
- "Review the open pull requests and summarize what needs attention"
- "Generate a changelog from the last 20 commits on the main branch"
Documentation
Overview
tRPC enables building fully type-safe APIs without code generation by creating seamless client-server type bridges. Changing a backend procedure instantly updates TypeScript types on the frontend, providing end-to-end type safety across the full stack.
Instructions
- When setting up tRPC, initialize with
initTRPC.create(), define a context factory for session/database access, and createpublicProcedureandprotectedProcedurebases with middleware chains. - When defining procedures, use Zod schemas for input validation (
.input(z.object({...}))), choose query for read operations and mutation for writes, and keep procedures thin by extracting business logic into service functions. - When organizing routers, create one router per feature domain and merge all sub-routers in a single
appRouter, exporting its type asAppRouter. - When integrating with Next.js, use
@trpc/nextfor App Router support,createCallerfor server components, and@trpc/react-queryfor client-side cache management with optimistic updates. - When handling errors, throw
TRPCErrorwith typed error codes (NOT_FOUND,UNAUTHORIZED,BAD_REQUEST) instead of generic errors. - When implementing real-time features, use subscription procedures with
wsLinkandobservable()for WebSocket-based updates. - When testing, use
createCallerto call procedures directly without HTTP, and inject mock context for isolated tests.
Examples
Example 1: Set up a type-safe API for a Next.js app
User request: "Add tRPC to my Next.js project with user and post routers"
Actions:
- Initialize tRPC with context factory extracting session from request
- Create
userRouterandpostRouterwith Zod-validated procedures - Merge into
appRouterand set up Next.js API route handler - Configure
@trpc/react-queryclient with batching link
Output: A fully typed API where frontend components have autocomplete for all backend procedures.
Example 2: Add authentication middleware
User request: "Create a protected procedure that checks user auth before allowing access"
Actions:
- Create auth middleware that validates session from context
- Define
protectedProcedureby chaining the middleware ontopublicProcedure - Use
protectedProcedurefor all routes requiring authentication - Return typed
UNAUTHORIZEDerror when session is missing
Output: A reusable protected procedure base with proper error handling.
Guidelines
- One router per feature domain:
userRouter,orderRouter,notificationRouter. - Merge all sub-routers in a single
appRouterand export its type:export type AppRouter = typeof appRouter. - Always validate inputs with Zod schemas; never trust raw client data.
- Use
TRPCErrorwith appropriate codes, not genericthrow new Error(). - Keep procedures thin: extract business logic into service functions.
- Co-locate input schemas with their procedures or in a shared
schemas/package for monorepos. - Use middleware for cross-cutting concerns instead of duplicating auth checks.
Information
- Version
- 1.0.0
- Author
- terminal-skills
- Category
- Development
- License
- Apache-2.0