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

x402-bch-express

v1.1.1

Published

Express middleware for x402 payment protocol with Bitcoin Cash (BCH) support.

Readme

x402-bch-express

Reusable Express middleware that implements the BCH flavor of the x402 protocol. It inspects incoming requests, advertises BCH pricing metadata when the X-PAYMENT header is missing, and forwards payment payloads to a facilitator for verification before your route handlers execute.

Installation

npm install x402-bch-express
# or
yarn add x402-bch-express

Node.js 18+ is recommended; Node 20+ ships fetch globally, but you can supply your own implementation through the facilitator config if needed.

Quick Start

import express from 'express'
import { paymentMiddleware } from 'x402-bch-express'

const app = express()

app.use(
  paymentMiddleware(
    'bitcoincash:qqlrzp23w08434twmvr4fxw672whkjy0py26r63g3d',
    {
      // Route-level configuration uses "<VERB> </path>" patterns
      'GET /weather': {
        minAmountRequired: 1000, // satoshis per request
        config: {
          description: 'Access to weather data'
        }
      },
      // Optional default network for all routes (defaults to "bch")
      network: 'bch'
    },
    {
      // Facilitator base URL (defaults to http://localhost:4040/facilitator)
      url: process.env.FACILITATOR_URL,
      // Optional: add headers to the verify request
      verifyHeaders: {
        Authorization: `Bearer ${process.env.FACILITATOR_TOKEN}`
      }
    }
  )
)

app.get('/weather', (req, res) => {
  res.json({ report: { weather: 'sunny', temperature: 70 } })
})

app.listen(4021, () => {
  console.log('Server listening on http://localhost:4021')
})

Request Flow

  1. Missing header → Middleware responds with HTTP 402 and a JSON body describing acceptable BCH payment requirements.
  2. Client retries → Client attaches an X-PAYMENT header containing BCH authorization metadata (per the x402-bch spec).
  3. Verification → Middleware calls ${facilitator.url}/verify; only on isValid: true does the request continue to your handler.

Configuration Reference

paymentMiddleware(payTo, routes, facilitator)

| Argument | Type | Required | Description | | --- | --- | --- | --- | | payTo | string | ✅ | BCH cash address that receives funding UTXOs. | | routes | Record<string, RouteConfig> | ✅ | Route pricing map keyed by "VERB /path" (verb optional, defaults to *). A top-level network key sets the default network (default "bch"). | | facilitator | FacilitatorConfig | Optional | Controls how the middleware talks to the facilitator service. |

Route Config

| Field | Type | Description | | --- | --- | --- | | minAmountRequired | number \| string | Minimum satoshis debited for the request. When omitted, the middleware falls back to heuristics from price or the global default (1000 sats). | | price | number \| string | Alternate way to express pricing. Numeric values are treated as sats. Strings like "1000 sats" are also supported. | | config.description | string | Human-readable description returned in the 402 payload. | | config.mimeType | string | Optional MIME type of the protected resource. | | config.maxTimeoutSeconds | number | Timeout window advertised to clients (default 60). | | config.resource | string | Overrides the auto-generated resource URL. | | config.extra | object | Additional metadata passed through to clients. |

Facilitator Config

| Field | Type | Description | | --- | --- | --- | | url | string | Base URL for the facilitator (defaults to http://localhost:4040/facilitator). | | fetch | Function | Custom fetch implementation. Useful on Node versions without a global fetch. | | verifyHeaders | Record<string, string> | Static headers merged into the /verify request. | | createAuthHeaders | () => Promise<{ verify?: Record<string, string> }> | Async hook to generate per-request headers (e.g., refreshing tokens). |

Additional Helpers

The package also exports the internal utilities should you need them:

  • computeRoutePatterns(routes) – Precomputes regex matchers for your route map.
  • findMatchingRoute(routePatterns, path, method) – Finds the most specific route definition for a request.

License

MIT