跳至主内容

快速迭代,零风险。 端到端类型安全 API 轻松实现。

充分发挥 TypeScript 类型推断能力,全面提升全栈应用开发效率

Supported by

Many thanks to all of our amazing sponsors!

端到端类型安全

修改了服务端代码?TypeScript 会在你保存文件前就在客户端提示错误!

流畅的开发体验

tRPC 无需构建或编译步骤,意味着没有代码生成、运行时膨胀或构建过程。

框架无关

兼容所有 JavaScript 框架和运行时环境。轻松集成到现有项目中。

自动补全

使用 tRPC 如同为 API 服务端代码配备 SDK,让你对端点充满信心。

轻量级包体积

tRPC 零依赖且客户端占用极小,极为轻量。

开箱即用

我们提供 React、Next.js、Express、Fastify、AWS Lambda、Solid、Svelte 等框架的适配器。

使用者

tRPC 已获得领先科技团队和众多财富 500 强企业的验证与信赖。

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

简单易用,
提供无与伦比的开发者体验

用 tRPC 快速构建类型安全的 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

定义你的 procedures

创建 tRPC API 的第一步是定义你的 procedures。

Procedures 是构建后端的基础函数,具有composable特性,可以是 query、mutation 或 subscription。Routers 则包含多个 procedures。

在此 procedure 中,我们使用 Zod 验证器确保客户端输入完全符合预期格式,并通过 query 返回简单文本。

文件末尾导出 router 类型,以便稍后在前端代码中使用。

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

创建 HTTP 服务

接下来,我们使用 appRouter 创建 HTTP 服务器。现在你已拥有一个运行中的 tRPC 服务端!

tRPC 提供多种适配器,可无缝适配你的技术栈:Next.js、Express、Fetch API(Astro/Remix/SvelteKit/Cloudflare Workers 等)、Fastify、AWS Lambda 或原生 Node HTTP 服务器。

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

连接客户端并开始查询!

服务端运行后,我们即可创建客户端并发起数据查询。

在初始化客户端时传入 AppRouter 类型,即可获得与后端 API 完全匹配的 TypeScript 自动补全和智能提示——无需任何代码生成!

你可能不再需要传统 API

"我创建 tRPC 的初衷是让人们能move faster——通过消除传统 API 层,同时确保应用在快速迭代中保持稳定。"

亲自尝试,告诉我们你的想法!

Alex/KATT

Alex/KATT

tRPC 创始人

无需我们多言!

众多开发者喜爱 tRPC 及其带来的价值

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 🔥

所有赞助者

衷心感谢所有卓越的sponsors,正是你们确保了 tRPC 的持续发展