Configuración de su primer proyecto tRPC
Recorra la instalación de tRPC, la creación de un router y la conexión de un cliente para realizar su primera llamada con seguridad de tipos.
Configuración de su primer proyecto tRPC es una lección gratuita de tRPC End-to-End Type Safe APIs en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de tRPC End-to-End Type Safe APIs, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de tRPC End-to-End Type Safe APIs incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en 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.
Aprende tRPC End-to-End Type Safe APIs con un tutor de IA — gratis
Escribe y ejecuta código real en tu navegador, obtén ayuda instantánea de un tutor de IA disponible 24/7 y continúa donde lo dejaste en la web o en la aplicación.
- Cursos
- 10
- Lecciones
- 40
Preguntas frecuentes
¿La lección «Configuración de su primer proyecto tRPC» es gratis?
Sí — el texto completo de «Configuración de su primer proyecto tRPC» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de tRPC End-to-End Type Safe APIs, actualiza a CoddyKit PRO. El curso de tRPC End-to-End Type Safe APIs incluye 4 lecciones en total.
¿Qué aprenderé en «Configuración de su primer proyecto tRPC»?
Recorra la instalación de tRPC, la creación de un router y la conexión de un cliente para realizar su primera llamada con seguridad de tipos. Practicas tRPC End-to-End Type Safe APIs con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar tRPC End-to-End Type Safe APIs?
No se requiere experiencia previa. tRPC End-to-End Type Safe APIs en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.
¿Cuánto tiempo toma la lección «Configuración de su primer proyecto tRPC»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de tRPC End-to-End Type Safe APIs?
Sí. Cada lección de tRPC End-to-End Type Safe APIs incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- ¿Qué es tRPC?
- El poder de la seguridad de tipos
- Descripción general de los conceptos básicos de tRPC
- Configuración de su primer proyecto tRPC