Most of the web app's API is tRPC 11 with TanStack Query 5: end-to-end types, no code generation.
Setup
| File | Purpose |
|---|
apps/web/trpc/init.ts | Context (headers and session) and the procedure builders |
apps/web/trpc/org.ts | Organization-scoped builders (Organizations module) |
apps/web/trpc/routers/_app.ts | The root router: one key per feature router |
apps/web/trpc/client.tsx | TRPCReactProvider and the useTRPC() hook |
apps/web/trpc/server.ts | Server-side trpc options proxy, getQueryClient() and caller() |
apps/web/trpc/apiRegistry.ts | Every procedure with its access level, for Admin → API |
apps/web/app/api/trpc/[trpc]/route.ts | The HTTP handler at /api/trpc |
Procedure builders
| Builder | Access |
|---|
baseProcedure | Public |
protectedProcedure | Signed in; guests may only run queries |
adminProcedure | Admins |
guestReadableAdminProcedure | Admins; guests may run queries |
orgProcedure, orgRoleProcedure(role) | Active workspace membership (Organizations module) |
Routers
| Key | Router file | Module |
|---|
landing | landingProcedures.ts | Core: landing content and the CMS editor |
documentation | docProcedures.ts | Core: docs index, pages and the docs editor |
support | supportProcedures.ts | Core: support messages, newsletter, assistant |
home | homeProcedures.ts | Core: set a password for OAuth accounts |
admin | adminProcedures.ts | Core: registration mode and invitations |
seo | seoProcedures.ts | Core: SEO audit and GA4 reports |
billing | billingProcedures.ts | Billing |
organization | organizationProcedures.ts | Organizations |
ai | aiProcedures.ts | AI Platform |
aiJobs | aiJobsProcedures.ts | AI Agents |
apiKey | apiKeyProcedures.ts | API Keys |
project, scaffold | projectProcedures.ts, scaffoldCatalogProcedures.ts | Open-source repository only |
Writing a router
// apps/web/trpc/routers/reportProcedures.ts
import { z } from "zod";
import { TRPCError } from "@trpc/server";
import db from "@workspace/database/client";
import { createTRPCRouter, protectedProcedure } from "@/trpc/init";
export const reportRouter = createTRPCRouter({
list: protectedProcedure.query(({ ctx }) =>
db.report.findMany({
where: { userId: ctx.session.user.id },
orderBy: { createdAt: "desc" },
}),
),
create: protectedProcedure
.input(z.object({ title: z.string().trim().min(1).max(120) }))
.mutation(({ ctx, input }) =>
db.report.create({ data: { title: input.title, userId: ctx.session.user.id } }),
),
remove: protectedProcedure
.input(z.object({ id: z.string() }))
.mutation(async ({ ctx, input }) => {
const { count } = await db.report.deleteMany({
where: { id: input.id, userId: ctx.session.user.id },
});
if (count === 0) throw new TRPCError({ code: "NOT_FOUND" });
return { ok: true };
}),
});
Then:
- Register it in
_app.ts: report: reportRouter.
- Add a group to
API_REGISTRY in apiRegistry.ts listing each procedure's name, type and access. trpc/routers/__tests__/apiRegistry.test.ts fails until the registry matches the router.
Calling from client components
"use client";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useTRPC } from "@/trpc/client";
export function Reports() {
const trpc = useTRPC();
const queryClient = useQueryClient();
const reports = useQuery(trpc.report.list.queryOptions());
const create = useMutation(
trpc.report.create.mutationOptions({
onSuccess: () => queryClient.invalidateQueries({ queryKey: trpc.report.list.queryKey() }),
}),
);
// ...
}
Calling from the server
import { getQueryClient, trpc, caller } from "@/trpc/server";
// Prefetch for hydration (preferred in pages)
await getQueryClient().ensureQueryData(trpc.report.list.queryOptions());
// Or call a procedure directly
const reports = await (await caller()).report.list();
Things to know
- No data transformer.
Date values arrive in the client as ISO strings. Convert them (new Date(value)), or return strings from your procedures.
- Errors: throw
TRPCError with UNAUTHORIZED, FORBIDDEN, NOT_FOUND, BAD_REQUEST, CONFLICT or PRECONDITION_FAILED. Never return secrets or stack traces in messages.
- Validation: every input goes through Zod. Trim strings and bound lengths.
- Native clients: the desktop app (a tRPC client) and the mobile app (fetch wrappers in
apps/mobile/lib) call the same /api/trpc endpoints with the session. They are not type-checked against AppRouter, so update them when a procedure they use changes.
- Heavy procedures (the download builder) are served from a separate endpoint,
/api/scaffold/trpc, through useScaffoldTRPC(), so the main /api/trpc function stays small. Use the same trick for procedures with large bundles.