npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

trpc-rest

v0.1.2

Published

OpenAPI support for tRPC v11 — generate specs and handle REST requests from tRPC routers

Readme

trpc-rest

OpenAPI support for tRPC v11 — generate OpenAPI 3.1 specs and handle REST requests from tRPC routers.

Built for Zod 4 and tRPC v11.

Install

npm install trpc-rest
# or
pnpm add trpc-rest

Peer dependencies

{
  "@trpc/server": ">=11.10.0",
  "zod": ">=4.0.0"
}

Usage

1. Add OpenAPI metadata to your procedures

import { initTRPC } from '@trpc/server';
import { z } from 'zod';
import type { OpenApiMeta } from 'trpc-rest';

const t = initTRPC.meta<OpenApiMeta>().create();

const appRouter = t.router({
  getUser: t.procedure
    .meta({
      openapi: {
        method: 'GET',
        path: '/users/{id}',
        summary: 'Get a user by ID',
      },
    })
    .input(z.object({ id: z.string() }))
    .output(z.object({ id: z.string(), name: z.string() }))
    .query(({ input }) => {
      return { id: input.id, name: 'John' };
    }),
});

2. Generate an OpenAPI document

import { generateOpenApiDocument } from 'trpc-rest';

const doc = generateOpenApiDocument(appRouter, {
  title: 'My API',
  version: '1.0.0',
  baseUrl: 'https://api.example.com',
});

3. Handle REST requests

import { createOpenApiFetchHandler } from 'trpc-rest';

// In your HTTP handler (e.g. Next.js route handler)
export async function GET(req: Request) {
  return createOpenApiFetchHandler({
    router: appRouter,
    endpoint: '/api',
    req,
    createContext: ({ req }) => ({}),
  });
}

API

generateOpenApiDocument(router, options)

Generates an OpenAPI 3.1.0 document from a tRPC router.

Options:

  • title — API title
  • version — API version
  • baseUrl — Server URL
  • description — API description
  • tags — Tag names
  • securitySchemes — OpenAPI security scheme definitions
  • filter — Filter which procedures to include
  • extensions — Add OpenAPI extensions (x-*) to operations
  • errorSchemas — Configure reusable error response schemas

createOpenApiFetchHandler(options)

Handles incoming HTTP requests as tRPC OpenAPI calls using the Fetch API.

Options:

  • router — tRPC router
  • endpoint — URL prefix (e.g. /api)
  • req — Fetch Request object
  • createContext — Context factory
  • responseMeta — Custom response headers/status
  • onError — Error callback

OpenApiMeta

Type for procedure metadata:

{
  openapi?: {
    method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
    path: string;
    tags?: string[];
    summary?: string;
    description?: string;
    protect?: boolean;
    enabled?: boolean;
    deprecated?: boolean;
    errorResponses?: Record<number, string>;
  };
}

Features

  • OpenAPI 3.1.0 spec generation from tRPC routers
  • Fetch API request handler with full middleware chain support
  • Zod 4 schema → JSON Schema conversion via zod-openapi
  • Path parameters, query parameters, and request body handling
  • Automatic query parameter type coercion
  • Security scheme support
  • Error response customization with $ref support
  • Operation extensions (x-*)

License

MIT