{"_id":"@bankofai/x402-fetch-legacy","_rev":"4-7c892c4a3f98f1c4d6010b6524748bd4","name":"@bankofai/x402-fetch-legacy","dist-tags":{"beta":"1.1.0","latest":"1.1.0"},"versions":{"1.1.0":{"name":"@bankofai/x402-fetch-legacy","version":"1.1.0","keywords":[],"author":{"name":"bankofai"},"license":"Apache-2.0","_id":"@bankofai/x402-fetch-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":"a80b2f37b07ffefe3f2825c17b94ec6ca9438961","tarball":"https://registry.npmjs.org/@bankofai/x402-fetch-legacy/-/x402-fetch-legacy-1.1.0.tgz","fileCount":8,"integrity":"sha512-c2cm5pL7mwQ139+EUDG/4geXzO788KIr6ydfK+Kod220YrfCfJzK0bAocK3wD5XmbCB0rXyqif3zElHvf1HgoA==","signatures":[{"sig":"MEQCIC5SFJOmw7qwrVQpKeXlqr/gKBTOcNiTpkXRxqXYx5u5AiB9vzgLDCvlLb4yCC2szNDE9/Zv1XY5OCVPhfAbVX0GDw==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":25969},"main":"./dist/cjs/index.js","_from":"file:bankofai-x402-fetch-legacy-1.1.0.tgz","types":"./dist/index.d.ts","module":"./dist/esm/index.js","exports":{".":{"import":{"types":"./dist/esm/index.d.mts","default":"./dist/esm/index.mjs"},"require":{"types":"./dist/cjs/index.d.ts","default":"./dist/cjs/index.js"}}},"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/0a4ba2a509b8f3a3a4e91554d161e668/bankofai-x402-fetch-legacy-1.1.0.tgz","_integrity":"sha512-c2cm5pL7mwQ139+EUDG/4geXzO788KIr6ydfK+Kod220YrfCfJzK0bAocK3wD5XmbCB0rXyqif3zElHvf1HgoA==","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","@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"},"_npmOperationalInternal":{"tmp":"tmp/x402-fetch-legacy_1.1.0_1773471607934_0.7288639736045508","host":"s3://npm-registry-packages-npm-production"}}},"time":{"created":"2026-03-14T07:00:07.813Z","modified":"2026-09-16T08:36:03.263Z","1.1.0":"2026-03-14T07:00:08.095Z"},"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-fetch\n\nA utility package that extends the native `fetch` API to automatically handle 402 Payment Required responses using the x402 payment protocol. This package enables seamless integration of payment functionality into your applications when making HTTP requests.\n\n## Installation\n\n```bash\nnpm install x402-fetch\n```\n\n## Quick Start\n\n```typescript\nimport { createWalletClient, http } from \"viem\";\nimport { privateKeyToAccount } from \"viem/accounts\";\nimport { wrapFetchWithPayment } from \"x402-fetch\";\nimport { baseSepolia } from \"viem/chains\";\n\n// Create a wallet client\nconst account = privateKeyToAccount(\"0xYourPrivateKey\");\nconst client = createWalletClient({\n  account,\n  transport: http(),\n  chain: baseSepolia,\n});\n\n// Wrap the fetch function with payment handling\nconst fetchWithPay = wrapFetchWithPayment(fetch, client);\n\n// Make a request that may require payment\nconst response = await fetchWithPay(\"https://api.example.com/paid-endpoint\", {\n  method: \"GET\",\n});\n\nconst data = await response.json();\n```\n\n## API\n\n### `wrapFetchWithPayment(fetch, walletClient, maxValue?, paymentRequirementsSelector?)`\n\nWraps the native fetch API to handle 402 Payment Required responses automatically.\n\n#### Parameters\n\n- `fetch`: The fetch function to wrap (typically `globalThis.fetch`)\n- `walletClient`: The wallet client used to sign payment messages (must implement the x402 wallet interface)\n- `maxValue`: Optional maximum allowed payment amount in base units (defaults to 0.1 USDC)\n- `paymentRequirementsSelector`: Optional function to select payment requirements from the response (defaults to `selectPaymentRequirements`)\n\n#### Returns\n\nA wrapped fetch function that automatically handles 402 responses by:\n1. Making the initial request\n2. If a 402 response is received, parsing the payment requirements\n3. Verifying the payment amount is within the allowed maximum\n4. Creating a payment header using the provided wallet client\n5. Retrying the request with the payment header\n\n## Example\n\n```typescript\nimport { config } from \"dotenv\";\nimport { createWalletClient, http } from \"viem\";\nimport { privateKeyToAccount } from \"viem/accounts\";\nimport { wrapFetchWithPayment } from \"x402-fetch\";\nimport { baseSepolia } from \"viem/chains\";\n\nconfig();\n\nconst { PRIVATE_KEY, API_URL } = process.env;\n\nconst account = privateKeyToAccount(PRIVATE_KEY as `0x${string}`);\nconst client = createWalletClient({\n  account,\n  transport: http(),\n  chain: baseSepolia,\n});\n\nconst fetchWithPay = wrapFetchWithPayment(fetch, client);\n\n// Make a request to a paid API endpoint\nfetchWithPay(API_URL, {\n  method: \"GET\",\n})\n  .then(async response => {\n    const data = await response.json();\n    console.log(data);\n  })\n  .catch(error => {\n    console.error(error);\n  });\n```\n\n","readmeFilename":"README.md"}