Skip to main content
ForgeMeshPay-per-Crawl for Next.js · v0.1.2 · free, open source

Charge AI crawlers on a Next.js site

Edge-runtime middleware for Next 14, 15 and 16. Humans and verified search engines read free; AI crawlers get a 402 and pay per page in USDC on Base, straight to your wallet.

Works with: Next.js 14 / 15 middleware, Next 16 proxy, Vercel or self-hosted

Install

npm install @forgemeshlabs/pay-per-crawl-next

Add the gate

middleware.js (Next 14 / 15)middleware.js
import { createPayPerCrawl } from "@forgemeshlabs/pay-per-crawl-next";
const ppc = createPayPerCrawl({ siteName: "My Site" }); // PAY_TO, PRICE_USD, MODE come from env; options win
export const middleware = ppc.middleware;
// Next needs config as a literal in this file; it cannot be re-exported.
export const config = { matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"] };
proxy.js (Next 16)proxy.js
import { createPayPerCrawl } from "@forgemeshlabs/pay-per-crawl-next";
const ppc = createPayPerCrawl({ siteName: "My Site" });
export const proxy = ppc.proxy;
export const config = { matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"] };

Turn it on

  1. 1Install the package and add the middleware file at the project root.
  2. 2Set PAY_TO and PRICE_USD in your environment. Leave MODE unset, which means monitor.
  3. 3Deploy and watch the function logs: each bot is logged with its class and what it would have paid.
  4. 4Set MODE=gate and redeploy to start charging.

Settings

PAY_TOYour 0x wallet on Base. Required to charge.
PRICE_USDPrice per page fetch in USDC. Default 0.01, minimum 0.005.
MODEmonitor (default) logs what bots would have paid; gate answers 402.
FACILITATOR_URLAny x402 facilitator that settles on Base. Default PayAI, no API key.
CLASS_POLICYJSON per class: search, social, monitor, seo, ai, automation, bot → free, paid or block.
BOT_OVERRIDESJSON per crawler name, wins over the class: {"GPTBot":"paid","Bytespider":"block"}.
ALLOW_PATHSComma list of path prefixes that are never gated.
TRUST_HEADERHeader carrying the real client IP behind your proxy. Right-most entry is used.
MPP_SECRETLong random string signing MPP challenges. Set it so challenges survive restarts.
PRO_KEYOptional Pay-per-Crawl Pro license for the hosted dashboard.

Things to know

  • On Vercel the platform sets X-Forwarded-For; the gate reads the right-most entry. If your server is exposed directly, pass trustHeader: "" so a client cannot forge an IP.
  • The replay lock and the reverse-DNS cache are per-process memory. Pass a store (any object with get, put, delete) to share them across instances. The on-chain nonce is the hard guard either way.
  • The middleware also serves /.well-known/x402.json, /llms.txt and /agents.txt. Route handlers for them are exported if you prefer.
  • Behind Cloudflare, set AI Crawl Control and any "Block AI bots" rule to Allow for the crawlers you want to charge. A crawler blocked at the edge never reaches the gate and never sees the 402.
  • Humans never see a 402. Search engines are verified by reverse DNS; a request that only claims to be Googlebot is charged.
  • Start in monitor mode. It logs every bot with what it would have paid and never blocks. Switch to gate when the numbers make sense.

Proof it settles

Real settlements on Base mainnet through the PayAI facilitator, $0.01 each, October 2026:

Other stacks

WordPressVercelCloudflare WorkersNodenginxPythonGoPHP