Skip to content

Repository files navigation

x402 OpenAI Python

Drop-in OpenAI Python client with transparent x402 payment support.

PyPIPython 3.11+CILicense


Wrap the standard openai.OpenAI client with a crypto wallet. When the server responds with HTTP 402, the library automatically signs and retries the request — zero code changes needed.

Installation

pip install x402-openai[evm] # Ethereum / Base / …
pip install x402-openai[svm] # Solana
pip install x402-openai[all] # all chains

Quick Start

fromx402_openaiimportX402OpenAIfromx402_openai.walletsimportEvmWalletclient=X402OpenAI(wallet=EvmWallet(private_key="0x…"))
res=client.chat.completions.create(
model="openai/gpt-4o-mini",
messages=[{"role": "user", "content": "Hello!"}],
)
print(res.choices[0].message.content)

Swap EvmWallet for SvmWallet to pay on Solana — the API is identical.

Usage

Async & Streaming

fromx402_openaiimportAsyncX402OpenAIclient=AsyncX402OpenAI(wallet=EvmWallet(private_key="0x…"))
stream=awaitclient.chat.completions.create(
model="openai/gpt-4o-mini",
messages=[{"role": "user", "content": "Explain x402"}],
stream=True,
)
asyncforchunkinstream:
ifchunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")

Multi-chain

fromx402_openai.walletsimportEvmWallet, SvmWalletclient=X402OpenAI(wallets=[
EvmWallet(private_key="0x…"),
SvmWallet(private_key="base58…"),
])

BIP-39 Mnemonic (EVM)

wallet=EvmWallet(mnemonic="word1 word2 … word12")
wallet=EvmWallet(mnemonic="…", account_index=2) # m/44'/60'/0'/0/2wallet=EvmWallet(mnemonic="…", derivation_path="m/44'/60'/2'/0/0") # custom path

The protocol selects the right chain automatically based on the server's payment requirements.

Payment Policies

Use policies to control which chain or scheme is preferred when multiple payment options are available:

fromx402_openaiimportX402OpenAI, prefer_network, prefer_scheme, max_amountfromx402_openai.walletsimportEvmWallet, SvmWalletclient=X402OpenAI(
wallets=[
EvmWallet(private_key="0x…"),
SvmWallet(private_key="base58…"),
],
policies=[
prefer_network("eip155:8453"), # Prefer Base mainnetprefer_scheme("exact"), # Prefer exact payment schememax_amount(1_000_000), # Cap at 1 USDC (6 decimals)
],
)

API Reference

X402OpenAI / AsyncX402OpenAI

Drop-in replacement for openai.OpenAI / openai.AsyncOpenAI. Provide exactly one credential source:

ParameterTypeDescription
walletWalletSingle wallet adapter
walletslist[Wallet]Multiple adapters (multi-chain)
policieslist[Policy]Payment policies (chain/scheme preference, amount cap)
x402_clientx402HTTPClient*Pre-configured x402 client (bypasses policies)

All standard OpenAI kwargs (base_url, timeout, max_retries, …) are forwarded. Default base_url: https://llm.qntx.org/v1

Wallet Adapters

ClassChainExtra
EvmWallet(private_key=…)EVMx402-openai[evm]
EvmWallet(mnemonic=…)EVM (BIP-39)x402-openai[evm]
SvmWallet(private_key=…)Solanax402-openai[svm]

Implement the Wallet protocol to add a new chain.

Low-level Transports

X402Transport / AsyncX402Transport — httpx transports for manual wiring into any httpx.Client.

Examples

See the examples/ directory. Each script is self-contained:

EVM_PRIVATE_KEY="0x…" python examples/chat_evm.py
SOLANA_PRIVATE_KEY="base58…" python examples/chat_svm.py
EVM_PRIVATE_KEY="0x…" python examples/streaming_evm.py
MNEMONIC="word1 word2 …" python examples/chat_evm_mnemonic.py
EVM_PRIVATE_KEY="0x…" python examples/chat_evm_policy.py
EVM_PRIVATE_KEY="0x…" python examples/streaming_evm_policy.py

License

This project is licensed under the MIT License.


A QuantX open-source project.

QuantX

Code is law. We write both.

About

Drop-in OpenAI Python client with transparent x402 payment support.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

262 stars

Watchers

6 watching

Forks

Releases

Sponsor this project

Contributors

Languages