tRPC

Build end-to-end typesafe APIs with tRPC routers, procedures, middleware, and Next.js integration.

5|Updated Dec 8, 2025
One-click install
npx skills add https://github.com/portalshq/portals-studio --skill trpc-portalshq
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: tRPC
Source: https://github.com/portalshq/portals-studio/tree/main/.windsurf/skills/trpc
Command: npx skills add https://github.com/portalshq/portals-studio --skill trpc-portalshq

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Building APIs in TypeScript often means duplicating type definitions between server and client, writing validation logic by hand, and losing autocomplete across the network boundary. This Skill provides expert guidance for tRPC so you get full type inference from server to client without code generation or schemas. ## Core Features & Use Cases - Router & Procedure Setup: Define queries, mutations, and subscriptions with Zod-validated inputs and nested or merged routers. - Middleware & Context: Implement authentication, logging, and role-based access control as reusable middleware with typed context. - Client Integration: Connect React Query-powered hooks with batching, optimistic updates, infinite queries, and structured error handling in Next.js App Router. - Use Case: You are building a Next.js dashboard and need a typesafe CRUD API. Use this Skill to scaffold the app router, protect procedures with auth middleware, and consume them in components with fully inferred hooks. ## Quick Start Ask the AI to set up a tRPC router with a protected procedure and a React Query client in a Next.js App Router project.

Frequently Asked Questions about tRPC

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I set up tRPC with Next.js App Router?

Create a router with initTRPC, expose it through a fetchRequestHandler route at app/api/trpc/[trpc]/route.ts, then wrap your app in a provider combining the tRPC client and React Query's QueryClientProvider. Components call procedures via typed hooks like trpc.user.list.useQuery().

How do I add authentication middleware in tRPC?

Create middleware with t.middleware that checks ctx.session and throws a TRPCError with code UNAUTHORIZED when missing. Chain it onto a procedure to create a protectedProcedure, which also narrows the context type so session is non-nullable downstream.

Does tRPC require code generation or schemas?

No. tRPC infers types directly from your server router definition. Exporting the AppRouter type and importing it on the client gives full autocomplete and compile-time safety without any codegen step.

How do I validate tRPC procedure inputs?

Pass a Zod schema to the .input() method of a procedure. Invalid input is rejected before your handler runs, and the parsed value is fully typed inside the resolver. Schemas can be reused and composed with omit, partial, and extend.

Why is my tRPC query not refetching after a mutation?

React Query caches results independently, so you must invalidate them manually. Call utils.yourRouter.yourProcedure.invalidate() inside the mutation's onSuccess callback using the trpc.useContext() helper to trigger a refetch.

When should I not use tRPC?

tRPC is a poor fit when your API must be consumed by non-TypeScript clients, since its type safety relies on sharing the AppRouter type within a TypeScript codebase. Public or multi-language APIs are better served by REST or GraphQL with explicit schemas.