Python Examples

Complete examples for building autonomous AI agents with x402 payment capabilities using OpenClaw and Clawmarket.

Installation

# Install OpenClaw and Clawmarket provider
pip install openclaw clawmarket

# For Solana wallet management
pip install solana

Quick Start

Configure Agent Wallet

# Generate Solana wallet for agent
solana-keygen new -o ~/.config/solana/agent.json

# Fund wallet with SOL for transaction fees
solana airdrop 1 ~/.config/solana/agent.json --url devnet

# Set environment variables
export CLAWMARKET_WALLET_PATH=~/.config/solana/agent.json
export CLAWMARKET_NETWORK=devnet

Basic Agent with Payment Capabilities

OpenClaw Provider Integration

Initialize Provider with Approval Policies

Using MCP Tools Directly

Autonomous Agent Use Cases

1. Invoice Settlement Agent

2. SaaS Subscription Agent

3. Resource Allocation Agent

Standalone MCP Server Mode

Launch Clawmarket as MCP Server

Connect Custom Agent to MCP Server

WebSocket Transaction Monitoring

Real-Time Transaction Updates

Error Handling for Agent Workflows

Robust Transaction Execution

Advanced Agent Patterns

Multi-Agent Coordination

Best Practices

  1. Approval Policies: Always configure approval policies to limit agent transaction exposure

  2. Idempotency Keys: Use idempotency keys for all payment operations to prevent duplicates

  3. Error Handling: Implement retry logic with exponential backoff for transient errors

  4. Monitoring: Set up WebSocket streams to monitor agent transactions in real-time

  5. Wallet Security: Store agent wallet keys in secure key management systems (HSM, Vault)

  6. Audit Logging: Log all agent payment decisions and transactions for compliance

  7. Testing: Test agent workflows on devnet before deploying to mainnet

Next Steps

Last updated