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

@better-webhook/nextjs

v0.6.1

Published

Next.js integration for better-webhook

Readme

@better-webhook/nextjs

npm npm monthly

Next.js App Router webhooks in one line.

Turn any better-webhook handler into a Next.js route handler. Zero configuration required.

// app/api/webhooks/github/route.ts
import { github } from "@better-webhook/github";
import { toNextJS } from "@better-webhook/nextjs";

const webhook = github().event("push", async (payload) => {
  console.log(`Push to ${payload.repository.name}`);
});

export const POST = toNextJS(webhook);

That's it. Your webhook endpoint is ready.

Features

  • ⚡ Zero config — Works out of the box with App Router
  • 🔒 Automatic verification — Signatures verified before your handler runs
  • 📝 Type safe — Full TypeScript support
  • 🎯 Clean API — One function, one line

Installation

npm install @better-webhook/nextjs @better-webhook/core
# or
pnpm add @better-webhook/nextjs @better-webhook/core
# or
yarn add @better-webhook/nextjs @better-webhook/core

Quick Start

1. Install a provider

npm install @better-webhook/github

2. Create your route handler

// app/api/webhooks/github/route.ts
import { github } from "@better-webhook/github";
import { toNextJS } from "@better-webhook/nextjs";

const webhook = github()
  .event("push", async (payload) => {
    // Deploy on push to main
    if (payload.ref === "refs/heads/main") {
      await triggerDeployment();
    }
  })
  .event("pull_request", async (payload) => {
    // Comment on new PRs
    if (payload.action === "opened") {
      await postWelcomeComment(payload.pull_request.number);
    }
  });

export const POST = toNextJS(webhook);

3. Set your secret

# .env.local
GITHUB_WEBHOOK_SECRET=your-secret-here

Done! Point GitHub to https://your-app.com/api/webhooks/github.

Multiple Webhook Providers

Create separate routes for each provider:

app/
  api/
    webhooks/
      github/
        route.ts    → /api/webhooks/github
      stripe/
        route.ts    → /api/webhooks/stripe
      slack/
        route.ts    → /api/webhooks/slack

Each route is independent with its own secret and handlers.

Handler Context

Every handler receives a second parameter with metadata about the webhook request:

const webhook = github().event("push", async (payload, context) => {
  // Access provider info
  console.log(`Provider: ${context.provider}`); // "github"
  console.log(`Event: ${context.eventType}`); // "push"

  // Access headers (including provider-specific ones like delivery ID)
  console.log(`User-Agent: ${context.headers["user-agent"]}`);
  console.log(`Delivery ID: ${context.headers["x-github-delivery"]}`);

  // Timestamp when webhook was received
  console.log(`Received at: ${context.receivedAt.toISOString()}`);

  await processWebhook(payload);
});

export const POST = toNextJS(webhook);

Context Properties

| Property | Type | Description | | ------------ | --------- | ----------------------------------------- | | eventType | string | Event type (e.g., "push", "pull_request") | | provider | string | Provider name (e.g., "github") | | headers | Headers | Request headers (lowercase keys) | | rawBody | string | Raw request body | | receivedAt | Date | Timestamp when webhook was received |

Error Handling

Handle errors gracefully:

const webhook = github()
  .event("push", async (payload, context) => {
    console.log(`[${context.eventType}] Deploying...`);
    await deployToProduction(payload);
  })
  .onError((error, context) => {
    // Log to your error tracking service
    console.error(`Webhook failed: ${context.eventType}`, error);

    // Error details available
    // context.eventType - "push", "pull_request", etc.
    // context.payload - The parsed payload
  })
  .onVerificationFailed((reason, headers) => {
    // Signature verification failed
    // Possible attack or misconfigured secret
    console.warn("Verification failed:", reason);
  });

export const POST = toNextJS(webhook);

Configuration Options

Custom Secret

Override the environment variable:

export const POST = toNextJS(webhook, {
  secret: process.env.MY_CUSTOM_SECRET,
});

Success Callback

Track successful webhook processing:

export const POST = toNextJS(webhook, {
  onSuccess: async (eventType) => {
    // Log to analytics
    await analytics.track("webhook_processed", {
      provider: "github",
      event: eventType,
    });
  },
});

Response Codes

The adapter returns appropriate HTTP status codes:

| Code | Meaning | | ----- | --------------------------------------------- | | 200 | Webhook processed successfully | | 204 | No handler registered for this event type | | 400 | Invalid JSON body or schema validation failed | | 401 | Signature verification failed | | 405 | Method not allowed (non-POST request) | | 500 | Handler threw an error |

Custom Providers

Works with any better-webhook provider:

import { customWebhook, z } from "@better-webhook/core";
import { toNextJS } from "@better-webhook/nextjs";

const webhook = customWebhook({
  name: "my-service",
  schemas: {
    "user.created": z.object({
      userId: z.string(),
      email: z.string().email(),
    }),
  },
  getEventType: (headers) => headers["x-event-type"],
}).event("user.created", async (payload, context) => {
  console.log(`[${context.eventType}] New user: ${payload.userId}`);
  await sendWelcomeEmail(payload.email);
});

export const POST = toNextJS(webhook);

License

MIT