Build an Autonomous Payment Agent
Learn how to create an AI agent that can autonomously discover paid services, make payment decisions, and execute USDC transactions—all without manual intervention.Time to complete: 30-40 minutesWhat you’ll build: A Claude-powered agent that browses the web, finds paid APIs, and autonomously pays to access them
Prerequisites
Crossmint API Key
Get from Crossmint Console
Anthropic API Key
For Claude AI from Anthropic
Node.js 18+
TypeScript development environment
Testnet USDC
From Circle Faucet
What You’ll Learn
- Creating Crossmint smart wallets programmatically
- Integrating x402 payment clients with AI agents
- Handling 402 responses automatically
- Building payment confirmation flows
- Managing agent budgets and spending limits
Architecture Overview
Step 1: Project Setup
tsconfig.json:
tsconfig.json
.env:
.env
Step 2: Create the Wallet Service
Create a module for managing the agent’s Crossmint wallet:src/wallet.ts
Step 3: x402 Adapter
Create the adapter to bridge Crossmint wallets with x402:src/x402Adapter.ts
Step 4: Payment Client
Create a client that handles x402 payments automatically:src/paymentClient.ts
Step 5: AI Agent Core
Create the Claude-powered agent:src/agent.ts
Step 6: Main Application
Create the entry point:src/index.ts
package.json:
Step 7: Testing the Agent
Get Testnet USDC
-
Run the agent to get wallet address:
- Copy the wallet address from output
- Visit Circle Faucet
- Select “Base Sepolia” and paste your address
- Mint 1 USDC
Example Conversations
Advanced Features
1. Payment Strategies
Implement different decision strategies:2. Transaction History
Track all payments:3. Retry Logic
Handle failed payments:Production Considerations
Security
Security
- API Key Management: Use environment variables, never commit keys
- Budget Limits: Set hard limits to prevent overspending
- Wallet Security: Crossmint handles custody, but limit API key permissions
- Rate Limiting: Implement request throttling to prevent abuse
Monitoring
Monitoring
- Spending Alerts: Notify when budget thresholds are reached
- Transaction Logs: Store all payments in a database
- Error Tracking: Use services like Sentry for error monitoring
- Performance: Track payment verification latency
Optimization
Optimization
- Caching: Cache paid API responses to avoid duplicate payments
- Batch Requests: Combine multiple queries when possible
- Gas Optimization: Use Base mainnet for lower fees
- Payment Pooling: Aggregate small payments for efficiency
Next Steps
Durable Objects
Deploy stateful agents with Cloudflare
Event RSVP
See a production multi-tenant agent
MCP Protocol
Build tool-calling agents with MCP
x402 Protocol
Deep dive into payment protocol
Troubleshooting
Agent approves payment but no TX hash
Agent approves payment but no TX hash
- Check facilitator is responding
- Verify wallet has USDC balance
- Ensure network matches (testnet vs mainnet)
- Check Base RPC is accessible
Claude refuses to use tools
Claude refuses to use tools
- Ensure tools are properly registered
- Check prompt includes task that requires tools
- Verify Anthropic API key is valid
- Try more explicit instructions
Wallet deployment fails
Wallet deployment fails
- Crossmint wallets work before deployment (ERC-6492)
- Deployment happens automatically on first transaction
- Check wallet has ETH for gas (though usually sponsored)
- Verify network configuration