Calling tRPC Procedures Directly from Server-Side Code
A server-side caller lets you invoke your own tRPC procedures with full type inference, bypassing HTTP round trips. Here's how it works, with code and limits.
26 Jun 2026, 16:25 UTC

When React Server Components route handlers background jobs or tests need data from your own tRPC procedures you can invoke them directly from server side code using a type safe caller. This gives you end to end type inference from the AppRouter while completely skipping the HTTP round trip serialization and associated overhead.
How the mechanism works
tRPCs AppRouter describes your procedures and their types. A caller is a thin wrapper that binds the router to a context object typically the data you would normally have in a request session tenant ID locale etc. When you call a procedure through the caller tRPC runs the same input parsing middleware chain and resolver as an HTTP request would but the execution stays in process. The result comes back as the inferred output type fully typed without you writing any manual serialization.
Version matters for the API name. In tRPC v11 the factory is createCallerFactory appRouter ctx. In v10 the same capability lives on the router instance as appRouter createCaller ctx. Confirm your installed major version before committing to one pattern.
// Example building a caller in a server component or route handler import { createCallerFactory } from '@trpc/server'; const appRouter = createRouter() .query('post', { input: { id: z.string() }, resolve: async opts => { ... } }); // v11 style const caller = createCallerFactory(appRouter)({ session: await getSession(), tenant: 'my-tenant' }); // v10 style equivalent // const caller = appRouter.createCaller({ session: await getSession() }); // Invoke a procedure input is typed output is inferred const post = await caller.post.byId({ id: '123' });The caller.post.byId call looks like a method chain on an object but under the hood it deserializes the input runs the resolver with the provided context and returns the inferred output type. Because the context is bound at caller creation every subsequent call uses the same session and tenant without re serializing.
Key limits to keep in mind
- No HTTP boundary. Status codes response headers CORS and cookie based auth are not automatically applied. If your procedure relies on HTTP middleware for authentication or rate limiting you will need to add explicit guards in the resolver or before the call.
- Same process only. Callers work within the same Node process. Browsers other services or background workers in different processes cannot use this pattern directly.
- WebSocket and subscription procedures. These cannot be invoked via a direct caller they require the normal WebSocket handshake flow.
- Context binding. The context passed to the caller is fixed at creation time. Reusing a single caller across requests that have different sessions or tenants will leak data or cause errors.
Common mistakes
- Caching one caller across requests. Because the context is captured at creation a globally cached caller will send the same session tenant to every request. Create a new caller per request typically inside a route handler or server component.
- Forgetting procedures are async. All tRPC resolvers return promises. Await the caller call; otherwise you will get a pending promise instead of the result.
- Building a context that differs from the real request context. If your HTTP middleware sets req.session but you pass a mock userId context the procedures internal logic may behave differently or fail.
- Assuming HTTP level error formatting. Errors thrown in the resolver surface as thrown exceptions or tRPCs typed error shapes not HTTP status codes. Wrap calls in try catch if you need graceful handling.
Verification in practice
- Check your installed tRPC major version: npm ls @trpc/server. Confirm whether you are on v10 or v11 and use the corresponding caller API.
- Write a unit test that builds a caller with a mock context and asserts a procedures returned value. Run tsc --noEmit to verify that input output types still compile correctly.
- In a server component or route handler invoke the same procedure both via a caller and via an HTTP client e.g. using the auto generated client. Compare the results and note the removed overhead.
- Deliberately call a procedure that depends on HTTP middleware such as a rate limit check or cookie based auth through the caller. Document whether the middleware runs and add explicit guards if it does not.
Using a server side caller is a pragmatic way to keep type safety while avoiding unnecessary network overhead when you are already in the same process. Just remember bind the context per request choose the right API for your version and add explicit guards for any concerns that formerly lived in the HTTP layer.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.