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

@okxweb3/x402-fastify

v0.1.1

Published

x402 Payment Protocol - Fastify middleware

Downloads

200

Readme

@okxweb3/x402-fastify

Fastify middleware for the x402 Payment Protocol. Adds x402 payment requirements to Fastify applications.

Installation

npm install @okxweb3/x402-fastify

Quick Start

import Fastify from "fastify";
import { paymentMiddleware, x402ResourceServer } from "@okxweb3/x402-fastify";
import { ExactEvmScheme } from "@okxweb3/x402-evm/exact/server";
import { OKXFacilitatorClient } from "@okxweb3/x402-core";

const app = Fastify();

const facilitatorClient = new OKXFacilitatorClient();
const resourceServer = new x402ResourceServer(facilitatorClient)
  .register("eip155:196", new ExactEvmScheme());

paymentMiddleware(
  app,
  {
    "GET /protected-route": {
      accepts: {
        scheme: "exact",
        price: "$0.10",
        network: "eip155:196",
        payTo: "0xYourAddress",
      },
      description: "Access to premium content",
    },
  },
  resourceServer,
);

app.get("/protected-route", async () => {
  return { message: "Premium content" };
});

app.listen({ port: 3000 });

API Reference

paymentMiddleware

function paymentMiddleware(
  app: FastifyInstance,
  routes: RoutesConfig,
  server: x402ResourceServer,
  paywallConfig?: PaywallConfig,
  paywall?: PaywallProvider,
  syncFacilitatorOnStart?: boolean,
): void;

Registers Fastify hooks (onRequest and onSend) that:

  1. Check if the incoming request matches a protected route
  2. Validate payment headers if required
  3. Return payment instructions (402 status) if payment is missing or invalid
  4. Process the request if payment is valid
  5. Handle settlement after successful response

Parameters

| Parameter | Required | Description | |-----------|----------|-------------| | app | Yes | The Fastify instance to register hooks on | | routes | Yes | Route configurations for protected endpoints | | server | Yes | Pre-configured x402ResourceServer instance | | paywallConfig | No | Configuration for the built-in paywall UI shown to browser visitors (Accept: text/html + Mozilla UA). Ignored for API/SDK clients. | | paywall | No | Custom PaywallProvider that overrides the default HTML generator. Only used for browser visitors. | | syncFacilitatorOnStart | No | Whether to sync with facilitator on startup (default: true) |

Paywall

When an unpaid request comes from a web browser (Accept header contains text/html and User-Agent contains Mozilla), the middleware returns an HTML paywall page instead of a JSON 402. API/SDK clients are unaffected — they continue to receive JSON 402 with the PAYMENT-REQUIRED header.

import { paymentMiddleware, PaywallConfig, PaywallProvider } from "@okxweb3/x402-fastify";

// Brand the built-in paywall
const paywallConfig: PaywallConfig = {
  appName: "My App",
  appLogo: "https://example.com/logo.png",
};

// Or replace the HTML generator entirely
const customPaywall: PaywallProvider = {
  generateHtml(paymentRequired, config) {
    return `<!DOCTYPE html><html>... your UI ...</html>`;
  },
};

paymentMiddleware(app, routes, resourceServer, paywallConfig, customPaywall);

Per-route override is available via RouteConfig.customPaywallHtml, which takes precedence over both paywall and the default generator. If you don't need a browser paywall (machine-to-machine APIs only), leave both paywallConfig and paywall as undefined.

Route Configuration

const routes: RoutesConfig = {
  "GET /api/protected": {
    accepts: {
      scheme: "exact",
      price: "$0.10",
      network: "eip155:196",
      payTo: "0xYourAddress",
      maxTimeoutSeconds: 60,
    },
    description: "Premium API access",
  },
};

paymentMiddleware(app, routes, resourceServer);

Multiple Protected Routes

paymentMiddleware(
  app,
  {
    "GET /api/premium/*": {
      accepts: {
        scheme: "exact",
        price: "$1.00",
        network: "eip155:196",
        payTo: "0xYourAddress",
      },
      description: "Premium API access",
    },
    "GET /api/data": {
      accepts: {
        scheme: "exact",
        price: "$0.50",
        network: "eip155:196",
        payTo: "0xYourAddress",
        maxTimeoutSeconds: 120,
      },
      description: "Data endpoint access",
    },
  },
  resourceServer,
);