X402Client wraps fetch with automatic x402 payment handling. When a server returns HTTP 402 Payment Required, the client signs an EIP-3009 USDC transfer and retries the request — transparently.
Optionally, when autoDraw is enabled and paired with a MorphoClient, the client will automatically borrow USDC from Morpho Blue when the Safe account balance is insufficient.
How x402 Works
- Flow
- Auto-Draw
- Agent sends a regular HTTP request via
x402.get()orx402.post() - Server responds
402 Payment Requiredwith payment requirements - The underlying
@x402/fetchlibrary handles the payment: it signs an EIP-3009 transferWithAuthorization for USDC - Request is retried with the payment signature
- Server verifies via the Coinbase CDP facilitator
- Payment settles, server returns the data
Constructor
- Private Key
- External WalletClient (viem)
Configuration
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
privateKey | string | Either this or walletClient | — | Agent wallet private key for signing |
walletClient | WalletClient | Either this or privateKey | — | viem WalletClient with an attached account |
rpcUrl | string | Yes | — | Base RPC endpoint |
backendUrl | string | Yes | — | Agether backend URL |
agentId | string | No | — | ERC-8004 agent ID (sent as X-Agent-Id header) |
accountAddress | string | No | — | Safe account address (payments come from here via EIP-1271) |
autoDraw | boolean | No | false | Enable auto-borrow from Morpho when low |
dailySpendLimitUsdc | string | No | — | Max daily USDC spend (e.g. "100") |
autoYield | boolean | No | false | When enabled, the client tries to withdraw from supply positions (earned yield) before borrowing new USDC from Morpho |
validatorModule | string | No | — | Agether8004ValidationModule address for EIP-1271 signature prefixing |
Methods
get<T>(url, options?)
Make a GET request to an x402-enabled endpoint. Returns an X402Response<T> object.
post<T>(url, body?, options?)
Make a POST request with x402 payment handling.
payWithAutoDraw<T>(url, options?)
Make a request with automatic Morpho borrowing when USDC balance is insufficient. Requires passing a morphoClient in the options.
getAddress()
Get the payment address (Safe account if accountAddress is set, otherwise EOA).
getSpendingTracker()
Get the current daily spending tracker state.
getRemainingDailyAllowance()
Get remaining daily spending allowance in USDC (human-readable string).
Response Type
All methods returnX402Response<T>:
Spending Controls
Daily Spending Cap
Set
dailySpendLimitUsdc to cap total auto-draw borrows per day. The client rejects payments that would exceed the limit.Auto-Yield Waterfall
When
autoYield: true, the client tries to withdraw from supply positions (earned yield) before borrowing new USDC from Morpho. This minimizes unnecessary borrowing.Validator Module
Set
validatorModule to the Agether8004ValidationModule address for EIP-1271 signature prefixing on x402 payments.Spending Tracker
Daily borrows are tracked internally. Resets at midnight UTC. Query via
getSpendingTracker() or getRemainingDailyAllowance().EIP-1271 Smart Wallet Signatures
Since the Safe account is a smart wallet (not an EOA), theX402Client uses EIP-1271 for signature validation. When accountAddress is set:
- The
fromfield in the EIP-3009 authorization is set to the Safe account - The signature is padded to >65 bytes to trigger USDC’s
bytesoverload oftransferWithAuthorization - USDC calls
isValidSignatureon the Safe account. The Safe7579 adapter routes the call toAgether8004ValidationModule.isValidSignatureWithSender(), which recovers the signer via ECDSA and checks it matchesidentityRegistry.ownerOf(agentId)— the current ERC-8004 NFT holder.
This padding is handled automatically by the X402Client. No manual intervention needed.
Safe Account Requirement
TheX402Client uses the Safe account (smart wallet) as the payment source, not the EOA. The account must have USDC. Options to fund it:

