Python SDK for x402 Bazaar — autonomous API marketplace with auto-payment
Project description
x402-bazaar
Python SDK for x402 Bazaar — the autonomous API marketplace with automatic USDC payments.
No other SDK combines crypto payments + API marketplace + AI agent tools in one package.
Quick Start
pip install x402-bazaar
from x402_bazaar import X402Client
# Zero-config: auto-generates wallet on SKALE (gas-free)
client = X402Client()
# Search for APIs
results = client.search("weather")
# Call with auto-payment (402 detection + USDC payment + retry)
data = client.call("weather-api", params={"city": "Paris"})
print(data.data)
That's it. The SDK handles wallet creation, payment detection, USDC transfers, and retries — all invisible.
Features
- Auto-payment — transparent HTTP 402 handling (detect, pay, retry)
- Multi-chain — Base, Polygon (EIP-3009 gas-free), SKALE (gas-free)
- Wallet management — auto-generate, import, AES-256-GCM encryption
- Budget control — daily/weekly/monthly spending limits with auto-reset
- Response validation — HMAC-SHA256 signature verification + quality score check
- Service protection — auto-blacklist malicious/empty services (TTL-based)
- Async native — sync + async with httpx
- Type-safe — full type hints, Pydantic models,
py.typed(PEP 561) - AI integrations — LangChain and CrewAI tools out-of-the-box
- Zero web3.py — lightweight JSON-RPC client, no heavy dependencies
Configuration
from x402_bazaar import X402Client
# Explicit wallet + chain
client = X402Client(
private_key="0x...",
chain="base", # "base", "polygon", "skale", "base-sepolia"
budget={"max": 5.0, "period": "daily"},
timeout=30, # seconds
validation_secret="my-hmac-key", # optional HMAC verification
)
# From encrypted wallet file
client = X402Client.from_encrypted("wallet.json", password="secret")
# Custom backend
client = X402Client(base_url="https://your-bazaar-instance.com")
Async Usage
async with X402Client(private_key="0x...") as client:
results = await client.search_async("jokes")
data = await client.call_async("joke-api")
health = await client.health_async()
balance = await client.get_balance_async()
All methods have an _async variant.
API Reference
Core Methods
| Method | Description |
|---|---|
search(query) |
Search APIs by keyword |
list_services(page, limit) |
List all APIs with pagination |
get_service(service_id) |
Get service details |
call(service_id, params) |
Call API with auto-payment |
get_balance(chain?) |
Get USDC balance (one or all chains) |
health() |
Backend health check |
Budget & Wallet
| Method | Description |
|---|---|
set_budget(max_daily=5.0) |
Set spending limit |
get_budget_status() |
Check spent/remaining/period |
fund_wallet() |
Get funding instructions for your chain |
claim_faucet() |
Claim free SKALE CREDITS |
Wallet Utilities
from x402_bazaar import generate_wallet, encrypt_wallet, decrypt_wallet
wallet = generate_wallet()
print(wallet.address) # 0x...
encrypt_wallet(wallet.private_key, "wallet.json", password="secret")
restored = decrypt_wallet("wallet.json", password="secret")
Response Validation
The SDK can verify response integrity using HMAC-SHA256 signatures:
client = X402Client(
private_key="0x...",
validation_secret="shared-secret",
)
# If the backend signs responses with the same secret,
# the SDK verifies automatically. Mismatches → service blacklisted.
Additionally, the SDK checks response quality: if a server claims high quality but returns empty data, the service is auto-blacklisted for 10 minutes.
LangChain Integration
pip install x402-bazaar[langchain]
from x402_bazaar import X402Client
from x402_bazaar.integrations.langchain import X402SearchTool, X402CallTool
client = X402Client()
tools = [X402SearchTool(client=client), X402CallTool(client=client)]
# Use with any LangChain agent
from langchain.agents import initialize_agent
agent = initialize_agent(tools=tools, llm=llm)
agent.run("Find a joke API and get me a joke")
CrewAI Integration
pip install x402-bazaar[crewai]
from x402_bazaar import X402Client
from x402_bazaar.integrations.crewai import X402SearchTool, X402CallTool
client = X402Client()
researcher = Agent(
tools=[X402SearchTool(client=client), X402CallTool(client=client)],
goal="Research data using paid APIs"
)
Balance & Budget
# Check USDC balance on all chains
balances = client.get_balance()
# {"base": 12.5, "polygon": 3.0, "skale": 5.0, "base-sepolia": 0.0}
# Single chain
balance = client.get_balance(chain="base")
# {"base": 12.5}
# Set spending limits
client.set_budget(max_daily=5.0)
# or: max_weekly=25.0, max_monthly=100.0
# Check budget status
status = client.get_budget_status()
print(f"Spent: ${status.spent}, Remaining: ${status.remaining}, Calls: {status.call_count}")
# Get funding instructions
info = client.fund_wallet()
print(info["instructions"])
Supported Chains
| Chain | Gas Cost | Speed | Notes |
|---|---|---|---|
| SKALE | Free (CREDITS) | ~1s | Best for getting started |
| Base | ~$0.001 ETH | ~2s | Coinbase ecosystem |
| Polygon | ~$0.001 POL | ~3s | EIP-3009 gas-free payments |
| Base Sepolia | Free (testnet) | ~1s | Testing only |
Error Handling
from x402_bazaar import X402Client
from x402_bazaar.exceptions import (
InsufficientBalanceError,
BudgetExceededError,
ApiError,
TimeoutError,
)
client = X402Client()
try:
data = client.call("expensive-api")
except InsufficientBalanceError as e:
print(f"Need {e.required} USDC, have {e.available}")
except BudgetExceededError as e:
print(f"Budget limit: {e.limit} USDC ({e.period})")
except ApiError as e:
print(f"API error {e.status_code}: {e}")
except TimeoutError as e:
print(f"Timed out after {e.timeout_ms}ms")
How Payment Works
client.call("service")sends a request with no payment headers- Backend returns HTTP 402 with payment details (amount, recipient, chain)
- SDK checks local budget limits
- SDK checks on-chain USDC balance
- SDK signs and sends USDC transfer (or EIP-3009 for Polygon)
- SDK retries the request with
X-Payment-TxHashheader - Backend verifies payment on-chain and returns the API response
All of this happens in a single client.call() invocation.
Development
git clone https://github.com/Wintyx57/x402-sdk-python.git
cd x402-sdk-python
pip install -e ".[dev]"
# Run unit tests
pytest tests/ -m "not integration" -v
# Run integration tests (requires internet)
pytest tests/ -m integration -v
# Run all tests
pytest tests/ -v
# Lint
ruff check x402_bazaar/
# Type check
mypy x402_bazaar/ --ignore-missing-imports
License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file x402_bazaar-1.2.0.tar.gz.
File metadata
- Download URL: x402_bazaar-1.2.0.tar.gz
- Upload date:
- Size: 30.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ce0cd5735b5dacdec3ad4f651a2ae014537361d7f6d8be4642b1e813ca65209
|
|
| MD5 |
fdf6f0fe58d77fccc1b0c48ea6990fa4
|
|
| BLAKE2b-256 |
86c12523c5acf3b47fcfd69ba652dca2894c1ac84843cdfe57c48e2c1db15ac8
|
File details
Details for the file x402_bazaar-1.2.0-py3-none-any.whl.
File metadata
- Download URL: x402_bazaar-1.2.0-py3-none-any.whl
- Upload date:
- Size: 26.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c8d06ff332146ac5f685dc336db7153a48a4cad36488bec376505704217964da
|
|
| MD5 |
d70f34c195f4439f37475efdba7e6555
|
|
| BLAKE2b-256 |
ae6ae6cbbe67e62bbcdd9f193d284ec81c80468ecdf8ea2fcc7a0483df501e2c
|