Pay for x402 APIs with 3 lines of Python — no wallet needed
Project description
x402-pay
Call any x402 API with one line of Python. No wallet needed.
import x402_pay
resp = x402_pay.get("https://weather.hugen.tokyo/weather/current?city=Tokyo")
print(resp.json())
Auto-provisions an API key with $0.05 trial credit on first use. Routes requests through a broker that handles on-chain payment on your behalf.
Install
pip install x402-pay
Usage
One-liner (sync)
import x402_pay
resp = x402_pay.get("https://weather.hugen.tokyo/weather/current?city=Tokyo")
print(resp.json())
resp = x402_pay.post("https://defi.hugen.tokyo/defi/simulate", json={
"chain_id": "1", "from": "0xABC...", "to": "0xDEF...",
})
Async client
from x402_pay import PayClient
async with PayClient() as client:
resp = await client.get("https://weather.hugen.tokyo/weather/current?city=Tokyo")
print(resp.json())
print(f"Balance: ${await client.balance():.2f}")
Explicit API key
from x402_pay import PayClient
async with PayClient(api_key="gw_YOUR_KEY") as client:
resp = await client.get("https://defi.hugen.tokyo/defi/token?chain=ethereum&address=0x...")
Wallet mode (power users)
pip install x402-pay[wallet]
from x402_pay import DirectClient
async with DirectClient(private_key="0x...") as client:
resp = await client.get("https://weather.hugen.tokyo/weather/current?city=Tokyo")
API Key Lifecycle
- First call auto-creates a key with $0.05 trial balance (10 calls at $0.005)
- Key saved to
~/.x402-pay/config.json - After trial: top up at the URL from
await client.topup_url()($1.00 x402 payment) - Or bring your own key:
PayClient(api_key="gw_...")
Configuration
| Variable | Default | Description |
|---|---|---|
X402_API_KEY |
auto-created | API key for broker mode |
X402_BROKER_URL |
https://discovery.hugen.tokyo |
Broker endpoint |
X402_PAY_CONFIG_DIR |
~/.x402-pay |
Config directory |
API Reference
Module-level (sync)
x402_pay.get(url, **kwargs)-- sync GET through brokerx402_pay.post(url, **kwargs)-- sync POST through brokerx402_pay.balance(api_key="")-- check balance
PayClient (async)
PayClient(api_key="", broker_url="", timeout=60.0)await client.get(url, **kwargs)-- GET through brokerawait client.post(url, **kwargs)-- POST through brokerawait client.balance()-- balance in USDawait client.topup_url()-- URL to top up key balance
DirectClient (async, requires [wallet])
DirectClient(private_key="0x...")await client.get(url)/await client.post(url)-- direct x402 payment
Exceptions
PayError-- base exceptionInsufficientBalance-- key balance too low (.balance,.needed,.topup_url)NotInCatalog-- URL not in x402 catalog (.url)BrokerError-- upstream call failed (.refunded)
License
MIT
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
x402_pay-0.2.1.tar.gz
(13.4 kB
view details)
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
x402_pay-0.2.1-py3-none-any.whl
(11.7 kB
view details)
File details
Details for the file x402_pay-0.2.1.tar.gz.
File metadata
- Download URL: x402_pay-0.2.1.tar.gz
- Upload date:
- Size: 13.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e3e8477a9726fc67826ad59dc3dbbe8aefde2bb73ad0dd37c9c6c1f12978ed11
|
|
| MD5 |
3e6d5023e7696c895613c28105538f20
|
|
| BLAKE2b-256 |
066d8c023ff03ebdd3bbb5fdb78bd9d8a8b3acc2655a2465cad292e97683ec38
|
File details
Details for the file x402_pay-0.2.1-py3-none-any.whl.
File metadata
- Download URL: x402_pay-0.2.1-py3-none-any.whl
- Upload date:
- Size: 11.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a449c894420eec1bb0955ff0dba80740f2993d253ee79007b1b04b5545db1142
|
|
| MD5 |
5c4911ae96594a7905f6cf29db9a62c0
|
|
| BLAKE2b-256 |
7664c483f84bf36340ba9586e4e2f61303ddd9d99e33100db7d6e012ae51a3a0
|