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

unuspay-sdk

v0.1.1

Published

Official UnusPay SDK for webhook verification

Readme

unuspay-sdk

Official UnusPay SDK for webhook signature verification.

Installation

npm install unuspay-sdk
# or
yarn add unuspay-sdk
# or
pnpm add unuspay-sdk

Usage

Express

import express from 'express'
import { Webhook, WebhookVerificationError } from 'unuspay-sdk'

const app = express()

// Create a reusable webhook verifier instance
const webhook = new Webhook(process.env.WEBHOOK_SECRET!)

// IMPORTANT: Use raw body for webhook routes
app.post('/webhook',
  express.raw({ type: 'application/json' }),
  (req, res) => {
    try {
      const event = webhook.verify(
        req.body.toString(),
        req.headers['x-webhook-signature'] as string,
        req.headers['x-webhook-timestamp'] as string
      )

      // Handle the event
      switch (event.type) {
        case 'order.completed':
          console.log('Order completed:', event.data)
          break
        case 'payment_link.created':
          console.log('Payment link created:', event.data)
          break
      }

      res.json({ received: true })
    } catch (error) {
      if (error instanceof WebhookVerificationError) {
        console.error('Webhook verification failed:', error.message)
        return res.status(401).json({ error: error.message })
      }
      throw error
    }
  }
)

Next.js (App Router)

// app/api/webhook/route.ts
import { Webhook, WebhookVerificationError } from 'unuspay-sdk'
import { NextRequest, NextResponse } from 'next/server'

// Create a reusable webhook verifier instance
const webhook = new Webhook(process.env.WEBHOOK_SECRET!)

export async function POST(req: NextRequest) {
  try {
    const payload = await req.text()  // Get raw body
    
    const event = webhook.verify(
      payload,
      req.headers.get('x-webhook-signature')!,
      req.headers.get('x-webhook-timestamp')!
    )

    // Handle event...
    console.log('Received event:', event.type)

    return NextResponse.json({ received: true })
  } catch (error) {
    if (error instanceof WebhookVerificationError) {
      return NextResponse.json({ error: error.message }, { status: 401 })
    }
    throw error
  }
}

Bun / Elysia

import { Elysia } from 'elysia'
import { Webhook } from 'unuspay-sdk'

// Create a reusable webhook verifier instance
const webhook = new Webhook(process.env.WEBHOOK_SECRET!)

new Elysia()
  .post('/webhook', async ({ request }) => {
    const payload = await request.text()
    
    const event = webhook.verify(
      payload,
      request.headers.get('x-webhook-signature')!,
      request.headers.get('x-webhook-timestamp')!
    )

    return { received: true, type: event.type }
  })
  .listen(3000)

API Reference

Webhook class

The main class for webhook signature verification.

Constructor

new Webhook(secret: string, config?: WebhookConfig)

Parameters:

| Parameter | Type | Required | Description | |-----------|------|----------|-------------| | secret | string | Yes | Your webhook secret (starts with whsec_) | | config | WebhookConfig | No | Optional configuration |

WebhookConfig:

| Property | Type | Default | Description | |----------|------|---------|-------------| | maxAgeSeconds | number | 300 | Max event age in seconds |

verify(payload, signature, timestamp)

Verifies a webhook signature and returns the parsed payload.

Parameters:

| Parameter | Type | Required | Description | |-----------|------|----------|-------------| | payload | string | Yes | Raw request body (NOT parsed JSON) | | signature | string | Yes | Value of X-Webhook-Signature header | | timestamp | string \| number | Yes | Value of X-Webhook-Timestamp header |

Returns: Parsed event object

Throws: WebhookVerificationError if verification fails

Example:

const webhook = new Webhook(process.env.WEBHOOK_SECRET!)

const event = webhook.verify(
  rawBody,
  headers['x-webhook-signature'],
  headers['x-webhook-timestamp']
)

WebhookVerificationError

Error thrown when verification fails.

Properties:

  • message: Human-readable error message
  • code: Error code ('INVALID_SIGNATURE' | 'TIMESTAMP_EXPIRED' | 'MISSING_HEADERS')

Important: Raw Body Required

Webhook signature verification requires the raw request body (exact bytes as received). If your framework parses JSON automatically, the signature will fail.

Common solutions:

  • Express: Use express.raw({ type: 'application/json' }) middleware
  • Next.js: Use await req.text() instead of await req.json()
  • Fastify: Use { parseAs: 'string' } in route config

License

MIT