{"_id":"@aeon-ai-pay/fetch","name":"@aeon-ai-pay/fetch","dist-tags":{"latest":"2.1.0"},"versions":{"2.1.0":{"name":"@aeon-ai-pay/fetch","version":"2.1.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/coinbase/x402.git"},"description":"x402 Payment Protocol Fetch Extension","devDependencies":{"@eslint/js":"^9.24.0","@types/node":"^22.13.4","@typescript-eslint/eslint-plugin":"^8.29.1","@typescript-eslint/parser":"^8.29.1","eslint":"^9.24.0","eslint-plugin-import":"^2.31.0","eslint-plugin-jsdoc":"^50.6.9","eslint-plugin-prettier":"^5.2.6","prettier":"3.5.2","tsup":"^8.4.0","tsx":"^4.19.2","typescript":"^5.7.3","vite":"^6.2.6","vite-tsconfig-paths":"^5.1.4","vitest":"^3.0.5"},"dependencies":{"viem":"^2.39.3","zod":"^3.24.2","@aeon-ai-pay/core":"^2.1.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"}}},"publishConfig":{"access":"public"},"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":"@aeon-ai-pay/fetch@2.1.0","bugs":{"url":"https://github.com/coinbase/x402/issues"},"homepage":"https://github.com/coinbase/x402#readme","_integrity":"sha512-vWGgGuoHvWb+7UasXcN/AwDMnFcuX1xsG8F4r7j1F4nGp+9bET+6QOEITzY6XewXo/nWg8e1aVCVlwdx/dI5kg==","_resolved":"/private/var/folders/rr/hyts0bs54zn73fgjjlld0tvm0000gn/T/c26b4442ace6c03b97b74bcea01a6ed2/aeon-ai-pay-fetch-2.1.0.tgz","_from":"file:aeon-ai-pay-fetch-2.1.0.tgz","_nodeVersion":"25.8.1","_npmVersion":"11.11.0","dist":{"integrity":"sha512-vWGgGuoHvWb+7UasXcN/AwDMnFcuX1xsG8F4r7j1F4nGp+9bET+6QOEITzY6XewXo/nWg8e1aVCVlwdx/dI5kg==","shasum":"2836bf433c830937f9811afa934734601c8fb653","tarball":"https://registry.npmjs.org/@aeon-ai-pay/fetch/-/fetch-2.1.0.tgz","fileCount":8,"unpackedSize":32100,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQCuMbkPYYlZuX4ykgkuv6BQy3ICVtFTLcLIJgF1Fd2lqgIhAMW67cY+pgnlP+oQIYrrSd0y/mcizCCVZC267IUaI982"}]},"_npmUser":{"name":"aeon_project","email":"code@alchemypay.org"},"directories":{},"maintainers":[{"name":"aeon_project","email":"code@alchemypay.org"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/fetch_2.1.0_1775096320146_0.879401588370387"},"_hasShrinkwrap":false}},"time":{"created":"2026-04-02T02:18:40.092Z","2.1.0":"2026-04-02T02:18:40.285Z","modified":"2026-04-02T02:18:40.448Z"},"maintainers":[{"name":"aeon_project","email":"code@alchemypay.org"}],"description":"x402 Payment Protocol Fetch Extension","homepage":"https://github.com/coinbase/x402#readme","keywords":[],"repository":{"type":"git","url":"git+https://github.com/coinbase/x402.git"},"author":{"name":"Coinbase Inc."},"bugs":{"url":"https://github.com/coinbase/x402/issues"},"license":"Apache-2.0","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 v2. This package enables seamless integration of payment functionality into your applications when making HTTP requests.\n\n## Installation\n\n```bash\npnpm install @x402/fetch\n```\n\n## Quick Start\n\n```typescript\nimport { wrapFetchWithPaymentFromConfig } from \"@x402/fetch\";\nimport { ExactEvmScheme } from \"@x402/evm\";\nimport { privateKeyToAccount } from \"viem/accounts\";\n\n// Create an account\nconst account = privateKeyToAccount(\"0xYourPrivateKey\");\n\n// Wrap the fetch function with payment handling\nconst fetchWithPayment = wrapFetchWithPaymentFromConfig(fetch, {\n  schemes: [\n    {\n      network: \"eip155:8453\", // Base Sepolia\n      client: new ExactEvmScheme(account),\n    },\n  ],\n});\n\n// Make a request that may require payment\nconst response = await fetchWithPayment(\"https://api.example.com/paid-endpoint\", {\n  method: \"GET\",\n});\n\nconst data = await response.json();\n```\n\n## API\n\n### `wrapFetchWithPayment(fetch, client)`\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- `client`: An x402Client instance with registered payment schemes\n\n### `wrapFetchWithPaymentFromConfig(fetch, config)`\n\nConvenience wrapper that creates an x402Client from a configuration object.\n\n#### Parameters\n\n- `fetch`: The fetch function to wrap (typically `globalThis.fetch`)\n- `config`: Configuration object with the following properties:\n  - `schemes`: Array of scheme registrations, each containing:\n    - `network`: Network identifier (e.g., 'eip155:8453', 'solana:mainnet', 'eip155:*' for wildcards)\n    - `client`: The scheme client implementation (e.g., `ExactEvmScheme`, `ExactSvmScheme`)\n    - `x402Version`: Optional protocol version (defaults to 2, set to 1 for legacy support)\n  - `paymentRequirementsSelector`: Optional function to select payment requirements from multiple options\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. Creating a payment header using the configured scheme client\n4. Retrying the request with the payment header\n\n## Examples\n\n### Basic Usage with EVM\n\n```typescript\nimport { config } from \"dotenv\";\nimport { wrapFetchWithPaymentFromConfig, decodePaymentResponseHeader } from \"@x402/fetch\";\nimport { privateKeyToAccount } from \"viem/accounts\";\nimport { ExactEvmScheme } from \"@x402/evm\";\n\nconfig();\n\nconst { EVM_PRIVATE_KEY, API_URL } = process.env;\n\nconst account = privateKeyToAccount(EVM_PRIVATE_KEY as `0x${string}`);\n\nconst fetchWithPayment = wrapFetchWithPaymentFromConfig(fetch, {\n  schemes: [\n    {\n      network: \"eip155:*\", // Support all EVM chains\n      client: new ExactEvmScheme(account),\n    },\n  ],\n});\n\n// Make a request to a paid API endpoint\nfetchWithPayment(API_URL, {\n  method: \"GET\",\n})\n  .then(async response => {\n    const data = await response.json();\n    \n    // Optionally decode the payment response header\n    const paymentResponse = response.headers.get(\"PAYMENT-RESPONSE\");\n    if (paymentResponse) {\n      const decoded = decodePaymentResponseHeader(paymentResponse);\n      console.log(\"Payment details:\", decoded);\n    }\n    \n    console.log(\"Response data:\", data);\n  })\n  .catch(error => {\n    console.error(error);\n  });\n```\n\n### Using Builder Pattern\n\nFor more control, you can use the builder pattern to register multiple schemes:\n\n```typescript\nimport { wrapFetchWithPayment, x402Client } from \"@x402/fetch\";\nimport { ExactEvmScheme } from \"@x402/evm/exact/client\";\nimport { ExactSvmScheme } from \"@x402/svm/exact/client\";\nimport { privateKeyToAccount } from \"viem/accounts\";\nimport { createKeyPairSignerFromBytes } from \"@solana/kit\";\nimport { base58 } from \"@scure/base\";\n\n// Create signers\nconst evmSigner = privateKeyToAccount(\"0xYourPrivateKey\");\nconst svmSigner = await createKeyPairSignerFromBytes(base58.decode(\"YourSvmPrivateKey\"));\n\n// Build client with multiple schemes\nconst client = new x402Client()\n  .register(\"eip155:*\", new ExactEvmScheme(evmSigner))\n  .register(\"solana:*\", new ExactSvmScheme(svmSigner));\n\n// Wrap fetch with the client\nconst fetchWithPayment = wrapFetchWithPayment(fetch, client);\n```\n\n### Multi-Chain Support\n\n```typescript\nimport { wrapFetchWithPaymentFromConfig } from \"@x402/fetch\";\nimport { ExactEvmScheme } from \"@x402/evm\";\nimport { ExactSvmScheme } from \"@x402/svm\";\n\nconst fetchWithPayment = wrapFetchWithPaymentFromConfig(fetch, {\n  schemes: [\n    // EVM chains\n    {\n      network: \"eip155:8453\", // Base Sepolia\n      client: new ExactEvmScheme(evmAccount),\n    },\n    // SVM chains\n    {\n      network: \"solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1\", // Solana devnet\n      client: new ExactSvmScheme(svmSigner),\n    },\n  ],\n});\n```\n\n### Custom Payment Requirements Selector\n\n```typescript\nimport { wrapFetchWithPaymentFromConfig, type SelectPaymentRequirements } from \"@x402/fetch\";\nimport { ExactEvmScheme } from \"@x402/evm\";\n\n// Custom selector that prefers the cheapest option\nconst selectCheapestOption: SelectPaymentRequirements = (version, accepts) => {\n  if (!accepts || accepts.length === 0) {\n    throw new Error(\"No payment options available\");\n  }\n  \n  // Sort by value and return the cheapest\n  const sorted = [...accepts].sort((a, b) => \n    BigInt(a.value) - BigInt(b.value)\n  );\n  \n  return sorted[0];\n};\n\nconst fetchWithPayment = wrapFetchWithPaymentFromConfig(fetch, {\n  schemes: [\n    {\n      network: \"eip155:8453\",\n      client: new ExactEvmScheme(account),\n    },\n  ],\n  paymentRequirementsSelector: selectCheapestOption,\n});\n```\n\n","readmeFilename":"README.md","_rev":"1-6670a5a02fb27d90bfef14872cc5e9fc"}