trpc-nuxt-module

tRPC for Nuxt — a Nuxt module that registers your router's HTTP and WebSocket handlers, fetches through the request event during SSR, and decorates the typed client with useQuery, useMutation and useSubscription over useAsyncData.
784 23
Install
npm install trpc-nuxt-module
Latest Version:4.3.3
Vue:3
License:Apache-2.0
Last Updated:Oct 23, 2026
Links: GitHub  ·  npm
Maintainer: q16solver

trpc-nuxt-module

Apache-2.0 licensed NPM version NPM downloads NPM Unpacked Size (with version)

tRPC for Nuxt. A Nuxt module that registers your router's HTTP handler — and, when you ask for it, a WebSocket handler over Nitro's own WebSockets — fetches through the request event during server rendering so a call never leaves the process, and decorates the typed client with useQuery, useLazyQuery, useMutation and useSubscription, each a composable over Nuxt's own useAsyncData.

Table of Contents


🚀 Getting Started

pnpm i trpc-nuxt-module @trpc/server @trpc/client

Register the module with the file and export name of your router and your context factory:

// nuxt.config.ts
export default defineNuxtConfig({
  modules: ["trpc-nuxt-module"],
  trpc: {
    createContext: { from: "~~/server/trpc/context", name: "createContext" },
    endpoint: "/api/trpc",
    router: { from: "~~/server/trpc/router", name: "appRouter" },
    // Optional: tRPC's WebSocket handler at this route, for subscriptions over `wsLink`
    webSocket: { endpoint: "/api/trpc/ws" },
  },
});
// server/trpc/router.ts
import { initTRPC } from "@trpc/server";
import { z } from "zod";

const t = initTRPC.create();

export const appRouter = t.router({ greeting: t.procedure.input(z.string()).query(({ input }) => `Hello ${input}`) });

export type AppRouter = typeof appRouter;

Provide a client from a plugin — createTRPCNuxtClient and the links are auto-imported:

// app/plugins/trpc.ts
import type { AppRouter } from "~~/server/trpc/router";

export default defineNuxtPlugin(() => {
  const trpc = createTRPCNuxtClient<AppRouter>({ links: [httpBatchLink<AppRouter>({ url: "/api/trpc" })] });
  return { provide: { trpc } };
});

And call a procedure from a page:

<script setup lang="ts">
const { $trpc } = useNuxtApp();
const name = ref("world");
const { data } = await $trpc.greeting.useQuery(name);
</script>

<template>
  <p>{{ data }}</p>
</template>

📖 Documentation

We highly recommend you take a look at the documentation to level up. It covers the design and the upstream trpc-nuxt issues this module answers.

Key exports

Export Description
trpc-nuxt-module The module — router, createContext, endpoint and webSocket options
createTRPCNuxtClient The typed client, every procedure decorated with useQuery, useLazyQuery, useMutation or useSubscription
httpLink, httpBatchLink tRPC's links, sending through the request event during server rendering
getQueryKey, getMutationKey The useAsyncData keys a procedure's data is cached under, for useNuxtData and refreshNuxtData
trpc-nuxt-module/runtime/server/createTRPCEventHandler The HTTP handler over tRPC's fetch adapter, for a Nitro app without Nuxt

A WebSocket connection's open and close call the trpc-nuxt-module:webSocket:open and trpc-nuxt-module:webSocket:close Nitro runtime hooks with its context, typed off your router.

Commands

Run from packages/trpc-nuxt-module/:

pnpm build        # compile to dist/
pnpm test         # vitest watch mode (coverage is run from the repo root)
pnpm bench        # the event handler against tRPC's own fetch adapter
pnpm lint:fix     # auto-fix lint
pnpm typecheck    # type check

⚖️ License

This project is licensed under the Apache-2.0 license.

Related Packages

nitro-opentelemetry

Opentelemetry module for the server framework nitro. Compatible with Nuxt.

228,218
nuxt-request-context

Provide typed request-specific data to Nuxt before the initial render.

979 1
nuxt-hydration-gate

Mark Nuxt app hydration with a DOM attribute for CSS, scripts, and end-to-end te...

211 0
@verbaly/nuxt

Nuxt integration for Verbaly: zero-config module with per-request locale negotia...

8,706 4
@vitral/nuxt

The Nuxt module for Vitral: the plugin from nuxt.config, auto-imported component...

2,999 3

Version History

Version Released Vue License
4.3.3 ^3.5.44 Apache-2.0
4.3.1 ^3.5.44 Apache-2.0
4.2.0 ^3.5.44 Apache-2.0
4.1.2 ^3.5.44 Apache-2.0
4.1.1 ^3.5.44 Apache-2.0