OpenLibx402 Client¶
HTTP client library for making X402-enabled API calls with automatic payment handling.
Installation¶
Features¶
- Explicit Client: Manual control over payment flow
- Implicit Client: Automatic payment handling with configurable retries
- Support for all HTTP methods (GET, POST, PUT, DELETE)
- Safety limits with
max_payment_amount - URL validation and SSRF protection
- Local development support with
allow_local - Proper connection cleanup and security
Usage¶
Explicit Client (Manual Control)¶
Local Development¶
For local development with localhost URLs:
Implicit Client (Auto-Payment)¶
Disable Auto-Retry¶
Security Notes¶
- Always call
close()when done to properly cleanup connections and sensitive data - Private keys are held in memory - ensure proper disposal
- Only use URLs from trusted sources to prevent SSRF attacks
- Default RPC URL is devnet - use mainnet URL for production
- Set
allow_local=Truefor local development (localhost URLs) - NEVER use
allow_local=Truein production deployments
API Reference¶
X402Client¶
get(url, payment=None, **kwargs)- GET requestpost(url, payment=None, **kwargs)- POST requestput(url, payment=None, **kwargs)- PUT requestdelete(url, payment=None, **kwargs)- DELETE requestrequest(method, url, payment=None, **kwargs)- Generic HTTP requestpayment_required(response)- Check if response requires paymentparse_payment_request(response)- Parse payment request from 402 responsecreate_payment(request, amount=None)- Create and broadcast paymentclose()- Cleanup connections and sensitive data
X402AutoClient¶
fetch(url, method="GET", auto_retry=None, **kwargs)- Request with auto-paymentget(url, auto_retry=None, **kwargs)- GET with auto-paymentpost(url, auto_retry=None, **kwargs)- POST with auto-paymentput(url, auto_retry=None, **kwargs)- PUT with auto-paymentdelete(url, auto_retry=None, **kwargs)- DELETE with auto-paymentclose()- Cleanup connections
License¶
MIT