{"_id":"@devswayam/x402-fetch","_rev":"2-2d4efcb75489428835d07402bd71620b","name":"@devswayam/x402-fetch","dist-tags":{"latest":"0.6.0"},"versions":{"0.5.1":{"name":"@devswayam/x402-fetch","version":"0.5.1","keywords":[],"author":{"name":"Coinbase Inc."},"license":"Apache-2.0","_id":"@devswayam/x402-fetch@0.5.1","maintainers":[{"name":"devswayam","email":"swayamkarle2@gmail.com"}],"homepage":"https://github.com/DevSwayam/x402#readme","bugs":{"url":"https://github.com/DevSwayam/x402/issues"},"dist":{"shasum":"40c1cb54178b489ccd6523e5cb67a37528df6122","tarball":"https://registry.npmjs.org/@devswayam/x402-fetch/-/x402-fetch-0.5.1.tgz","fileCount":8,"integrity":"sha512-kkvQfokLaTnUWWD8al5be/xd5PYL2Yf71l2QmqEjoo0Q41h+yJAsl3lcTbAQcJVdOOakAavbGXHje3RfnsOvpg==","signatures":[{"sig":"MEYCIQCj/1dLtdEz7gWHGuyUpHdHgyHpdWZwhl5pDwg4AKSoRwIhAJ5qRgpf6Vl3t54//iFSonEpOAFDhFho8tYskxtYAmAv","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":22905},"main":"./dist/cjs/index.js","_from":"file:devswayam-x402-fetch-0.5.1.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":"devswayam","email":"swayamkarle2@gmail.com"},"_resolved":"/private/var/folders/mm/pn7zw65s2n994mhcr3vrpxq00000gn/T/38210ca21d0ffd7ffab1fe521620b5ba/devswayam-x402-fetch-0.5.1.tgz","_integrity":"sha512-kkvQfokLaTnUWWD8al5be/xd5PYL2Yf71l2QmqEjoo0Q41h+yJAsl3lcTbAQcJVdOOakAavbGXHje3RfnsOvpg==","repository":{"url":"git+https://github.com/DevSwayam/x402.git","type":"git","directory":"typescript/packages/x402-fetch"},"_npmVersion":"11.3.0","description":"x402 Payment Protocol","directories":{},"_nodeVersion":"24.1.0","dependencies":{"zod":"^3.24.2","viem":"^2.21.26","unwallet-horizen":"^0.1.1"},"publishConfig":{"access":"public"},"_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_0.5.1_1756292406137_0.4512723998161845","host":"s3://npm-registry-packages-npm-production"}},"0.6.0":{"name":"@devswayam/x402-fetch","version":"0.6.0","main":"./dist/cjs/index.js","module":"./dist/esm/index.js","types":"./dist/index.d.ts","keywords":[],"license":"Apache-2.0","author":{"name":"Coinbase Inc."},"repository":{"type":"git","url":"git+https://github.com/DevSwayam/x402.git","directory":"typescript/packages/x402-fetch"},"publishConfig":{"access":"public"},"description":"x402 Payment Protocol","devDependencies":{"@types/node":"^22.13.4","@eslint/js":"^9.24.0","eslint":"^9.24.0","eslint-plugin-jsdoc":"^50.6.9","eslint-plugin-prettier":"^5.2.6","@typescript-eslint/eslint-plugin":"^8.29.1","@typescript-eslint/parser":"^8.29.1","eslint-plugin-import":"^2.31.0","prettier":"3.5.2","tsup":"^8.4.0","tsx":"^4.19.2","typescript":"^5.7.3","vite-tsconfig-paths":"^5.1.4","vitest":"^3.0.5","vite":"^6.2.6"},"dependencies":{"viem":"^2.21.26","zod":"^3.24.2","unwallet-horizen":"^0.2.0"},"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":{"start":"tsx --env-file=.env index.ts","test":"vitest run","test:watch":"vitest","build":"tsup","watch":"tsc --watch","format":"prettier -c .prettierrc --write \"**/*.{ts,js,cjs,json,md}\"","format:check":"prettier -c .prettierrc --check \"**/*.{ts,js,cjs,json,md}\"","lint":"eslint . --ext .ts --fix","lint:check":"eslint . --ext .ts"},"_id":"@devswayam/x402-fetch@0.6.0","bugs":{"url":"https://github.com/DevSwayam/x402/issues"},"homepage":"https://github.com/DevSwayam/x402#readme","_integrity":"sha512-d0TZMxz+AanblyIpv+28vAurN0yJe00A+F4JNZbwzKjl5Wa4W9ScZyW5RO5k7AEc/ElchnWVwaISb1xJm17LGg==","_resolved":"/private/var/folders/mm/pn7zw65s2n994mhcr3vrpxq00000gn/T/8eab34c6bf32fa169c90607d2b7ea7e6/devswayam-x402-fetch-0.6.0.tgz","_from":"file:devswayam-x402-fetch-0.6.0.tgz","_nodeVersion":"24.1.0","_npmVersion":"11.3.0","dist":{"integrity":"sha512-d0TZMxz+AanblyIpv+28vAurN0yJe00A+F4JNZbwzKjl5Wa4W9ScZyW5RO5k7AEc/ElchnWVwaISb1xJm17LGg==","shasum":"ef417f81c8151b21e48e76fee2da198798a544a7","tarball":"https://registry.npmjs.org/@devswayam/x402-fetch/-/x402-fetch-0.6.0.tgz","fileCount":8,"unpackedSize":22905,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIH0VuKeMLPVl5CEj7PEEqrqkbPC5k5qGeOnt9uLrKoN4AiBE/4EUAbNtKV9kxEJL+SqFoV4J4t5D4Nv+dZBzJec0+w=="}]},"_npmUser":{"name":"devswayam","email":"swayamkarle2@gmail.com"},"directories":{},"maintainers":[{"name":"devswayam","email":"swayamkarle2@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/x402-fetch_0.6.0_1756316351327_0.3539467557615561"},"_hasShrinkwrap":false}},"time":{"created":"2025-08-27T11:00:06.074Z","modified":"2025-08-27T17:39:11.724Z","0.5.1":"2025-08-27T11:00:06.366Z","0.6.0":"2025-08-27T17:39:11.524Z"},"bugs":{"url":"https://github.com/DevSwayam/x402/issues"},"author":{"name":"Coinbase Inc."},"license":"Apache-2.0","homepage":"https://github.com/DevSwayam/x402#readme","keywords":[],"repository":{"type":"git","url":"git+https://github.com/DevSwayam/x402.git","directory":"typescript/packages/x402-fetch"},"description":"x402 Payment Protocol","maintainers":[{"name":"devswayam","email":"swayamkarle2@gmail.com"}],"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"}