Hoppa till huvudinnehållet

Flyg framåt utan att bryta saker. Enkelt skapande av typsäkra API:er från slutpunkt till slutpunkt.

Upplev den fulla kraften av TypeScript-inferens för att öka produktiviteten i din fullstack-applikation.

Supported by

Many thanks to all of our amazing sponsors!

Automatisk typsäkerhet

Gjorde du en ändring på serversidan? TypeScript varnar dig för fel på din klient innan du ens sparar filen!

Smidig DX (Developer Experience)

tRPC har inga bygg- eller kompileringssteg, vilket innebär ingen kodgenerering, runtime-uppblåsthet eller byggsteg.

Ramverksoberoende

Kompatibel med alla JavaScript-ramverk och runtime-miljöer. Det är enkelt att lägga till i dina befintliga projekt.

Autokomplettering

Att använda tRPC är som att använda ett SDK för din API:s serverkod, vilket ger dig tillförsikt i dina slutpunkter.

Lätt paketstorlek

tRPC har noll beroenden och en minimal klient-sida storlek vilket gör det lättviktigt.

Batterier ingår

Vi tillhandahåller adapters för React, Next.js, Express, Fastify, AWS Lambda, Solid, Svelte och fler.

Används av

tRPC är testat och betrott av ledande teknikteam och många Fortune 500-företag.

  • Cal.com
  • Documenso
  • Google
  • Langfuse
  • Mistral
  • Netflix
  • PayPal
  • Ping.gg
  • Pleo

Enkelt att använda med
oöverträffad utveckarupplevelse

Det är snabbt och enkelt att komma igång med tRPC för att bygga ett typsäkert API.

ts
const t = initTRPC.create();
 
const router = t.router;
const publicProcedure = t.procedure;
 
const appRouter = router({
greeting: publicProcedure
.input(z.object({ name: z.string() }))
.query((opts) => {
const { input } = opts;
const input: { name: string; }
 
return `Hello ${input.name}` as const;
}),
});
 
export type AppRouter = typeof appRouter;
ts
const t = initTRPC.create();
 
const router = t.router;
const publicProcedure = t.procedure;
 
const appRouter = router({
greeting: publicProcedure
.input(z.object({ name: z.string() }))
.query((opts) => {
const { input } = opts;
const input: { name: string; }
 
return `Hello ${input.name}` as const;
}),
});
 
export type AppRouter = typeof appRouter;

1

Definiera dina procedurer

Första steget för att skapa ett tRPC-API är att definiera dina procedurer.

Procedurer är funktionerna vi kommer använda för att bygga din backend. De är composable och kan vara queries, mutations eller subscriptions. Routers innehåller flera procedurer.

I denna procedur använder vi en Zod-validerare för att säkerställa att indatan från klienten har exakt den form som vår procedur förväntar sig. Vi kommer också returnera en enkel textsträng från queryn.

I slutet av filen exporterar vi router-typen så vi kan använda den i vår frontend-kod inom bara några ögonblick.

ts
const { listen } = createHTTPServer({
router: appRouter,
});
 
// The API will now be listening on port 3000!
listen(3000);
ts
const { listen } = createHTTPServer({
router: appRouter,
});
 
// The API will now be listening on port 3000!
listen(3000);

2

Skapa din HTTP-server

Därefter skapar vi vår HTTP-server med vår appRouter. Nu har vi en tRPC-server igång!

tRPC har många adaptrar så att den kan möta dig där du är: Next.js, Express, Fetch API (Astro, Remix, SvelteKit, Cloudflare Workers, etc.), Fastify, AWS Lambda eller en vanlig Node HTTP-server.

ts
const trpc = createTRPCClient<AppRouter>({
links: [
httpBatchLink({
url: 'http://localhost:3000',
}),
],
});
 
const res = await trpc.greeting.query({ name: 'John' });
ts
const trpc = createTRPCClient<AppRouter>({
links: [
httpBatchLink({
url: 'http://localhost:3000',
}),
],
});
 
const res = await trpc.greeting.query({ name: 'John' });

3

Anslut din klient och börja skicka queries!

Nu när servern är igång kan vi skapa en klient och börja hämta data.

Vi skickar med AppRouter-typen när vi skapar klienten för att få TypeScript-autocompletion och Intellisense som matchar backend-API:et utan någon kodgenerering!

Du kanske inte behöver ett traditionellt API

"Jag byggde tRPC för att låta människor move faster genom att ta bort behovet av ett traditionellt API-lager, samtidigt som vi kan vara säkra på att våra appar inte kommer att brytas när vi itererar snabbt."

Prova själv och låt oss veta vad du tycker!

Alex/KATT

Alex/KATT

Skaparen av tRPC

Ta inte bara vårt ord för det!

Många utvecklare älskar tRPC och vad det för med sig.

theo

Theo - t3.gg

@theo

The amount that tRPC has improved the quality of our code, the speed of our delivery, and the happiness of our devs is hard to comprehend. I know I shill it a lot but seriously, please try @trpcio
ralex1993

R. Alex Anderson 🚀

@ralex1993

🤯 tRPC 10 enables VS Code's "Change All Occurrences" feature to work _across the client/server boundary_! In this video, I rename a procedure input using "Change All Occurrences", and that change propagates to anywhere the input is used across the entire app. 🤩 cc @trpcio
kentcdodds

Kent C. Dodds 🌌

@kentcdodds

If I didn't already get end-to-end type safety from @remix_run, I would 100% be investigating @trpcio very seriously. If you're not on Remix, I suggest you give it a look 👀
sockthedev

Sock, the dev 🧦

@sockthedev

If you are all in on TypeScript you MUST use tRPC for your API. No ifs, no buts. tRPC destroys the boundary between frontend and backend. You get to focus on building features for your app. Best tool for time to market hyper mode. Marry me @alexdotjs 💍
leeb

Lee Byron

@leeb

Hearing @theo and @mxstbr #tRPC and @GraphQL and find they agree that both are awesome and there’s a time and a place for each 💖
jokull

Jökull Solberg

@jokull

tRPC is insane. I’m building a Stripe integration – I return Stripe API payloads from the server I get the response data typed for my React components without even saving the files, as if I’m using the Stripe library on the frontend not backend. /cc @alexdotjs
christian_legge

Christian Legge

@christian_legge

Spent all of yesterday learning and implementing @trpcio and wow, what a great investment. I can't believe how much time I spent (read: wasted) validating and parsing queries and responses!
TkDodo

Dominik 🇺🇦

@TkDodo

That being said, we _are_ starting a production project right now, and we're using @nextjs with @trpcio . It's so good I don't even know where to start 🔥. Probably with the e2e type-safety 😍 Haven't thought about client state much but the former probably applies.
housecor

Cory House

@housecor

@trpcio Love it. Simple, strong types. Feels like a more elegant choice than plain REST or GraphQL when using TS in a monorepo.
andersmellson

Anders Bech Mellson

@andersmellson

Spent today playing with @trpcio v10 and I'm officially in love 😍 ps. Don't tell my wife 🙊
grabbou

Mike | grabbou.eth 🚀

@grabbou

@theo @trpcio Totally. I am literally smiling every time I write a procedure, because it reminds of how hard it used to be in the past. Built-in errors, typed middleware (that can alter context), input validation. It's just massive!
osamaqarem

Osama Qarem

@osamaqarem

I’ve been playing with @trpcio and Solito together and this is without a doubt the fastest way to iterate on an api/website/mobile app there is today 🔥

Alla sponsorer

Vi älskar verkligen alla våra fantastiska sponsors som hjälper till att se till att tRPC finns kvar.