{"id":45999303,"url":"https://github.com/zbdpay/agent-fetch","last_synced_at":"2026-03-04T01:01:10.236Z","repository":{"id":340668796,"uuid":"1167039677","full_name":"zbdpay/agent-fetch","owner":"zbdpay","description":"Fetch API Compat","archived":false,"fork":false,"pushed_at":"2026-02-28T20:23:30.000Z","size":286,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-02-28T23:14:21.855Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zbdpay.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-02-25T22:03:38.000Z","updated_at":"2026-02-28T20:23:32.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/zbdpay/agent-fetch","commit_stats":null,"previous_names":["zbdpay/agent-fetch"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/zbdpay/agent-fetch","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zbdpay%2Fagent-fetch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zbdpay%2Fagent-fetch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zbdpay%2Fagent-fetch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zbdpay%2Fagent-fetch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zbdpay","download_url":"https://codeload.github.com/zbdpay/agent-fetch/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zbdpay%2Fagent-fetch/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29987654,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-01T22:42:38.399Z","status":"ssl_error","status_checked_at":"2026-03-01T22:41:51.863Z","response_time":124,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-02-28T22:04:04.220Z","updated_at":"2026-03-03T00:01:42.787Z","avatar_url":"https://github.com/zbdpay.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @zbdpay/agent-fetch\n\nL402-aware fetch client for paid HTTP resources.\n\nThis package handles the full payment challenge flow:\n- parse `402 Payment Required` responses\n- support both `L402` and `LSAT` schemes\n- pay using caller-provided hooks\n- retry with `Authorization` proof\n- cache proofs locally to avoid duplicate payments\n\nWant to run this immediately? See [Examples (Fastest Way to Run)](#examples-fastest-way-to-run).\n\n## Requirements\n\n- Node.js `\u003e=22`\n- npm\n\n## Install\n\n```bash\nnpm install @zbdpay/agent-fetch\n```\n\n## Quick Start\n\n```ts\nimport { agentFetch, FileTokenCache } from \"@zbdpay/agent-fetch\";\n\nconst tokenCache = new FileTokenCache(`${process.env.HOME}/.zbd-wallet/token-cache.json`);\n\nconst response = await agentFetch(\"https://example.com/protected\", {\n  tokenCache,\n  maxPaymentSats: 100,\n  pay: async (challenge) =\u003e {\n    // Pay challenge.invoice with your wallet implementation.\n    // Return preimage, plus optional paymentId/amountPaidSats.\n    return {\n      preimage: \"\u003cpayment-preimage\u003e\",\n      paymentId: \"\u003cpayment-id\u003e\",\n      amountPaidSats: challenge.amountSats,\n    };\n  },\n  waitForPayment: async (paymentId) =\u003e {\n    // Optional poller for async settlement.\n    // Return pending/completed/failed.\n    return {\n      status: \"completed\",\n      paymentId,\n      preimage: \"\u003cpayment-preimage\u003e\",\n      amountPaidSats: 21,\n    };\n  },\n});\n\nconst body = await response.json();\nconsole.log(response.status, body);\n```\n\n## Behavior\n\n- If cached auth exists and is not expired, request is sent immediately with proof.\n- If response is not `402`, original response is returned untouched.\n- If response is `402`, challenge is parsed from `WWW-Authenticate` and/or JSON body.\n- If payment succeeds, proof is generated as `\u003cSCHEME\u003e \u003cmacaroon-or-token\u003e:\u003cpreimage\u003e` and request is retried.\n- If `maxPaymentSats` is set and challenge exceeds it, call fails before payment.\n- If async settlement is used and times out, call fails with a timeout error.\n\n## Public API\n\nExports from `src/index.ts`:\n\n- `agentFetch`\n- `requestChallenge`\n- `payChallenge`\n- `fetchWithProof`\n- `FileTokenCache`\n- types: `AgentFetchOptions`, `PaymentChallenge`, `PaidChallenge`, `PaymentSettlement`, `TokenCache`, `TokenRecord`, `ChallengeScheme`\n\n## Options (`AgentFetchOptions`)\n\n- `pay` (required): function to pay a parsed challenge\n- `waitForPayment` (optional): poller for async settlement\n- `tokenCache` (optional): token cache backend\n- `requestInit` (optional): forwarded fetch options\n- `fetchImpl` (optional): custom fetch implementation\n- `maxPaymentSats` (optional): payment guardrail\n- `paymentTimeoutMs` (optional, default `30000`)\n- `paymentPollIntervalMs` (optional, default `300`)\n- `now`, `sleep` (optional testability hooks)\n\n## Token Cache\n\n`FileTokenCache` stores per-URL tokens as JSON and writes atomically.\n\n- no `expiresAt`: token is reused until overwritten/deleted\n- with `expiresAt`: expired token is evicted on read\n\nDefault cache location is chosen by the caller. In this suite, `agent-wallet` uses `~/.zbd-wallet/token-cache.json`.\n\n## Examples (Fastest Way to Run)\n\nIf you want a working paid-request flow in minutes, start with these scripts before wiring your own app code.\n\n- `examples/zbd-agent-fetch.mjs`: end-to-end paid fetch using ZBD API for invoice payment\n- `examples/fetch-with-known-proof.mjs`: call a protected endpoint with a precomputed L402 token\n\nRun from this repo:\n\n```bash\nnpm run build\nPROTECTED_URL=\"http://localhost:8787/protected\" ZBD_API_KEY=\u003cyour_api_key\u003e npm run example:zbd\n```\n\nIf you already have an authorization token:\n\n```bash\nPROTECTED_URL=\"http://localhost:8787/protected\" L402_AUTHORIZATION=\"L402 \u003cmacaroon\u003e:\u003cpreimage\u003e\" npm run example:proof\n```\n\n## Scripts\n\n```bash\nnpm run build\nnpm run test\nnpm run lint\nnpm run typecheck\nnpm run smoke:imports\nnpm run example:zbd\nnpm run example:proof\nnpm run release:dry-run\n```\n\n## Related Packages\n\n- `@zbdpay/agent-wallet` uses this package for `zbdw fetch`\n- `@zbdpay/agent-pay` provides middleware that emits L402 challenges this client can consume\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzbdpay%2Fagent-fetch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzbdpay%2Fagent-fetch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzbdpay%2Fagent-fetch/lists"}