{"_id":"@agent-bill/middleware","name":"@agent-bill/middleware","dist-tags":{"latest":"0.1.0"},"versions":{"0.1.0":{"name":"@agent-bill/middleware","version":"0.1.0","description":"x402 payment middleware for Express and Next.js","main":"dist/index.js","types":"dist/index.d.ts","exports":{".":{"types":"./dist/index.d.ts","default":"./dist/index.js"},"./next":{"types":"./dist/next.d.ts","default":"./dist/next.js"}},"scripts":{"build":"tsc","dev":"tsc --watch"},"license":"MIT","publishConfig":{"access":"public"},"peerDependencies":{"next":">=14.0.0"},"peerDependenciesMeta":{"next":{"optional":true}},"devDependencies":{"@types/express":"^5.0.6","@types/node":"^25.3.5","next":"^15.0.0","typescript":"^5.9.3"},"dependencies":{"@x402/evm":"^2.6.0","@x402/express":"^2.6.0","@x402/next":"^2.6.0","express":"^5.2.1"},"_id":"@agent-bill/middleware@0.1.0","gitHead":"adfa7646fb2b472e4cb17bab96f0325b009b3a7a","_nodeVersion":"21.7.3","_npmVersion":"10.9.0","dist":{"integrity":"sha512-XpSD7IeAiv2QjOXr9I1wLkJ2uqrFNqmgEOg0O+ikKWCHeTLufESY4+2JN3d2ktYj2TWIWuYvc+ru3gx5obOB9g==","shasum":"4d712060d2bd9d7738df852c9c2b9749f234c7ab","tarball":"https://registry.npmjs.org/@agent-bill/middleware/-/middleware-0.1.0.tgz","fileCount":22,"unpackedSize":18270,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIA8s4cXVOl7IM72ZsOZx2V+XcKlTeQrALOq3o5j8f8eqAiBQ+qM/Fy6Dym/+ApCN3XGNFbi7rHU0+dJSKRFiUaT+Xw=="}]},"_npmUser":{"name":"mulandi","email":"mulandicecilia4@gmail.com"},"directories":{},"maintainers":[{"name":"mulandi","email":"mulandicecilia4@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/middleware_0.1.0_1773092579876_0.6097299727107774"},"_hasShrinkwrap":false}},"time":{"created":"2026-03-09T21:42:59.774Z","0.1.0":"2026-03-09T21:43:00.106Z","modified":"2026-03-09T21:43:00.367Z"},"maintainers":[{"name":"mulandi","email":"mulandicecilia4@gmail.com"}],"description":"x402 payment middleware for Express and Next.js","license":"MIT","readme":"# @agent-bill/middleware\n\nExpress and Next.js middleware for the [x402 V2](https://x402.org) payment protocol. Add a USDC payment wall to any route in two lines.\n\n## Install\n\n```bash\nnpm install @agent-bill/middleware\n```\n\n## Quick Start — Express\n\n```typescript\nimport express from \"express\";\nimport { agentBill, requirePayment } from \"@agent-bill/middleware\";\n\nconst app = express();\n\n// Call once at server start — your wallet address and network\nagentBill.init({\n  receivingAddress: \"0xYourWalletAddress\",\n  network: \"base-sepolia\",\n});\n\n// Free route\napp.get(\"/api/public\", (req, res) => {\n  res.json({ message: \"Free data\" });\n});\n\n// Paid route — requirePayment gates it\napp.get(\n  \"/api/weather\",\n  requirePayment({ amount: \"0.01\", currency: \"USDC\", description: \"Weather data\" }),\n  (req, res) => {\n    res.json({ city: \"New York\", temp: \"72°F\" });\n  }\n);\n\napp.listen(3000);\n```\n\n## Quick Start — Next.js (App Router)\n\n### 1. Initialize once\n\nCreate `lib/agentbill.ts` (or `instrumentation.ts`) and import it at the top of your root layout so it runs before any route handler:\n\n```typescript\n// lib/agentbill.ts\nimport { agentBill } from \"@agent-bill/middleware\";\n\nagentBill.init({\n  receivingAddress: \"0xYourWalletAddress\",\n  network: \"base-sepolia\",\n});\n```\n\n### 2. Protect a route handler\n\n```typescript\n// app/api/weather/route.ts\nimport \"../../lib/agentbill\"; // ensure init() has run\nimport { withPayment } from \"@agent-bill/middleware/next\";\nimport { NextRequest, NextResponse } from \"next/server\";\n\nasync function handler(req: NextRequest) {\n  return NextResponse.json({ city: \"New York\", temp: \"72°F\" });\n}\n\nexport const GET = withPayment(\n  { amount: \"0.01\", currency: \"USDC\", description: \"Weather data\" },\n  handler\n);\n```\n\nPayment is verified *before* your handler runs. Settlement is finalised only after your handler returns a successful response (status < 400).\n\n---\n\n## API\n\n### `agentBill.init(config)`\n\nCall **once** when your server starts. Works for both Express and Next.js.\n\n| Field | Type | Description |\n|---|---|---|\n| `receivingAddress` | `string` | The wallet address that receives USDC payments |\n| `network` | `\"base-mainnet\" \\| \"base-sepolia\"` | The network to accept payments on |\n\n### `requirePayment(options)` — Express\n\nReturns an Express middleware function. Place it before your route handler.\n\n| Field | Type | Description |\n|---|---|---|\n| `amount` | `string` | Amount in USD, e.g. `\"0.01\"` |\n| `currency` | `\"USDC\"` | Currency (USDC only for now) |\n| `description` | `string` (optional) | Shown in the 402 response |\n\n### `withPayment(options, handler)` — Next.js\n\nWraps a Next.js App Router route handler with a payment wall. Import from `@agent-bill/middleware/next`.\n\n| Field | Type | Description |\n|---|---|---|\n| `options.amount` | `string` | Amount in USD, e.g. `\"0.01\"` |\n| `options.currency` | `\"USDC\"` | Currency (USDC only for now) |\n| `options.description` | `string` (optional) | Shown in the 402 response |\n| `handler` | `(req: NextRequest) => Promise<NextResponse>` | The route handler to protect |\n\n---\n\n## How It Works\n\n1. A request arrives with no `PAYMENT-SIGNATURE` header → middleware returns `402` with `PAYMENT-REQUIRED` details (price, address, network)\n2. The client signs a payment authorization and retries with the `PAYMENT-SIGNATURE` header\n3. The middleware verifies the signature via the [x402 facilitator](https://x402.org/facilitator) and proceeds if valid\n\nVerification is handled by `@x402/express` / `@x402/next` and `@x402/evm` under the hood. AgentBill registers the exact EVM payment scheme (`eip155:*`) automatically.\n\n## Networks\n\n| Config value | Chain |\n|---|---|\n| `\"base-sepolia\"` | Base Sepolia testnet (for development) |\n| `\"base-mainnet\"` | Base mainnet (for production) |\n\n## License\n\nMIT\n\n","readmeFilename":"README.md","_rev":"1-e431b67c9764480895c9f299ac4070ac"}