{"_id":"@bankofai/x402-next-legacy","_rev":"4-8ca3bb1e2d15a10befeadb3a15d1d89b","name":"@bankofai/x402-next-legacy","dist-tags":{"beta":"1.1.0","latest":"1.1.0"},"versions":{"1.1.0":{"name":"@bankofai/x402-next-legacy","version":"1.1.0","keywords":[],"author":{"name":"bankofai"},"license":"Apache-2.0","_id":"@bankofai/x402-next-legacy@1.1.0","maintainers":[{"name":"virtual_parson","email":"parson.hu@tron.network"},{"name":"hades.ye","email":"hades.ye@tron.network"},{"name":"leo_tron","email":"leo.wu@tron.network"},{"name":"roger.gan","email":"roger.gan@tron.network"},{"name":"gummy789j","email":"gummy789j@gmail.com"},{"name":"wzc1206","email":"david.wang@tron.network"},{"name":"bobo123123hhh","email":"bobo.liu@tron.network"}],"homepage":"https://github.com/BofAI/x402#readme","bugs":{"url":"https://github.com/BofAI/x402/issues"},"dist":{"shasum":"94a6c1a18eb67c34450d1e95b512c86af5599741","tarball":"https://registry.npmjs.org/@bankofai/x402-next-legacy/-/x402-next-legacy-1.1.0.tgz","fileCount":8,"integrity":"sha512-dd+ZnEC9Ao6URNqlj/+7FMDOwWVLnU/WjteCyg6aDkqzc0Qju37hDGhap6iiN1DKPAp0gz2JmjlXVzZOGLAxeQ==","signatures":[{"sig":"MEYCIQC14uEws93sGRZOXwomSBqgYH9PUc59+GJ31BkDz61isQIhAJudpXaQO+Tgg3crNfTBywD1I8aykdVMX1oVcLy2Nb8R","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":116508},"main":"./dist/cjs/index.js","type":"module","_from":"file:bankofai-x402-next-legacy-1.1.0.tgz","types":"./dist/index.d.ts","module":"./dist/esm/index.js","exports":{".":{"import":{"types":"./dist/esm/index.d.ts","default":"./dist/esm/index.js"},"require":{"types":"./dist/cjs/index.d.cts","default":"./dist/cjs/index.cjs"}}},"scripts":{"lint":"eslint . --ext .ts --fix","test":"vitest run","build":"tsup","start":"tsx --env-file=.env index.ts","watch":"tsc --watch","format":"prettier -c .prettierrc --write \"**/*.{ts,js,cjs,json,md}\"","lint:check":"eslint . --ext .ts","test:watch":"vitest","format:check":"prettier -c .prettierrc --check \"**/*.{ts,js,cjs,json,md}\""},"_npmUser":{"name":"bobo123123hhh","email":"bobo.liu@tron.network"},"_resolved":"/private/var/folders/0f/72lzrqsd7_dcw0_6_qdsyslc0000gn/T/99a840bd238112693cbf60be91268e29/bankofai-x402-next-legacy-1.1.0.tgz","_integrity":"sha512-dd+ZnEC9Ao6URNqlj/+7FMDOwWVLnU/WjteCyg6aDkqzc0Qju37hDGhap6iiN1DKPAp0gz2JmjlXVzZOGLAxeQ==","repository":{"url":"git+https://github.com/BofAI/x402.git","type":"git"},"_npmVersion":"11.8.0","description":"x402 Payment Protocol","directories":{},"_nodeVersion":"25.6.0","dependencies":{"zod":"^3.24.2","viem":"^2.21.26","@solana/kit":"^5.0.0","@coinbase/cdp-sdk":"^1.22.0","@bankofai/x402-legacy":"^1.1.0"},"_hasShrinkwrap":false,"devDependencies":{"tsx":"^4.19.2","tsup":"^8.4.0","vite":"^6.2.6","eslint":"^9.24.0","vitest":"^3.0.5","prettier":"3.5.2","@eslint/js":"^9.24.0","typescript":"^5.7.3","@types/node":"^22.13.4","eslint-plugin-jsdoc":"^50.6.9","vite-tsconfig-paths":"^5.1.4","eslint-plugin-import":"^2.31.0","eslint-plugin-prettier":"^5.2.6","@typescript-eslint/parser":"^8.29.1","@typescript-eslint/eslint-plugin":"^8.29.1"},"peerDependencies":{"next":">=15.5.9 || >=16.0.10"},"_npmOperationalInternal":{"tmp":"tmp/x402-next-legacy_1.1.0_1773471608213_0.3005763886460404","host":"s3://npm-registry-packages-npm-production"}}},"time":{"created":"2026-03-14T07:00:08.124Z","modified":"2026-09-16T08:36:03.469Z","1.1.0":"2026-03-14T07:00:08.392Z"},"bugs":{"url":"https://github.com/BofAI/x402/issues"},"author":{"name":"bankofai"},"license":"Apache-2.0","homepage":"https://github.com/BofAI/x402#readme","keywords":[],"repository":{"url":"git+https://github.com/BofAI/x402.git","type":"git"},"description":"x402 Payment Protocol","maintainers":[{"email":"parson.hu@tron.network","name":"virtual_parson"},{"email":"roger.gan@tron.network","name":"roger.gan"},{"email":"gummy789j@gmail.com","name":"gummy789j"},{"email":"david.wang@tron.network","name":"wzc1206"},{"email":"bobo.liu@tron.network","name":"bobo123123hhh"},{"email":"zerodevblock@gmail.com","name":"zerodev0"},{"email":"leon.zhang@tron.network","name":"leonatmoon"}],"readme":"# x402-next\n\nNext.js middleware integration for the x402 Payment Protocol. This package allows you to easily add paywall functionality to your Next.js applications using the x402 protocol.\n\n## Installation\n\n```bash\npnpm install x402-next\n```\n\n## Quick Start\n\n### Protecting Page Routes\n\nPage routes are protected using the x402-next middleware. Create a middleware file in your Next.js project (e.g., `middleware.ts`):\n\n```typescript\nimport { paymentMiddleware, Network } from 'x402-next';\n\nexport const middleware = paymentMiddleware(\n  \"0xYourAddress\",\n  {\n    '/protected': {\n      price: '$0.01',\n      network: \"base-sepolia\",\n      config: {\n        description: 'Access to protected content'\n      }\n    },\n  }\n);\n\n// Configure which paths the middleware should run on\nexport const config = {\n  matcher: ['/protected/:path*'],\n  runtime: \"nodejs\",\n};\n```\n\n### Protecting API Routes\n\nAPI routes are protected using the `withX402` route wrapper. This is the recommended approach to protect API routes as it guarantees payment settlement only AFTER successful API responses (status < 400). API routes can also be protected by `paymentMiddleware`, however this will charge clients for failed API responses:\n\n```typescript\n// app/api/your-endpoint/route.ts\nimport { NextRequest, NextResponse } from \"next/server\";\nimport { withX402 } from \"x402-next\";\n\nconst handler = async (_: NextRequest) => {\n  return NextResponse.json({ data: \"your response\" });\n};\n\nexport const GET = withX402(\n  handler,\n  \"0xYourAddress\",\n  {\n    price: \"$0.01\",\n    network: \"base-sepolia\",\n    config: { description: \"Access to API endpoint\" }\n  }\n);\n```\n\n## Configuration\n\n### paymentMiddleware\n\nThe `paymentMiddleware` function is used to protect page routes. It can also protect API routes, however this will charge clients for failed API responses. It accepts four parameters:\n\n1. `payTo`: Your receiving address (`0x${string}`)\n2. `routes`: Route configurations for protected endpoints\n3. `facilitator`: (Optional) Configuration for the x402 facilitator service\n4. `paywall`: (Optional) Configuration for the built-in paywall\n\n### withX402\n\nThe `withX402` function wraps API route handlers. This is the recommended approach to protect API routes as it guarantees payment settlement only AFTER successful API responses (status < 400). It accepts five parameters:\n\n1. `handler`: Your API route handler function\n2. `payTo`: Your receiving address (`0x${string}`)\n3. `routeConfig`: Payment configuration for this specific route\n4. `facilitator`: (Optional) Configuration for the x402 facilitator service\n5. `paywall`: (Optional) Configuration for the built-in paywall\n\nSee the Middleware Options section below for detailed configuration options.\n\n## Middleware Options\n\nThe middleware supports various configuration options:\n\n### Route Configuration\n\n```typescript\ntype RoutesConfig = Record<string, Price | RouteConfig>;\n\ninterface RouteConfig {\n  price: Price;           // Price in USD or token amount\n  network: Network;       // \"base\" or \"base-sepolia\"\n  config?: PaymentMiddlewareConfig;\n}\n```\n\n### Payment Configuration\n\n```typescript\ninterface PaymentMiddlewareConfig {\n  description?: string;               // Description of the payment\n  mimeType?: string;                  // MIME type of the resource\n  maxTimeoutSeconds?: number;         // Maximum time for payment (default: 60)\n  outputSchema?: Record<string, any>; // JSON schema for the response\n  customPaywallHtml?: string;         // Custom HTML for the paywall\n  resource?: string;                  // Resource URL (defaults to request URL)\n}\n```\n\n### Facilitator Configuration\n\n```typescript\ntype FacilitatorConfig = {\n  url: string;                        // URL of the x402 facilitator service\n  createAuthHeaders?: CreateHeaders;  // Optional function to create authentication headers\n};\n```\n\n### Paywall Configuration\n\nFor more on paywall configuration options, refer to the [paywall README](../x402/src/paywall/README.md).\n\n```typescript\ntype PaywallConfig = {\n  cdpClientKey?: string;              // Your CDP Client API Key\n  appName?: string;                   // Name displayed in the paywall wallet selection modal\n  appLogo?: string;                   // Logo for the paywall wallet selection modal\n  sessionTokenEndpoint?: string;      // API endpoint for Coinbase Onramp session authentication\n};\n```\n\n## Accessing Mainnet with @coinbase/x402\n\nTo use the official Coinbase facilitator package (`@coinbase/x402`) in your Next.js project:\n\n1. Install the Coinbase facilitator package:\n\n```bash\npnpm install @coinbase/x402\n```\n\n2. Use it in your middleware file::\n\n```ts\n// middleware.ts\nimport { paymentMiddleware } from \"x402-next\";\nimport { facilitator } from \"@coinbase/x402\";\n\nexport const middleware = paymentMiddleware(\n  \"0xYourAddress\",\n  {\n    \"/protected\": {\n      price: \"$0.01\",\n      network: \"base\",\n      // other config options\n    },\n  },\n  facilitator // Use the Coinbase facilitator\n);\n```\n\n3. Set up your CDP API keys as environment variables:\n\n```bash\n# .env\nCDP_API_KEY_ID=your-cdp-api-key-id\nCDP_API_KEY_SECRET=your-cdp-api-key-secret\n```\n\n## Optional: Coinbase Onramp Integration\n\n**Note**: Onramp integration is completely optional. Your x402 paywall will work perfectly without it. This feature is for users who want to provide an easy way for their customers to fund their wallets directly from the paywall.\n\nWhen configured, a \"Get more USDC\" button will appear in your paywall, allowing users to purchase USDC directly through Coinbase Onramp.\n\n### Quick Setup\n\n#### 1. Configure Your Middleware\n\nAdd `sessionTokenEndpoint` to your middleware configuration. This tells the paywall where to find your session token API:\n\n```typescript\nexport const middleware = paymentMiddleware(\n  payTo,\n  routes,\n  facilitator,\n  {\n    sessionTokenEndpoint: \"/api/x402/session-token\", // Enable onramp functionality\n    cdpClientKey: \"your-cdp-client-key\",\n    appName: \"My App\",\n  }\n);\n```\n\n**Important**: The `sessionTokenEndpoint` can be any path you choose - just make sure it matches where you create your API route in the next step. Without this configuration, the \"Get more USDC\" button will be hidden.\n\n#### 2. Create the Session Token API\n\nCreate an API route that matches the path you configured above:\n\n```typescript\n// app/api/x402/session-token/route.ts\nexport { POST } from \"x402-next\";\n```\n\nThat's it! The `x402-next` package provides the complete session token implementation.\n\n#### 3. Get CDP API Keys\n\n1. Go to [CDP Portal](https://portal.cdp.coinbase.com/)\n2. Navigate to your project's **[API Keys](https://portal.cdp.coinbase.com/projects/api-keys)**\n3. Click **Create API key**\n4. Download and securely store your API key\n\n#### 4. Enable Onramp Secure Initialization in CDP Portal\n\n1. Go to [CDP Portal](https://portal.cdp.coinbase.com/)\n2. Navigate to **Payments → [Onramp & Offramp](https://portal.cdp.coinbase.com/products/onramp)**\n3. Toggle **\"Enforce secure initialization\"** to **Enabled**\n\n#### 5. Set Environment Variables\n\nAdd your CDP API keys to your environment:\n\n```bash\n# .env\nCDP_API_KEY_ID=your_secret_api_key_id_here\nCDP_API_KEY_SECRET=your_secret_api_key_secret_here\n```\n\n### How Onramp Works\n\nOnce set up, your x402 paywall will automatically show a \"Get more USDC\" button when users need to fund their wallets. \n\n1. **Generates session token**: Your backend securely creates a session token using CDP's API\n2. **Opens secure onramp**: User is redirected to Coinbase Onramp with the session token\n3. **No exposed data**: Wallet addresses and app IDs are never exposed in URLs\n\n### Troubleshooting Onramp\n\n#### Common Issues\n\n1. **\"Missing CDP API credentials\"**\n    - Ensure `CDP_API_KEY_ID` and `CDP_API_KEY_SECRET` are set\n    - Verify you're using **Secret API Keys**, not Client API Keys\n\n2. **\"Failed to generate session token\"**\n    - Check your CDP Secret API key has proper permissions\n    - Verify your project has Onramp enabled\n\n3. **API route not found**\n    - Ensure you've created your session token API route at the path you configured\n    - Check that your API route path matches your `sessionTokenEndpoint` configuration\n    - Verify the export: `export { POST } from \"x402-next\";`\n    - Example: If you configured `sessionTokenEndpoint: \"/api/custom/onramp\"`, create `app/api/custom/onramp/route.ts`\n\n\n## Resources\n\n- [x402 Protocol](https://x402.org)\n- [CDP Documentation](https://docs.cdp.coinbase.com)\n- [CDP Discord](https://discord.com/invite/cdp)","readmeFilename":"README.md"}