Wrap the standard openai.OpenAI client with per-chain private keys.
When the server responds with HTTP 402, the library automatically signs and retries the request — zero code changes needed.
Supplying evm registers both exact and upto. svm and tvm register exact only. Default spend controls from x402 cap each payment at $1 of a recognized default asset.
Installation
pip install 'x402-openai[evm]' # EVM (Ethereum / Base / …)
pip install 'x402-openai[svm]' # Solana
pip install 'x402-openai[tvm]' # TVM (TON)
pip install 'x402-openai[all]' # all chains
Quick Start
from x402_openai import X402OpenAI
client = X402OpenAI(evm="0x…")
res = client.chat.completions.create(
model="openai/gpt-4o-mini",
messages=[{"role": "user", "content": "Hello!"}],
)
print(res.choices[0].message.content)
Pass svm="base58…" instead of evm to pay on Solana — the rest of the API is identical. The same constructor accepts tvm.
Usage
Streaming
from x402_openai import AsyncX402OpenAI
client = AsyncX402OpenAI(evm="0x…")
stream = await client.chat.completions.create(
model="openai/gpt-4o-mini",
messages=[{"role": "user", "content": "Explain x402"}],
stream=True,
)
async for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
Multi-chain
client = X402OpenAI(
evm="0x…",
svm="base58…",
tvm="hex-or-base64…",
)
The protocol selects the right chain automatically based on the server's payment requirements.
Key formats
| Option | Key material |
|---|---|
evm |
0x hex secp256k1 |
svm |
base58 64-byte secret |
tvm |
hex/base64 32-byte seed or 64-byte secret |
Bare evm / svm / tvm strings become { private_key }. Empty strings throw. Config objects are EvmConfig / SvmConfig (private_key, optional rpc_url) and TvmConfig (private_key, optional network / provider / api_key / provider_base_url).
TVM
TVM registers exact only on a concrete CAIP-2. Default network is tvm:-239 (or pass network="tvm:-3"). The client never registers tvm:* — the signer is bound to one network.
- Hex/base64 32-byte seed or 64-byte secret, or
TvmConfig(private_key, network?, provider?, api_key?, provider_base_url?). - The 402 must set
extra.areFeesSponsored is True. - Default asset is USDT jetton; native TON is not a default asset — pass
spend_controls.allowed_assetsto allow it.
Long-lived TVM clients hold ExactTvmScheme HTTP clients. Call client.close() (sync) or await client.aclose() (async) when finished. close() before the first request is a no-op. A request after close() raises X402OpenAI is closed and does not rebuild.
Spend controls
x402Client() / x402ClientSync() already allow only default (USD-pegged) assets and cap each payment at $1. This package does not change that default.
Pass spend_controls to raise the cap, allow extra assets, or disable controls:
client = X402OpenAI(
evm="0x…",
spend_controls={"max_amount_per_payment": "$5"},
)
- Omit
spend_controlsto keep the official$1+ default-asset allowlist. spend_controls=Falsedisables allowlist and caps.- Gateway prices above
$1require the caller to raisemax_amount_per_payment.
exact and upto
evm registers ExactEvmScheme and UptoEvmScheme on eip155:*. svm registers ExactSvmScheme on solana:* (no Python upto). tvm registers ExactTvmScheme on the configured CAIP-2. No extra flag; the gateway is not probed.
- EVM
upto: Permit2 (permitWitnessTransferFrom). The 402 must includeextra.facilitatorAddress. Pass{ rpc_url }onevmto enable official EIP-2612 / ERC-20 approval sponsoring. The 402amountis the authorized maximum; the client signs that max (the server may charge<=max at settle). If the ceiling exceeds spend controls, payment creation throws. - SVM
exact: the 402 must includeextra.feePayer. There is no SVMuptoscheme in Pythonx402.
from x402_openai import X402OpenAI, prefer_scheme
client = X402OpenAI(
evm="0x…",
policies=[prefer_scheme("upto")],
)
prefer_scheme("upto") only affects chains that registered upto (EVM). An SVM-only client still pays exact.
Payment Policies
Use policies to prefer a chain or scheme when multiple options remain after spend controls. Policies do not cap spend.
from x402_openai import X402OpenAI, prefer_network, prefer_scheme
client = X402OpenAI(
evm="0x…",
svm="base58…",
policies=[
prefer_network("eip155:8453"), # Prefer Base mainnet
prefer_scheme("upto"),
],
)
If nothing matches, all remaining options pass through. If any upto requirement remains, prefer_scheme("upto") keeps only those (EVM); otherwise the list passes through and SVM can pay exact.
Closing
client.close() # X402OpenAI
await client.aclose() # AsyncX402OpenAI
close() / aclose() dispose TVM ExactTvmScheme HTTP clients. Close before the first request is a no-op. A request after close raises X402OpenAI is closed and does not rebuild.
API Reference
X402OpenAI / AsyncX402OpenAI
Drop-in replacement for openai.OpenAI / openai.AsyncOpenAI. Provide at least one of evm, svm, tvm, or x402_client:
| Parameter | Type | Description |
|---|---|---|
evm |
str or EvmConfig |
EVM secp256k1 private key (0x hex). Registers exact and upto on eip155:*. |
svm |
str or SvmConfig |
Solana base58 secret key. Registers exact only on solana:*. |
tvm |
str or TvmConfig |
TON seed/secret. Registers exact on tvm:-239 by default (tvm:-3 if set). Never tvm:*. |
spend_controls |
SpendControls or False |
Official spend controls. Omit for $1 + default assets. |
policies |
list[Policy] |
Preference policies (prefer_network / prefer_scheme). |
payment_requirements_selector |
Selector |
Picks among remaining requirements after spend controls and policies. |
x402_client |
x402ClientSync / x402Client |
Pre-configured core x402 client (exclusive with keys, spend_controls, policies, payment_requirements_selector). |
| Type | Fields | Notes |
|---|---|---|
EvmConfig |
{ private_key, rpc_url? } |
rpc_url enables EIP-2612 / ERC-20 approval sponsoring |
SvmConfig |
{ private_key, rpc_url? } |
rpc_url is Solana JSON-RPC |
TvmConfig |
{ private_key, network?, provider?, api_key?, provider_base_url? } |
network is tvm:-239 or tvm:-3 |
Empty keys throw.
close() / aclose() release TVM handles. Close before the first request is a no-op. A request after close raises X402OpenAI is closed and does not rebuild.
SpendControls is the official snake_case TypedDict from x402.
All standard OpenAI options (base_url, timeout, max_retries, …) are forwarded. Default base_url: https://llm.qntx.org/v1. api_key defaults to "x402". http_client is not accepted.
| Option | Chain | Install extra |
|---|---|---|
evm |
EVM | x402-openai[evm] |
svm |
Solana | x402-openai[svm] |
tvm |
TVM | x402-openai[tvm] |
License
This project is licensed under the MIT License.
Metadata
Release files for x402-openai 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| x402_openai-1.0.0.tar.gz | 171.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| x402_openai-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 186.7 kB
Release files / x402_openai-1.0.0.tar.gz
| Download URL | x402_openai-1.0.0.tar.gz |
|---|---|
| Size | 171.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
598b2396fa5e39e60b75d7f1069a533068448c895ae059af5f76d507ab940437
|
|
BLAKE2b-256 checksum How to use checksums |
948038f7e1486acbeff8d8f519c340479759b86b1ac2f80bbd7a0fb9e28759e3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","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":true}
|
Release files / x402_openai-1.0.0-py3-none-any.whl
| Download URL | x402_openai-1.0.0-py3-none-any.whl |
|---|---|
| Size | 15.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
075e19f752417ace9ffa7914324ba715f604ee612ed60b9782687d576c8ee5fc
|
|
BLAKE2b-256 checksum How to use checksums |
0ab4f0ff0ab72e4407bba9e50a4017e73acf1fa083d5ca6bc5173be07fadf637
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","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":true}
|