Configurando seu primeiro projeto tRPC
Veja como instalar o tRPC, criar um roteador e conectar um cliente para fazer sua primeira chamada com segurança de tipos.
Configurando seu primeiro projeto tRPC é uma aula grátis de tRPC End-to-End Type Safe APIs no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de tRPC End-to-End Type Safe APIs, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de tRPC End-to-End Type Safe APIs inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
What We Are Building
You know what tRPC is and why type safety matters. Now you'll set up a real project and make your first end-to-end type-safe call.
Installing the Packages
tRPC ships as separate server and client packages. Install them alongside Zod for validation.
npm install @trpc/server @trpc/client zodInitializing tRPC
Create one t object with initTRPC.create() — everything else (router, procedures) is built from it.
import { initTRPC } from "@trpc/server";
const t = initTRPC.create();
export const router = t.router;
export const publicProcedure = t.procedure;Creating a Router
A router groups procedures. Here's a minimal one exposing a single greeting query.
export const appRouter = router({
hello: publicProcedure.query(() => {
return "Hello tRPC";
}),
});Exporting the Router Type
The core trick: export only the router's type, not its code. The client learns the full API shape with zero codegen.
export type AppRouter = typeof appRouter;Serving the Router
Attach your router to an HTTP server using a tRPC adapter — here, the standalone createHTTPServer.
import { createHTTPServer } from "@trpc/server/adapters/standalone";
createHTTPServer({ router: appRouter }).listen(3000);Creating the Client
The client imports the AppRouter type to unlock full autocomplete and type checking against your server.
import { createTRPCClient, httpBatchLink } from "@trpc/client";
import type { AppRouter } from "./server";
const client = createTRPCClient<AppRouter>({
links: [httpBatchLink({ url: "http://localhost:3000" })],
});Making the First Call
Call the procedure as if it were a local typed function — that's the whole point of tRPC.
const greeting = await client.hello.query();
console.log(greeting); // "Hello tRPC"Type Safety in Action
Type safety in action: rename hello on the server and the client call becomes a compile error instantly — no runtime surprise.
Project Structure
A common project layout: server/trpc.ts for the t object, server/router.ts for procedures, and client/index.ts for the typed client.
Adding Inputs
Most procedures take input. Validate it with Zod and that input type flows to the client automatically.
greet: publicProcedure
.input(z.object({ name: z.string() }))
.query(({ input }) => "Hi " + input.name),Quick Check
Test your setup knowledge.
Recap
Recap: you installed the tRPC packages, built a router and exported its type, and made a typed client call — a full end-to-end type-safe pipeline.
Aprenda tRPC End-to-End Type Safe APIs com um tutor de IA — grátis
Escreva e execute código real no seu navegador, obtenha ajuda instantânea de um tutor de IA 24/7 e continue de onde parou na web ou no app.
- Cursos
- 10
- Aulas
- 40
Perguntas Frequentes
A aula “Configurando seu primeiro projeto tRPC” é grátis?
Sim — o texto completo de “Configurando seu primeiro projeto tRPC” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de tRPC End-to-End Type Safe APIs, atualize para CoddyKit PRO. O curso de tRPC End-to-End Type Safe APIs inclui 4 aulas no total.
O que vou aprender em “Configurando seu primeiro projeto tRPC”?
Veja como instalar o tRPC, criar um roteador e conectar um cliente para fazer sua primeira chamada com segurança de tipos. Você pratica tRPC End-to-End Type Safe APIs com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar tRPC End-to-End Type Safe APIs?
Nenhuma experiência prévia é necessária. tRPC End-to-End Type Safe APIs no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “Configurando seu primeiro projeto tRPC”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de tRPC End-to-End Type Safe APIs?
Sim. Cada aula de tRPC End-to-End Type Safe APIs inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- O que é tRPC?
- O poder da segurança de tipos
- Visão geral dos conceitos fundamentais do tRPC
- Configurando seu primeiro projeto tRPC