0Pricing
TypeScript Academy · Lesson

Defining Routers and Procedures

Build queries and mutations with input validation.

Initializing tRPC

Every tRPC backend starts by creating an instance with initTRPC. It returns builders for routers and procedures. You usually create it once and export the pieces you need.

import { initTRPC } from '@trpc/server';

const t = initTRPC.create();
export const router = t.router;
export const publicProcedure = t.procedure;

A First Procedure

A procedure is a single API endpoint. The simplest one is a query that returns a value. publicProcedure.query defines a readable endpoint with no input.

import { router, publicProcedure } from './trpc';

const appRouter = router({
  health: publicProcedure.query(() => 'ok'),
});

All lessons in this course

  1. The tRPC Architecture
  2. Defining Routers and Procedures
  3. Client-Server Type Inference
  4. Middleware and Context
← Back to TypeScript Academy