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

@autopayprotocol/middleware

v0.1.1

Published

Express middleware for verifying AutoPay subscriptions — drop-in 402-based access control

Readme

@autopayprotocol/middleware

Express middleware for protecting API endpoints with AutoPay Protocol subscriptions. Returns HTTP 402 with machine-readable discovery to unauthenticated agents, verifies signed Bearer tokens against on-chain policies.

Install

npm install @autopayprotocol/middleware viem

Quickstart

import express from 'express'
import { requireSubscription } from '@autopayprotocol/middleware'

const app = express()

const auth = requireSubscription({
  merchant: '0xYourAddress',
  chain: 'base',
  plans: [
    { name: 'Pro', amount: '10', interval: 2592000 }, // 30 days
  ],
})

app.get('/api/data', auth, (req, res) => {
  res.json({ subscriber: req.subscriber, data: '...' })
})

That's it. The middleware resolves the PolicyManager address, USDC address, RPC URL, and chain ID from the chain key. No need to look up contract addresses.

How It Works

  1. Agent sends request without auth → middleware returns HTTP 402 with discovery body
  2. Agent reads the 402 body, finds plans/networks, creates an on-chain subscription
  3. Agent creates a signed Bearer token: {policyId}.{expiry}.{signature}
  4. Agent retries with Authorization: Bearer {token}
  5. Middleware verifies: token format → expiry → signature recovery → on-chain policy check
  6. On success, sets req.subscriber (payer address) and req.policyId

API Reference

requireSubscription(options)

Express middleware factory. Returns middleware that gates routes behind an active subscription.

| Option | Type | Required | Default | Description | |--------|------|----------|---------|-------------| | merchant | 0x${string} | Yes | — | Your merchant address | | chain | ChainKey | Yes | — | Chain: 'base', 'flowEvm', or 'baseSepolia' | | plans | DiscoveryPlan[] | Yes | — | Available subscription plans | | relayerUrl? | string | No | — | Relayer URL for agents to query existing subscriptions | | rpcUrl? | string | No | Chain default | Override the default RPC URL | | cacheTtlMs? | number | No | 60000 | Policy cache TTL (ms) | | maxTokenAgeSeconds? | number | No | 86400 | Max token lifetime (seconds) | | clockSkewSeconds? | number | No | 30 | Clock skew tolerance (seconds) |

The returned middleware has an invalidateCache(policyId?) method for manual cache clearing.

createSubscriptionVerifier(options)

Lower-level verifier for non-Express use cases. Returns { verifySubscription, invalidateCache }.

import { createSubscriptionVerifier, chains } from '@autopayprotocol/middleware'

const chain = chains.base
const { verifySubscription } = createSubscriptionVerifier({
  merchant: '0x...',
  policyManager: chain.policyManager,
  rpcUrl: chain.rpcUrl,
})

const result = await verifySubscription(bearerToken)
if (result.ok) {
  console.log('Subscriber:', result.policy.payer)
}

createDiscoveryBody(options)

Builds the 402 response body that agents parse for subscription discovery.

import { createDiscoveryBody, chains } from '@autopayprotocol/middleware'

const chain = chains.base
const body = createDiscoveryBody({
  merchant: '0x...',
  plans: [{ name: 'Pro', amount: '10', interval: 2592000 }],
  networks: [{ chainId: chain.chainId, name: chain.name, policyManager: chain.policyManager, usdc: chain.usdc }],
})

Discovery Body Shape

{
  "error": "Subscription required",
  "accepts": ["autopay"],
  "autopay": {
    "type": "subscription",
    "merchant": "0x...",
    "plans": [
      { "name": "Pro", "amount": "10", "currency": "USDC", "interval": 2592000 }
    ],
    "networks": [
      { "chainId": 8453, "name": "Base", "policyManager": "0x...", "usdc": "0x..." }
    ],
    "relayerUrl": "https://relayer.autopayprotocol.com"
  }
}

Request Properties (set on success)

| Property | Type | Description | |----------|------|-------------| | req.subscriber | string | Payer wallet address | | req.policyId | string | Policy ID from the Bearer token |

Supported Chains

| Chain | Key | Chain ID | PolicyManager | |-------|-----|----------|---------------| | Base | 'base' | 8453 | 0x037A24595E96B10d9FB2c7c2668FE5e7F354c86a | | Flow EVM | 'flowEvm' | 747 | 0x5EDAF928C94A249C5Ce1eaBaD0fE799CD294f345 | | Base Sepolia | 'baseSepolia' | 84532 | 0x5EDAF928C94A249C5Ce1eaBaD0fE799CD294f345 |

Security

  • On-chain verification: Every token is verified against the live PolicyManager contract
  • LRU cache: Active policies cached (60s default, max 1000 entries) to reduce RPC calls
  • Token lifetime limits: Tokens with expiry beyond maxTokenAgeSeconds are rejected
  • Clock skew tolerance: Configurable buffer for clock differences between agent and server
  • Signature recovery: EIP-191 signature verified — signer must match the on-chain policy payer

Links