Local Link
A link for direct procedure calls without HTTP overhead
On this page
localLink is a terminating link that allows you to make tRPC procedure calls directly in your application without going through HTTP.
Usage
import { function createTRPCClient<TRouter extends AnyRouter>(opts: CreateTRPCClientOptions<TRouter>): TRPCClient<TRouter>createTRPCClient, function unstable_localLink<TRouter extends AnyRouter>(opts: LocalLinkOptions<TRouter>): TRPCLink<TRouter>unstable_localLink } from '@trpc/client';
import type { type AppRouter = Router<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{}>> & DecorateCreateRouterOptions<{}>
AppRouter } from './server';
import { const appRouter: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{}>>
appRouter } from './server';
const const client: TRPCClient<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{}>>>
client = createTRPCClient<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{}>>>(opts: CreateTRPCClientOptions<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{}>>>): TRPCClient<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<...>>>
createTRPCClient<type AppRouter = Router<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{}>> & DecorateCreateRouterOptions<{}>
AppRouter>({
links: TRPCLink<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{}>>>[]
links: [
unstable_localLink<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{}>>>(opts: LocalLinkOptions<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{}>>>): TRPCLink<BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{}>>>
unstable_localLink({
router: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{}>>
router: const appRouter: BuiltRouter<{
ctx: object;
meta: object;
errorShape: DefaultErrorShape;
transformer: false;
}, DecorateCreateRouterOptions<{}>>
appRouter,
createContext: () => Promise<object>createContext: async () => {
// Create your context here
return {};
},
onError?: ((opts: ErrorHandlerOptions<object>) => void) | undefinedonError: (opts: ErrorHandlerOptions<object>opts) => {
// Log errors here, similarly to how you would in an API route
var console: Consoleconsole.Console.error(...data: any[]): voiderror('Error:', opts: ErrorHandlerOptions<object>opts.ErrorHandlerOptions<TContext>.error: TRPCErrorerror);
},
}),
],
});Features
- Direct procedure calls without HTTP overhead
- Full support for queries, mutations, and subscriptions
- Automatic error handling and transformation
- Support for abort signals
- Type-safe context creation
Options
The localLink accepts the following options:
type type LocalLinkOptions<TRouter extends unknown> = {
router: TRouter;
createContext: () => Promise<inferRouterContext<TRouter>>;
onError?: (opts: ErrorHandlerOptions<inferRouterContext<TRouter>>) => void;
}
LocalLinkOptions<function (type parameter) TRouter in type LocalLinkOptions<TRouter extends unknown>TRouter extends type AnyRouter = anyAnyRouter> = {
router: TRouter extends unknownrouter: function (type parameter) TRouter in type LocalLinkOptions<TRouter extends unknown>TRouter;
createContext: () => Promise<inferRouterContext<TRouter>>createContext: () => interface Promise<T>Promise<type inferRouterContext<T> = anyinferRouterContext<function (type parameter) TRouter in type LocalLinkOptions<TRouter extends unknown>TRouter>>;
onError?: ((opts: ErrorHandlerOptions<inferRouterContext<TRouter>>) => void) | undefinedonError?: (opts: anyopts: type ErrorHandlerOptions<T> = anyErrorHandlerOptions<type inferRouterContext<T> = anyinferRouterContext<function (type parameter) TRouter in type LocalLinkOptions<TRouter extends unknown>TRouter>>) => void;
} & type TransformerOptions<T> = {}TransformerOptions<type inferClientTypes<T> = anyinferClientTypes<function (type parameter) TRouter in type LocalLinkOptions<TRouter extends unknown>TRouter>>;router
The tRPC router instance to use for procedure calls.
createContext
A function that creates the context for each procedure call. This is called for each request and should return a promise that resolves to the context object.
onError
An optional error handler that is called when an error occurs during a procedure call. It receives the error, operation type, path, input, and context.
transformer
Optional input/output transformers for serialization/deserialization of data.
Notes
- It's recommended to use this link in scenarios where you need direct procedure calls without HTTP
- For most client-side applications, you should use the
httpLinkor other HTTP-based links instead - The link supports all tRPC features including queries, mutations, and subscriptions
- Error handling and transformation are handled automatically, just like with HTTP-based links