tRPC — End-to-End Typesafe APIs for TypeScript
tRPC lets you build fully typesafe APIs without schemas, code generation, or runtime bloat. Share types between frontend and backend automatically. Works with Next.js, React, Vue, and more.
Installation avec revue préalable
Cet actif nécessite une revue. Le prompt copié demande un dry-run, affiche les écritures, puis continue seulement après confirmation.
npx -y tokrepo@latest install baa87f69-3559-11f1-9bc6-00163e2b0d79 --target codexDry-run d'abord, confirmez les écritures, puis lancez cette commande.
What it is
tRPC lets you build fully typesafe APIs in TypeScript without writing schemas, running code generators, or adding runtime validation overhead. When you define a procedure on the server, the client automatically knows the input types, output types, and available endpoints. Change a return type on the server and your frontend shows a compile error immediately.
tRPC targets TypeScript teams building full-stack applications where the API layer between frontend and backend is a source of type mismatches. It works with Next.js, React, Vue, and any TypeScript frontend framework.
How it saves time or tokens
tRPC eliminates the schema definition and code generation step that tools like GraphQL or OpenAPI require. There is no .graphql file to maintain, no openapi.yaml to keep in sync, and no codegen CLI to run after changes. TypeScript's type inference handles everything at compile time. For development velocity, this means changing an API response shape and seeing the impact across your entire codebase instantly.
How to use
- Install tRPC packages:
npm install @trpc/server @trpc/client @trpc/react-query @tanstack/react-query zod
- Define a router on the server:
import { initTRPC } from '@trpc/server';
import { z } from 'zod';
const t = initTRPC.create();
export const appRouter = t.router({
getUser: t.procedure
.input(z.object({ id: z.string() }))
.query(({ input }) => {
return { id: input.id, name: 'Alice', role: 'admin' };
}),
});
export type AppRouter = typeof appRouter;
- Call from the client with full type safety -- the client knows
getUserreturns{ id, name, role }.
Example
Full client-server type inference:
// Client-side: types are inferred from the server router
import { trpc } from './utils/trpc';
function UserProfile() {
const { data } = trpc.getUser.useQuery({ id: '123' });
// data is typed as { id: string; name: string; role: string }
return <h1>{data?.name}</h1>;
}
Rename name to displayName on the server, and the client shows a TypeScript error immediately.
Related on TokRepo
- AI Tools for API — API frameworks and tools for building web services
- AI Tools for Coding — Development tools for TypeScript and full-stack projects
Common pitfalls
- tRPC requires both frontend and backend to be TypeScript. If your backend is Python, Go, or another language, tRPC does not apply.
- tRPC uses HTTP POST for all procedures by default. This can cause issues with CDN caching that expects GET requests. Use the httpBatchLink configuration for batching.
- Large routers with hundreds of procedures can slow down TypeScript type checking. Split routers into smaller sub-routers and merge them.
Questions fréquentes
For TypeScript-only stacks, tRPC provides similar type safety benefits without the schema layer. If you need a language-agnostic API consumed by non-TypeScript clients, GraphQL or REST with OpenAPI is more appropriate.
Minimal. tRPC's type safety is entirely at compile time. At runtime, it is a thin layer over HTTP that serializes and deserializes JSON. There is no runtime schema validation unless you explicitly add Zod input validation.
Yes. tRPC has first-class Next.js integration. The server router runs in Next.js API routes, and the client hooks work in React components. This is the most common deployment pattern for tRPC.
Yes. The @trpc/react-query package wraps TanStack React Query, providing familiar useQuery and useMutation hooks with full tRPC type inference. You get caching, refetching, and optimistic updates out of the box.
tRPC uses middleware and context. You create a context function that extracts the user from the request (JWT, session, etc.) and pass it to procedures. Protected procedures check the context for authentication before executing.
Sources citées (3)
- tRPC GitHub— tRPC provides end-to-end typesafe APIs for TypeScript
- tRPC Documentation— Type inference between frontend and backend without code generation
- tRPC Next.js Docs— Next.js integration for full-stack TypeScript applications
En lien sur TokRepo
Fil de discussion
Actifs similaires
oRPC — Typesafe RPC Framework for TypeScript
Build end-to-end typesafe APIs with a lightweight RPC framework that works across React, Vue, Svelte, and server runtimes.
Create T3 App — Full-Stack TypeScript Starter with Next.js, tRPC & Prisma
The best way to start a full-stack, typesafe Next.js application. Scaffolds a project with Next.js, TypeScript, tRPC, Prisma, Tailwind CSS, and NextAuth.js in seconds.
Create T3 App — Full-Stack Typesafe Next.js Starter
Create T3 App is the fastest way to scaffold a full-stack, typesafe Next.js application using the T3 Stack: Next.js, TypeScript, tRPC, Prisma, Tailwind CSS, and NextAuth.js.
Create T3 App — Full-Stack TypeScript Starter with Next.js, tRPC & Prisma
An interactive CLI that scaffolds a production-ready full-stack TypeScript application with a curated stack of Next.js, tRPC, Tailwind CSS, Prisma, and NextAuth.js.