Pay for x402 APIs with 3 lines of Python — no wallet needed
Project description
x402-pay
Call any x402 API with one API key. No per-API wallet integration needed.
from x402_pay import PayClient
async with PayClient(api_key="gw_your_topped_up_key") as client:
resp = await client.get("https://weather.hugen.tokyo/weather/current?city=Tokyo")
print(resp.json())
Routes requests through a broker that handles on-chain payment on your behalf. $0.01/call.
Install
pip install x402-pay
Modes
API Key Mode (default, no wallet)
Get a key and top it up first:
# Create key (free, gets $0.05 search credit)
curl -X POST https://discovery.hugen.tokyo/keys/create
# Top up to unlock broker ($1.00 x402 payment)
curl -X POST "https://discovery.hugen.tokyo/keys/topup?key=gw_YOUR_KEY"
Then use it:
from x402_pay import PayClient
async with PayClient(api_key="gw_YOUR_KEY") as client:
# Any x402 API — broker pays upstream, deducts $0.01 from your key
resp = await client.get("https://weather.hugen.tokyo/weather/current?city=Tokyo")
print(resp.json())
# Check remaining balance
print(f"${await client.balance():.2f}")
Wallet Mode (power users)
pip install x402-pay[wallet]
from x402_pay import DirectClient
async with DirectClient(private_key="0x...") as client:
# Pay upstream directly from your wallet — no broker fee
resp = await client.get("https://weather.hugen.tokyo/weather/current?city=Tokyo")
print(resp.json())
Configuration
API keys are stored in ~/.x402-pay/config.json. Override with environment variables:
| 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
PayClient
PayClient(api_key="", broker_url="", timeout=60.0)— create clientawait client.get(url)— GET request through brokerawait client.post(url, json={...})— POST request through brokerawait client.balance()— check key balance in USDclient.api_key— current API key
DirectClient
DirectClient(private_key="0x...")— create wallet clientawait 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
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_pay-0.1.0.tar.gz.
File metadata
- Download URL: x402_pay-0.1.0.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
71b6e554c13de510d97f060f26334a52107ddb325b1099675ce0dd035925a93c
|
|
| MD5 |
d6c6fcfada1ddfd154d8fb4698a2056f
|
|
| BLAKE2b-256 |
9d2f1faf340a02b8c6255227a6086225748f7bf9ac99ca75e411f6249134b9be
|
File details
Details for the file x402_pay-0.1.0-py3-none-any.whl.
File metadata
- Download URL: x402_pay-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.6 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 |
2373cdec69312c72bf146d0cd56ff267a29999f93cc8c8d59538a8c4997610c1
|
|
| MD5 |
ecaa1fcaab01e3f92edfa15bb144aa5a
|
|
| BLAKE2b-256 |
1d0d0d4952539c2d9635050f0dda75713d86770dfcc8ede5212c712eb67e5533
|