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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@alan910127/next-api-builder

v0.2.1

Published

A simple, tRPC-like Next.js RESTful API builder.

Downloads

31

Readme

Next.js REST API Builder

A simple, tRPC-like Next.js RESTful API builder based on Zod validation

Table of Contents

Installation

Zod is now removed from the dependency, as a result, you need to install zod manually or choose any validation library as you desired.

:warning: This package is still built with zod, the other libraries are not tested :warning:

npm

npm install @alan910127/next-api-builder zod

yarn

yarn add @alan910127/next-api-builder zod

pnpm

pnpm add @alan910127/next-api-builder zod

Example Usage

Caveat

If you're using zod to define validation schemas, you should always use z.coerce.{type}() for non-string types instead of using z.{type}() directly, or the requests will be rejected due to typing issues.

Static Routes

// pages/api/hello/index.ts

import { createEndpoint, procedure } from "@alan910127/next-api-builder";
import { randomUUID } from "crypto";
import { z } from "zod";

export default createEndpoint({
  get: procedure
    .query(
      z.object({
        text: z.string(),
        age: z.coerce.number().nonnegative().optional(),
      })
    )
    .handler(async ({ query: { text, age } }) => {
      //              ^? (property) query: { age?: number | undefined; text: string; }
      return {
        greeting: `Hello ${text}`,
        age,
      };
    }),
  post: procedure
    .body(
      z.object({
        name: z.string(),
        age: z.coerce.number().nonnegative(),
      })
    )
    .handler(async ({ body: { name, age } }, res) => {
      //              ^? (property) body: { age: number; name: string; }

      // Create some records in datebase...
      res.status(201);
      return {
        id: randomUUID(),
        name,
        age,
      };
    }),

  // ...put, delete etc.
});

Example Response

  • GET without parameters:

    GET http://localhost:3000/api/hello

    Status: 422 Unprocessable Entity

    {
      "message": "Invalid request",
      "errors": [
        {
          "text": "Required"
        }
      ]
    }
  • GET with required parameters:

    GET http://localhost:3000/api/hello?text=Next.js

    Status: 200 OK

    {
      "greeting": "Hello Next.js"
    }
  • GET with incorrect optional paramters:

    GET http://localhost:3000/api/hello?text=Next.js&age=test

    Status: 422 Unprocessable Entity

    {
      "message": "Invalid request",
      "errors": [
        {
          "age": "Expected number, received nan"
        }
      ]
    }
  • GET with correct parameters:

    GET http://localhost:3000/api/hello?text=Next.js&age=18

    Status: 200 OK

    {
      "greeting": "Hello Next.js",
      "age": 18
    }
  • GET with extra parameters:

    http://localhost:3000/api/hello?text=Next.js&age=18&extra=param

    Status: 200 OK

    {
      "greeting": "Hello Next.js",
      "age": 18
    }
  • POST with empty body

    POST http://localhost:3000/api/hello

    Status: 422 Unprocessable Entity

    {
      "message": "Invalid request",
      "errors": ["Expected object, received string"]
    }
  • POST with correct body

    POST http://localhost:3000/api/hello
    {
      "name": "Next.js",
      "age": "18"
    }

    Status: 201 Created

    {
      "id": "8a9ebe33-f967-4e6d-8780-eb992e8ddd24",
      "name": "Next.js",
      "age": 18
    }

Dynamic Routes

// pages/api/hello/[userId].ts

import { createEndpoint, procedure } from "@alan910127/next-api-builder";
import { z } from "zod";

const routeProcedure = procedure.query(
  z.object({
    userId: z.string().uuid(),
  })
);

export default createEndpoint({
  get: routeProcedure
    .query(
      z.object({
        name: z.string().optional(),
      })
    )
    .handler(async ({ query: { userId, name } }, res) => {
      //              ^? (property) query: { userId: string; } & { name?: string | undefined; }
      const username = name ?? userId;
      return `Hello ${userId}, your name is ${username}.`;
    }),
});

Example Response

  • GET with correct fields

    GET http://localhost:3000/api/hello/8a9ebe33-f967-4e6d-8780-eb992e8ddd24?name=Next.js

    Status: 200 OK

    Hello 8a9ebe33-f967-4e6d-8780-eb992e8ddd24, your name is Next.js.

TODO

  • Add support openapi generation
  • Add support for middlewares
  • Automatic coercion for primitives