Payments (x402) — @lucid-fdn/pay
The@lucid-fdn/pay package provides a drop-in replacement for the fetch() function, enabling automatic x402 payments for AI agents. It supports multiple blockchain networks, including Base, Ethereum, Arbitrum, Optimism, Polygon, ApeChain, Monad, Solana, and Sui.
Installation
To get started, install the package using npm:Quick Start
Here’s a quick example to demonstrate how to use@lucid-fdn/pay:
How It Works
- Your request is sent to the API as a normal
fetch()call. - If the server returns HTTP 402, the response body contains an x402 v2
options[]array — one entry per supported chain and facilitator, each with the recipient address, token, amount, and chain. @lucid-fdn/payselects the option matching your wallet’s chain (or falls back to the first option), checks the amount against your budget, sends an on-chain USDC transfer, and retries withX-Payment-Proof(tx hash) andX-Payment-Chainheaders.- The server delegates verification to the appropriate facilitator and returns the response.
Supported Chains
| Chain | Token | Facilitators |
|---|---|---|
| Base | USDC | Direct, Coinbase, PayAI, Thirdweb |
| Ethereum | USDC | PayAI, Thirdweb |
| Arbitrum | USDC | PayAI, Thirdweb |
| Optimism | USDC | PayAI, Thirdweb |
| Polygon | USDC | Thirdweb |
| ApeChain | USDC | Thirdweb |
| Monad | USDC | Direct, PayAI, Thirdweb |
| Solana | USDC | PayAI |
| Sui | USDC | Sui (native) |
API Reference
createLucidFetch(config): lucidFetch
This function returns a configured fetch function that automatically handles x402 payments.
lucidFetch(url, init?, config?)
A one-shot convenience function that accepts an inline config as the third argument. If no config is provided, it behaves as a standard fetch().
Configuration (LucidPayConfig)
| Option | Type | Default | Description |
|---|---|---|---|
wallet | PaymentWallet | required | Wallet adapter used for payments |
maxAmountPerRequest | string | no limit | Maximum amount per request in micro-units (USDC has 6 decimals, so "1000000" = 1 USDC) |
maxRetries | number | 1 | Number of retries after payment |
onPayment | (info: PaymentInfo) => void | — | Called after a successful payment |
onPaymentSkipped | (info) => void | — | Called when a payment exceeds the budget |
Per-Request Overrides (LucidFetchOptions)
This extends the standard RequestInit and allows you to override the wallet or budget on individual requests:
PaymentInfo
The object passed to the onPayment callback includes:
| Field | Type | Description |
|---|---|---|
txHash | string | On-chain transaction hash |
amount | string | Amount paid (micro-units) |
recipient | string | Payment recipient address |
chain | string | Chain identifier (e.g. "base-sepolia") |
token | string | Token symbol (e.g. "USDC") |
url | string | The URL that required payment |
Wallets
ViemWallet (production)
This wallet wraps a viem WalletClient to sign and send real ERC-20 transfers on-chain. Consumers need to bring their own viem dependency.
MemoryWallet (testing)
An in-memory wallet that records payments without sending real transactions. This is useful for tests and local development.
Custom Wallets
You can implement thePaymentWallet interface to use any signing backend:
.png?fit=max&auto=format&n=VsjUqn6fLqEhBiuI&q=85&s=8b4c7e6431e9a6af1ef23b77bb4ff5fd)
.png?fit=max&auto=format&n=VsjUqn6fLqEhBiuI&q=85&s=d5651a45e4bfbabc33f74e146af3f94a)