tRPC - Move Fast and Break Nothing. End-to-end typesafe APIs made easy.

tRPC

3 min read Original article ↗

v11 · stable

Experience the full power of TypeScript inference to boost productivity for your full-stack application.

StarQuickstart

$ pnpm add @trpc/server @trpc/client

const trpc = createTRPCClient({
  router: routerType<AppRouter>(),
  links: [
    httpBatchLink({
      url: 'http://localhost:3000',
    }),
  ],
});

const res = await trpc.greeting.query({ name: 'John' });

const res: `Hello ${string}`

The client of the router below: the types come from the server, with nothing generated.

As used by

  • Netflix
  • Google
  • Cal.com
  • PayPal
  • Langfuse
  • Mistral
  • Documenso
  • Legora
  • Pleo
  • Ping.gg
  • CodeRabbit
  • Greptile

How it works

It's quick and easy to get started with tRPC to build a typesafe API.

  1. Define your procedures

    The first step to creating a tRPC API is to define your procedures.

    Procedures are the functions we will use to build your backend. They're composable and can be queries, mutations, or subscriptions. Routers contain multiple procedures.

    In this procedure, we use a Zod validator to ensure the input from the client has exactly the shape that our procedure expects. We will also return a simple text string from the query.

    At the end of the file, we export the type of the router so we can use it in our frontend code in just a few moments.

    Define procedures
    const t = initTRPC.create();
    
    const router = t.router;
    const publicProcedure = t.procedure;
    
    const appRouter = router({
      greeting: publicProcedure
        .input(z.object({ name: z.string() }))
        .query((opts) => {
          const { input } = opts;

    const input: {
        name: string;
    }

    The input, after the validators and any middleware that replaced it with next({ input }).

    return `Hello ${input.name}` as const; }), }); export type AppRouter = typeof appRouter;
  2. Create your HTTP server

    Next, we create our HTTP server using our appRouter. We now have a tRPC server running!

    tRPC has many adapters so it can meet you where you are. Next.js, Express, the Fetch API (Astro, Remix, SvelteKit, Cloudflare Workers, etc.), Fastify, AWS Lambda, or a vanilla Node HTTP server.

    Adapters
    const { listen } = createHTTPServer({
      router: appRouter,
    });
    
    // The API will now be listening on port 3000!
    listen(3000);
  3. Connect your client and start querying!

    Now that we have the server running, we can create a client and start querying data.

    We pass the AppRouter type when creating the client to give us TypeScript autocompletion and Intellisense that matches the backend API without requiring any code generation!

    The vanilla client
    const trpc = createTRPCClient({
      router: routerType<AppRouter>(),
      links: [
        httpBatchLink({
          url: 'http://localhost:3000',
        }),
      ],
    });
    
    const res = await trpc.greeting.query({ name: 'John' });

    const res: `Hello ${string}`

Features

  • Automatic typesafetyMade a server side change? TypeScript will warn you of errors on your client before you even save the file!

  • Snappy DXtRPC has no build or compile steps, meaning no code generation, runtime bloat or build step.

  • Framework agnosticCompatible with all JavaScript frameworks and runtimes. It's easy to add to your existing projects.

  • AutocompletionUsing tRPC is like using an SDK for your API's server code, giving you confidence in your endpoints.

  • Light bundle sizetRPC has zero dependencies and a tiny client-side footprint making it lightweight.

  • Batteries includedWe provide adapters for React, Next.js, Express, Fastify, AWS Lambda, Solid, Svelte, and more.

tRPC is MIT licensed and built in the open, kept going by the companies and people below. Thank you!

Become a sponsor

Our latest sponsors: Vault, Venue.ink, and Stefan Smiljkovic. Thank you!