Angular tRPC integration
The Angular tRPC integration connects a typed tRPC router to Angular applications that use RxJS and server-side rendering. It creates an injectable client for query and mutation procedures, reuses server results during hydration, and exposes a Nitro handler that turns HTTP requests into tRPC calls.
Client and request boundary
The client side is the main boundary between Angular code and the tRPC API. It turns an application router type into an injectable proxy and sends procedure calls through a shared link chain.
Client creation
home/…/client.ts provides createTrpcClient, which accepts an AppRouter type, an API URL, optional tRPC settings, and batch-link settings. It returns the client token, its provider factory, and a reactive header value so an application can register the client once and inject it where needed.
The provider installs cache-state support and builds the client with application links first. The transfer-state link then handles server-rendered data, and the HTTP batch link sends calls to the configured URL. The application example uses /api/trpc and shares superjson with its server router. See Server APIs and notes for the wider server API context.
References: libs/…/client.ts• L12-19 libs/…/client.ts• L50-94 apps/…/trpc-client.ts• L1-14 apps/…/app.config.ts• L10-19
RxJS procedure proxy
The RxJS proxy gives Angular code a router-shaped client. A nested procedure path ends in query or mutate, and each call returns an RxJS Observable with the procedure output. This keeps the procedure contract inferred from the shared router while fitting Angular subscription and signal workflows.
The proxy supports queries and mutations only. It passes each request through the tRPC link chain, converts the tRPC observable to an RxJS observable, forwards the result or TRPCClientError, and removes the subscription when the RxJS subscriber unsubscribes.
References: libs/…/trpc-rxjs-proxy.ts• L22-52 libs/…/trpc-rxjs-proxy.ts• L61-95 libs/…/trpc-rxjs-proxy.ts• L146-215
Request link chain
The link chain is the request path from a typed client call to the transport. Each link can inspect or handle an operation before the next link runs, so application-specific links can extend the request without changing the proxy.
The default chain ends with an HTTP batch link. Its fetch layer can use the host application's $fetch implementation and can supply a local Nitro or Analog host when a request runs on the server. A configured data transformer must match the transformer used by the server router.
The request sequence is:
flowchart LR
A[Angular procedure call] --> B[RxJS tRPC proxy]
B --> C[Application links]
C --> D[Transfer-state link]
D --> E[HTTP batch link]
E --> F[Nitro tRPC route]
References: libs/…/client.ts• L21-48 libs/…/client.ts• L64-77 libs/…/shared-internal.ts• L32-55 libs/…/shared-internal.ts• L58-93
Rendered data and cache state
The integration uses Angular TransferState to move procedure results from server rendering to the browser. Cache state limits this reuse to the initial client startup, which prevents old server-rendered values from serving as a long-term client cache.
Transfer-state link
The transfer-state link creates a key from the operation type, procedure path, and input. On the server it forwards the request and stores the result in TransferState; during browser startup it reads a matching value and can return it without a second network call. superjson preserves values that plain JSON cannot represent.
After the application becomes stable, the cache state turns off. Later requests use the network instead of the transferred value. Failed requests remove their stored value so an error does not remain available for a later request.
References: libs/…/transfer-state-link.ts• L8-17 libs/…/transfer-state-link.ts• L39-81 libs/…/cache-state.ts• L6-25
Cache lifetime
The cache-state provider starts with caching active and registers an Angular bootstrap listener. That listener waits for application stability, then marks caching inactive. The transfer-state link reads this shared state for every operation, so the cache lifetime is controlled at the application level rather than by individual procedures.
This design matches the purpose of hydration: the browser can use data already produced for the page, while normal client activity receives fresh responses after startup.
References: libs/…/cache-state.ts• L10-25 libs/…/transfer-state-link.ts• L42-63
Server procedure boundary
The server side adapts Nitro and h3 requests to the tRPC HTTP protocol. It keeps router execution, request context, response metadata, and error reporting in the server application while the shared handler owns the HTTP translation.
Nitro server adapter
home/…/server.ts provides createTrpcNitroHandler. The host application gives it an appRouter and an optional context creator. The returned h3 handler reads the procedure path from the route parameter, reads query or request-body input, and passes the request to tRPC's HTTP resolver.
The adapter returns tRPC's status, headers, and body through the Nitro response. It also supports batching and optional response metadata and error hooks. A missing trpc route parameter produces a server error because the adapter cannot identify the procedure.
References: libs/…/server.ts• L18-49 libs/…/server.ts• L51-63 libs/…/server.ts• L65-127
Application router and context
The application composes its procedures into appRouter and exports AppRouter as the type shared with the client. The context creator defines the request-scoped values available to procedures; the current example returns an empty context. Procedures then validate input and perform the domain action, such as listing, creating, or removing notes.
The route home/…/[trpc].ts connects this router and context to the shared Nitro adapter. This gives the client URL /api/trpc one entry point for the router's procedure paths.
References: apps/…/context.ts• L1-7 apps/…/trpc.ts• L1-13 apps/…/index.ts• L1-8 apps/…/[trpc].ts• L1-8 apps/…/notes.ts• L6-28
Shared call utilities
Shared utilities keep the client and server sides aligned without exposing the full tRPC client internals to Angular code. They define the supported request kinds, pass link context, run links in order, and bridge asynchronous procedure results to Angular's render flow.
Link execution and data transformation
The internal request chain calls one link at a time and passes the resulting operation to the next link. It requires a final link that produces a response, which is why the default HTTP batch link is the transport endpoint. Shared types also enforce the data-transformer contract between the router and client.
References: libs/…/shared-internal.ts• L11-35 libs/…/shared-internal.ts• L36-55 libs/…/shared-internal.ts• L58-93
Render waiting
waitFor accepts either an RxJS observable or a promise and returns a promise for its first observable value. It also schedules a Zone macro task and invokes it when the result arrives, which lets server-rendered content resolvers account for the asynchronous procedure call.
References: libs/…/wait-for.ts• L1-21
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the client transformer and server router transformer aligned. The client request and transfer-state paths both depend on that shared serialization contract.
- Keep the client URL and the Nitro route parameter name aligned. The HTTP client targets
/api/trpc, while the adapter reads thetrpcroute parameter. - When changing the router procedure shape, check both the exported
AppRoutertype and the client proxy call shape. The client derives its paths and input/output types from that router. - When changing hydration or application bootstrap timing, review the cache-state transition. It controls whether the transfer-state link can answer a request without a network call.
- When changing link order, preserve a terminal transport link after any link that forwards operations. The request chain cannot complete without one.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Client and server use different data transformers | Inputs or outputs can fail to deserialize or lose their intended value types | Configure the same transformer on both sides, as the example does with superjson |
The tRPC route does not expose a trpc parameter | The Nitro adapter returns an internal server error because it cannot find the procedure path | Keep the route in [trpc].ts or an equivalent catch-all route with that parameter |
| The client provider is not registered in Angular application providers | Injecting the tRPC client fails before a procedure call can run | Register provideTrpcClient() during application setup |
| A custom link does not forward an operation and does not return a result | The procedure observable never reaches the HTTP transport | End custom handling deliberately or pass the operation to the next link |
| A transferred result is read with a key that differs from the server key | Hydration misses the stored value and sends an extra request | Keep operation type, path, and input stable across server and browser calls |
Key files
| File | Why to read it |
|---|---|
| libs/…/client.ts | Defines the public Angular client factory, default links, headers, and server-aware fetch boundary |
| libs/…/trpc-rxjs-proxy.ts | Shows how typed router paths become RxJS query and mutation calls |
| libs/…/transfer-state-link.ts | Defines server-to-browser result reuse and its operation cache key |
| libs/…/cache-state.ts | Defines the startup window for transfer-state caching |
| libs/…/shared-internal.ts | Defines link execution and the client/server transformer contract |
| libs/…/server.ts | Defines the Nitro and h3 adapter for tRPC HTTP requests |
| apps/…/trpc-client.ts | Shows application client configuration and router type sharing |
| apps/…/[trpc].ts | Connects the application router to the server adapter |
| apps/…/index.ts | Defines the application router and exports the client-facing AppRouter type |
| apps/…/context.ts | Defines the request context contract used by server procedures |
| libs/…/wait-for.ts | Bridges observable or promise results to Zone-aware render waiting |