fetch() call into a complete payment flow across x402, L402, and MPP protocols. Your agent never touches wallets, protocols, or transaction signing directly. The SDK handles everything behind the scenes.
The Payment Flow
When your agent callsagent.fetch(url), here is what happens under the hood:
Step by Step
-
Probe — The SDK sends a regular GET request to the endpoint. If the server returns
200, the response passes through as-is. No payment needed. -
402 Detection — If the server returns
402 Payment Required, the SDK runs MppAdapter, X402Adapter, and L402Adapter in parallel (Promise.allSettled) to determine which payment protocol is in use. -
Quote — The SDK extracts the price, currency, network, and facilitator address from the 402 response. This is the same information you get from
agent.quote(url). -
Budget Validation — Before any money moves, the SDK validates the price against your configured budget limits (per-transaction, daily, monthly). If the price exceeds any limit, a
BudgetExceededErroris thrown and no payment is made. - Sign — The SDK creates a cryptographic payment authorization using your wallet credentials. For x402, this is an EIP-3009 signature (gasless for you). For L402, this is a Lightning invoice payment.
- Deliver — The SDK re-sends the original request with the signed payment attached (in headers for x402, in a macaroon for L402).
- Verify & Settle — The server verifies the payment cryptographically (EIP-712 signature for x402, macaroon validation for L402) and settles it on-chain or via Lightning. Once confirmed, the server returns the content.
-
Record — The SDK logs the payment amount in its budget tracker and returns the response to your agent, including payment metadata (
response.payment).
What the Agent Sees
From your agent’s perspective, nothing unusual happens. It calledfetch() and got data back:
200, and returns the response. response.payment is null.
Session Flow
For MPP endpoints that support streaming, useopenSession() instead of fetch(). Sessions open a payment channel with a capped deposit, then let you make multiple requests and stream data with per-voucher micropayments.
Code Example
MCP Transport
For AI agents that call third-party MCP servers requiring payment (error code-32042), use wrapMcpClient(). The wrapper intercepts payment challenges, checks budget, creates credentials, and retries the call automatically.
Next Steps
- Architecture — how the SDK packages fit together
- Protocol Detection — how adapters identify x402, L402, and hybrid formats in parallel
- Budget & Safety — spending limits and safety guarantees
- Sessions — payment channels and streaming micropayments
- MCP Transport — transparent payments for MCP tool calls