tally.x402 resource lets your agent call any HTTP service that uses the x402 protocol. When the service returns 402 Payment Required, the SDK parses the payment terms, pays via Tally, and retries with the on-chain proof in the X-Payment header — your agent code sees one fetch() call.
Use this for any agent that needs to pay for paywalled APIs without you hand-rolling the 402 dance every time. See the paying-x402-agent guide for an end-to-end walk-through.
Method
tally.x402.fetch(url, input)
Fetch a URL. If the service returns 402, pay via Tally and retry automatically. Returns the final HTTP response plus the receipt for the payment (if one was needed).
Types
X402FetchInput
X402FetchResult
Errors
tally.x402.fetch() throws a TallyError for SDK-side problems. Errors that originate from the underlying payments.create call (policy denial, validation, rate limiting) come through as the standard typed exceptions. x402-specific failures use these .type values:
.type | When |
|---|---|
x402_protocol | The 402 response was missing accepts[], malformed JSON, or used an unsupported network. |
x402_amount_exceeds_cap | The 402 quoted more than the caller’s max_amount_usdc. |
x402_payment_missing_tx_hash | Tally accepted the payment but returned no tx hash (should not happen — file a bug). |
validation_failed | The caller passed an invalid max_amount_usdc decimal string. |
response.ok / response.status as you would with any fetch.
Patterns
Generic tool for any LLM agent
Drop this into your LLM tool list and the agent can pay for any x402-paywalled endpoint with a single tool call:Idempotent retries
If your tool wrapper might retry the same logical x402 call (network blip, agent loop, etc.), pass a stableidempotency_key so the second attempt collapses into the first payment instead of double-spending: