TypeScriptMarch 7, 2026John Doe1 min read

Building Type-Safe APIs with TypeScript and tRPC

9.7 signal
CategoryTypeScript
Theme fitTutorials, threat coverage, product updates, and support-led content
Suggested CTARoute readers into docs, compare pages, or product detail pages
SEO modeSemantic headings, TOC, schema, and clean long-form layout

Type safety across the full stack eliminates an entire class of bugs. Learn how to build end-to-end type-safe APIs using TypeScript, tRPC, and Zod validation.

import { initTRPC, TRPCError } from "@trpc/server";
import { z } from "zod";

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

const postRouter = t.router({
  list: t.procedure
    .input(z.object({
      limit: z.number().min(1).max(100).default(20),
      cursor: z.string().optional(),
    }))
    .query(async ({ input, ctx }) => {
      const posts = await ctx.db.post.findMany({
        take: input.limit + 1,
        cursor: input.cursor ? { id: input.cursor } : undefined,
        orderBy: { createdAt: "desc" },
      });

      let nextCursor: string | undefined;
      if (posts.length > input.limit) {
        const nextItem = posts.pop();
        nextCursor = nextItem?.id;
      }

      return { posts, nextCursor };
    }),

  create: t.procedure
    .input(z.object({
      title: z.string().min(1).max(200),
      content: z.string().min(1),
      tags: z.array(z.string()).max(5),
    }))
    .mutation(async ({ input, ctx }) => {
      if (!ctx.session?.user) {
        throw new TRPCError({ code: "UNAUTHORIZED" });
      }
      return ctx.db.post.create({
        data: { ...input, authorId: ctx.session.user.id },
      });
    }),
});

More Coverage

ShieldCore Support AI

Ask about plans, setup, manuals, support, migration, or product differences.

Sera 2.0
Hi. I can help with pricing, manuals, support tickets, WooCommerce routing, Stripe, PayPal, and product comparison.
Try asking: “How do I compare plans?” or “Where are the manuals?”