Cette page est affichée en anglais. Une traduction française est en cours.
SkillsApr 11, 2026·3 min de lecture

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.

Prêt pour agents

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.

Needs Confirmation · 64/100Policy : confirmer
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
step-1.md
Commande avec revue préalable
npx -y tokrepo@latest install baa87f69-3559-11f1-9bc6-00163e2b0d79 --target codex

Dry-run d'abord, confirmez les écritures, puis lancez cette commande.

TL;DR
Build fully typesafe APIs that share types between frontend and backend automatically. No schemas, no codegen, no runtime bloat.
§01

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.

§02

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.

§03

How to use

  1. Install tRPC packages:
npm install @trpc/server @trpc/client @trpc/react-query @tanstack/react-query zod
  1. 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;
  1. Call from the client with full type safety -- the client knows getUser returns { id, name, role }.
§04

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.

§05

Related on TokRepo

§06

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

Does tRPC replace GraphQL?+

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.

What is the runtime overhead of tRPC?+

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.

Does tRPC work with Next.js?+

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.

Can I use tRPC with React Query?+

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.

How does tRPC handle authentication?+

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

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires