20 min
Real-World Client Implementation
Learning Objectives
- Implement production-ready error handling
- Add retry logic with exponential backoff
- Monitor wallet balance
- Log payment transactions
Production Checklist
Before deploying your x402 client to production, make sure you:
•Use environment variables for private keys (never commit!)
•Add retry logic with exponential backoff
•Log payment transactions for debugging
•Monitor wallet balance and alert when low
•Cache responses when appropriate
typescript
import { wrapFetchWithPayment } from "@x402/fetch";
import { x402Client } from "@x402/core/client";
import { registerExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";
// Setup with production configuration
const evmSigner = privateKeyToAccount(process.env.X402_PRIVATE_KEY as `0x${string}`);
const client = new x402Client();
// Register payment scheme
registerExactEvmScheme(client, { signer: evmSigner });
// Wrap fetch with payment capabilities
const fetchWithPayment = wrapFetchWithPayment(fetch, client);
// Helper function for typed API calls
async function callPaidAPI<T>(url: string): Promise<T> {
try {
const response = await fetchWithPayment(url);
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
return await response.json();
} catch (error) {
// Handle errors appropriately
throw error;
}
}Build a CLI tool that calls a paid weather API
Complete the TODOs to create a working CLI tool
Requirements:
Fetches weather data
CLI successfully calls the weather API
Logs payment transaction
Transaction hash is logged to console
Your Solution