Skip to main content

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

  1. First call auto-creates a key with $0.05 trial balance (10 calls at $0.005)
  2. Key saved to ~/.x402-pay/config.json
  3. After trial: top up at the URL from await client.topup_url() ($1.00 x402 payment)
  4. 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 broker
  • x402_pay.post(url, **kwargs) -- sync POST through broker
  • x402_pay.balance(api_key="") -- check balance

PayClient (async)

  • PayClient(api_key="", broker_url="", timeout=60.0)
  • await client.get(url, **kwargs) -- GET through broker
  • await client.post(url, **kwargs) -- POST through broker
  • await client.balance() -- balance in USD
  • await 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 exception
  • InsufficientBalance -- 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.3.1.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

x402_pay-0.3.1-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file x402_pay-0.3.1.tar.gz.

File metadata

  • Download URL: x402_pay-0.3.1.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for x402_pay-0.3.1.tar.gz
Algorithm Hash digest
SHA256 bc80ee48633bd596265ecb46f477b89af87eec7e53bea98b3e8c1c83dc1a3312
MD5 4141ea2b11e92608d8032d22e1c4ab49
BLAKE2b-256 434712554d579812a3ee760da2056cbeb5a0d0a431d3b5098d2f80a8d3561afa

See more details on using hashes here.

File details

Details for the file x402_pay-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: x402_pay-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for x402_pay-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 440c2d2aaf3161c480a2bcb638653f0c6d7f7ed5da01d19885cf5d5bc879511e
MD5 1fd8c1226429cc408cdf0bc130e13d77
BLAKE2b-256 11d3064a3ad2c4bd3e2f9c130fea2324382510fdef164a7ebbb879e6f6760dc9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page