Skip to content
trpc logo

tRPC — End-to-End Type-Safe APIs

trpc

You are an expert in tRPC, the framework for building type-safe APIs without schemas or code generation. You help developers create full-stack TypeScript applications where the server defines procedures and the client calls them with full type inference — no REST routes, no GraphQL schemas, no OpenAPI specs, just TypeScript functions that are type-safe from database to UI.

TerminalSkills/skills0installs160stars

SKILL.md

Full skill instructions

tRPC — End-to-End Type-Safe APIs

You are an expert in tRPC, the framework for building type-safe APIs without schemas or code generation. You help developers create full-stack TypeScript applications where the server defines procedures and the client calls them with full type inference — no REST routes, no GraphQL schemas, no OpenAPI specs, just TypeScript functions that are type-safe from database to UI.

Core Capabilities

Server

// server/​trpc.ts — tRPC setup
import { initTRPC, TRPCError } from "@trpc/​server";
import { z } from "zod";

const t = initTRPC.context<Context>().create();

export const router = t.router;
export const publicProcedure = t.procedure;
export const protectedProcedure = t.procedure.use(async ({ ctx, next }) => {
  if (!ctx.session?.user) throw new TRPCError({ code: "UNAUTHORIZED" });
  return next({ ctx: { user: ctx.session.user } });
});

// server/​routers/​users.ts
export const usersRouter = router({
  getById: publicProcedure
    .input(z.object({ id: z.string() }))
    .query(async ({ input, ctx }) => {
      const user = await ctx.db.users.findUnique({ where: { id: input.id } });
      if (!user) throw new TRPCError({ code: "NOT_FOUND", message: "User not found" });
      return user;
    }),

  list: publicProcedure
    .input(z.object({
      cursor: z.string().optional(),
      limit: z.number().min(1).max(100).default(20),
    }))
    .query(async ({ input, ctx }) => {
      const items = await ctx.db.users.findMany({
        take: input.limit + 1,
        cursor: input.cursor ? { id: input.cursor } : undefined,
        orderBy: { createdAt: "desc" },
      });
      const hasMore = items.length > input.limit;
      return { items: items.slice(0, input.limit), nextCursor: hasMore ? items[input.limit].id : undefined };
    }),

  update: protectedProcedure
    .input(z.object({ name: z.string().min(1), bio: z.string().max(500).optional() }))
    .mutation(async ({ input, ctx }) => {
      return ctx.db.users.update({ where: { id: ctx.user.id }, data: input });
    }),
});

// server/​routers/​_app.ts
export const appRouter = router({
  users: usersRouter,
  posts: postsRouter,
});
export type AppRouter = typeof appRouter;

React Client

import { trpc } from "@/​utils/​trpc";

function UserProfile({ userId }: { userId: string }) {
  // Full type inference — hover shows return type from server
  const { data: user, isLoading } = trpc.users.getById.useQuery({ id: userId });
  const updateUser = trpc.users.update.useMutation({
    onSuccess: () => utils.users.getById.invalidate({ id: userId }),
  });
  const utils = trpc.useUtils();

  if (isLoading) return <Spinner />;

  return (
    <div>
      <h1>{user?.name}</​h1>   {/​* user is typed as User | undefined */​}
      <button onClick={() => updateUser.mutate({ name: "New Name" })}>
        {updateUser.isPending ? "Saving..." : "Update"}
      </​button>
    </​div>
  );
}

// Infinite scroll
function UserList() {
  const { data, fetchNextPage, hasNextPage } = trpc.users.list.useInfiniteQuery(
    { limit: 20 },
    { getNextPageParam: (lastPage) => lastPage.nextCursor },
  );

  return (
    <>
      {data?.pages.flatMap(p => p.items).map(user => <UserCard key={user.id} user={user} />)}
      {hasNextPage && <button onClick={() => fetchNextPage()}>Load More</​button>}
    </>
  );
}

Installation

npm install @trpc/​server @trpc/​client @trpc/​react-query @tanstack/​react-query zod

Best Practices

  1. Zero code generation — Types flow from server to client via TypeScript inference; no build step needed
  2. Zod validation — Use .input(z.object(...)) for runtime validation; type-safe at compile AND runtime
  3. React Query under the hood — useQuery, useMutation, useInfiniteQuery all work; full caching
  4. Procedure types — query for reads, mutation for writes, subscription for WebSocket streams
  5. Middleware — Chain middleware for auth, logging, rate limiting; protectedProcedure pattern
  6. Error handling — Use TRPCError with standard codes; client receives typed error responses
  7. Batching — tRPC batches multiple queries into one HTTP request by default; reduces roundtrips
  8. Next.js integration — Use @trpc/​next for seamless App Router / Pages Router integration

More skills from TerminalSkills

zipkin logo
TerminalSkills/skills

zipkin

>-

160 0
View
yjs logo
TerminalSkills/skills

yjs

Expert guidance for Yjs, the high-performance CRDT (Conflict-free Replicated Data Type) framework for building collaborative applications. Helps developers implement real-time document editing, offline-first sync, and peer-to-peer collaboration with automatic conflict resolution.

160 0
View
testcontainers logo
TerminalSkills/skills

testcontainers

When the user wants to run integration tests with real dependencies using Docker containers managed by Testcontainers. Also use when the user mentions "testcontainers," "integration testing with Docker," "database integration tests," "containerized tests," or "test with real database." For API mo...

160 0
View
twilio logo
TerminalSkills/skills

twilio

>-

160 0
View
wxt logo
TerminalSkills/skills

wxt

>-

160 0
View
xata logo
TerminalSkills/skills

xata

Expert guidance for Xata, the serverless data platform that combines PostgreSQL, Elasticsearch, and AI capabilities in a single API. Helps developers build applications with full-text search, vector similarity search, file attachments, and branching — all through a type-safe TypeScript SDK.

160 0
View
together-ai logo
TerminalSkills/skills

together-ai

Cloud platform for running open-source AI models. Provides inference APIs for LLMs, image models, and embedding models. Supports fine-tuning on custom data, OpenAI-compatible API format, and competitive pricing for open-source model hosting.

160 0
View
weaviate logo
TerminalSkills/skills

weaviate

Weaviate is an open-source vector database with built-in vectorization modules. Learn schema definition, GraphQL and REST APIs, hybrid search combining BM25 and vectors, and self-hosted deployment with Docker.

160 0
View
val-town logo
TerminalSkills/skills

val-town

>-

160 0
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View