# tRPC

> End-to-end typesafe APIs for TypeScript. Write procedures on the server, call them from the client with their types, and serve them on any runtime.

Every page is also markdown: add `.md` to its URL, or request it with `Accept: text/markdown`.

## Getting started

- [Getting started](https://v12.trpc.io/docs/getting-started.md): A router, a handler that serves it, and a client that calls it with the router's types.
- [Packages and stability](https://v12.trpc.io/docs/packages.md): The entry points of @trpc12/trpc, the framework packages, and what experimental_ and @stability promise.

## Server

- [Procedures](https://v12.trpc.io/docs/server/procedures.md): Queries, mutations and subscriptions, with validated input and output.
- [Routers](https://v12.trpc.io/docs/server/routers.md): Plain objects of procedures, merged with mergeRouters and split with lazy().
- [Middleware](https://v12.trpc.io/docs/server/middleware.md): Base procedures that add to the context, replace the input or short-circuit a call.
- [Errors](https://v12.trpc.io/docs/server/errors.md): One error class, typed on the client from what resolvers return, with or without declarations.
- [Context](https://v12.trpc.io/docs/server/context.md): What every call gets as ctx, where it comes from, and how middleware adds to it.
- [Streaming](https://v12.trpc.io/docs/server/streaming.md): Return async generators, Effect streams and nested promises; validate each item with stream().
- [Files](https://v12.trpc.io/docs/server/files.md): File and Blob values in inputs and outputs, sent as multipart, raw bodies or streamed chunks.
- [Responses](https://v12.trpc.io/docs/server/responses.md): Set headers and status from a resolver, or return a response of your own.
- [Effect](https://v12.trpc.io/docs/server/effect.md): Resolvers that return Effects, services from the handler's layer, typed failures and tracing.
- [Extensions](https://v12.trpc.io/docs/server/extensions.md): Packages that add middleware, ctx, meta keys, errors and services to every procedure with t.concat().
- [Serializers](https://v12.trpc.io/docs/server/serializers.md): Plain JSON by default; danSON or superjson-style serializers keep Dates, Maps and files, and client types follow.
- [Contract-first](https://v12.trpc.io/docs/server/contract-first.md): Describe procedures in a contract, implement them later, and type clients from the contract alone.
- [Server-side calls](https://v12.trpc.io/docs/server/server-side-calls.md): Call procedures in process with createRouterClient, call() and localLink.

## Middleware

- [Lifecycle hooks](https://v12.trpc.io/docs/middleware/lifecycle.md): onStart, onSuccess, onError and onFinish, for logging and metrics.
- [Rate limiting](https://v12.trpc.io/docs/middleware/rate-limit.md): rateLimit() counts calls with Effect's RateLimiter, in memory or in Redis, and sets the RateLimit headers.
- [Locks](https://v12.trpc.io/docs/middleware/lock.md): experimental_lock() runs calls with the same key one at a time, across instances.
- [Durable calls](https://v12.trpc.io/docs/middleware/durable.md): experimental_durable() keeps a call's result, so a client that lost the response gets it instead of running the mutation twice.

## Client

- [The client](https://v12.trpc.io/docs/client/overview.md): createTrpcClient types every call from the router's type, and the call options from its links.
- [Links](https://v12.trpc.io/docs/client/links.md): httpLink, wsLink, splitLink, switchLink and the other built-in links, and how calls travel over HTTP.
- [Custom links](https://v12.trpc.io/docs/client/custom-links.md): Write a link with link(), and declare the call options it reads, the errors it fails with and what it needs.
- [Errors without try/catch](https://v12.trpc.io/docs/client/errors.md): safe() and createSafeClient() return result tuples with typed, narrowable errors.
- [Validating on the client](https://v12.trpc.io/docs/client/validation.md): validationLink checks inputs before sending and results when they arrive, against a contract.
- [The Effect client](https://v12.trpc.io/docs/client/effect.md): createEffectClient returns an Effect per call and a Stream per subscription, with typed errors in the error channel.
- [Tracing](https://v12.trpc.io/docs/client/tracing.md): Client spans that propagate traceparent and b3, so client and server share one trace.

## Subscriptions

- [Subscriptions](https://v12.trpc.io/docs/subscriptions/overview.md): Streams of events over SSE or WebSockets, with tracked() ids so reconnects resume.
- [Publisher](https://v12.trpc.io/docs/subscriptions/publisher.md): Typed publish/subscribe as an Effect service, with a channel per audience and resumable events.
- [Publisher backends](https://v12.trpc.io/docs/subscriptions/backends.md): Recipes for a Publisher on Redis streams, Postgres LISTEN/NOTIFY and Durable Objects.

## OpenAPI

- [REST routes](https://v12.trpc.io/docs/openapi/routes.md): Serve the same router over REST: route meta, the handler, inputs, outputs and groups.
- [The document](https://v12.trpc.io/docs/openapi/document.md): Generate an OpenAPI 3.1 document from the router's schemas, or from its types.
- [Calling REST from the client](https://v12.trpc.io/docs/openapi/client.md): openApiLink sends each call to its REST route, typed from a contract.
- [A contract from OpenAPI](https://v12.trpc.io/docs/openapi/from-document.md): Generate a typed contract module from any OpenAPI 3.0 or 3.1 document.

## Adapters

- [Adapters](https://v12.trpc.io/docs/adapters/overview.md): One handler serves HTTP, SSE and WebSockets on every runtime; pick the adapter for yours.
- [Node](https://v12.trpc.io/docs/adapters/node.md): Serve on Node with Effect's HttpServer, or mount the handler in Express, Fastify and node:http.
- [Bun](https://v12.trpc.io/docs/adapters/bun.md): Bun.serve takes handler.fetch; WebSockets run on Effect's Bun server.
- [Deno](https://v12.trpc.io/docs/adapters/deno.md): Deno.serve with handler.fetch, and handler.websocket for upgrades.
- [Fetch runtimes](https://v12.trpc.io/docs/adapters/fetch.md): Cloudflare Workers, Next.js route handlers and other fetch runtimes take handler.fetch as it is.
- [AWS Lambda](https://v12.trpc.io/docs/adapters/aws-lambda.md): toLambdaHandler for API Gateway, ALB, function URLs and Lambda@Edge, with response streaming.
- [Durable Objects](https://v12.trpc.io/docs/adapters/durable-objects.md): Hibernating WebSockets, publishers, durable calls, locks, Workers Traces and connection tokens on Cloudflare Durable Objects.
- [Message ports](https://v12.trpc.io/docs/adapters/message-ports.md): Serve calls over a MessagePort, a web Worker or a worker_threads port.
- [Firebase](https://v12.trpc.io/docs/adapters/firebase.md): Call procedures from Firebase callable functions and map their errors to HttpsError.
- [Raw sockets](https://v12.trpc.io/docs/adapters/sockets.md): serveSocket serves the router over TCP, TLS or Unix sockets, with experimental_socketLink on the client.

## Integrations

- [TanStack Query](https://v12.trpc.io/docs/integrations/tanstack-query.md): Query and mutation options for every TanStack Query framework, with streamed mutations, subscriptions and hydration.
- [Server Actions](https://v12.trpc.io/docs/integrations/server-actions.md): Turn procedures into React Server Actions that take typed input or FormData, with useActionState and optimistic updates.
- [React Server Components](https://v12.trpc.io/docs/integrations/rsc.md): Prefetch in server components, stream pending queries to the browser, and tag Next's data cache by query key.
- [React without TanStack Query](https://v12.trpc.io/docs/integrations/experimental-react.md): Render-as-you-fetch with use() and Suspense in @trpc12/experimental-react, where cache features are links.
- [React atoms](https://v12.trpc.io/docs/integrations/experimental-react-atom.md): Query, mutation and subscription atoms on Effect Atom, typed with each procedure's errors.
- [AI SDK](https://v12.trpc.io/docs/integrations/ai-sdk.md): Stream useChat responses from a procedure, and give procedures to the model as tools.
- [Better Auth](https://v12.trpc.io/docs/integrations/better-auth.md): Read the Better Auth session in a middleware and type ctx.user with its user.
- [NestJS](https://v12.trpc.io/docs/integrations/nestjs.md): Serve a contract from Nest controllers with @trpc12/nest.

## Migrations

- [Migrating from v11](https://v12.trpc.io/docs/migrations/from-v11.md): Serve v11 clients from the new router with createV11Handler, run the codemod, then move the clients.

## Optional

- [Full documentation](https://v12.trpc.io/llms-full.txt): every page above in one file
