Skip to main content

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

Project description

x402-openai

Drop-in OpenAI Python client with transparent x402 payment support — EVM, Solana, and beyond.

Python 3.11+ License

Install

# EVM only
pip install x402-openai[evm]

# Solana (SVM) only
pip install x402-openai[svm]

# Both chains
pip install x402-openai[all]

Quick Start

EVM (Ethereum / Base / …)

from x402_openai import X402OpenAI
from x402_openai.wallets import EvmWallet

client = X402OpenAI(wallet=EvmWallet(private_key="0x…"))

# Use exactly like openai.OpenAI — everything just works!
completion = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello!"}],
)
print(completion.choices[0].message.content)

SVM (Solana)

from x402_openai import X402OpenAI
from x402_openai.wallets import SvmWallet

client = X402OpenAI(wallet=SvmWallet(private_key="base58…"))

Multi-chain

Register multiple wallets — the x402 protocol automatically selects the right chain based on the server's payment requirements.

from x402_openai import X402OpenAI
from x402_openai.wallets import EvmWallet, SvmWallet

client = X402OpenAI(wallets=[
    EvmWallet(private_key="0x…"),
    SvmWallet(private_key="base58…"),
])

EVM mnemonic phrase

from x402_openai.wallets import EvmWallet

wallet = EvmWallet(mnemonic="word1 word2 … word12")

# BIP-44 account #2: m/44'/60'/0'/0/2
wallet = EvmWallet(mnemonic="word1 word2 …", account_index=2)

# Ledger Live derivation path
wallet = EvmWallet(mnemonic="word1 word2 …", derivation_path="m/44'/60'/2'/0/0")

Legacy EVM shortcut (backward compatible)

Existing code using private_key= / mnemonic= directly continues to work:

client = X402OpenAI(private_key="0x…")
client = X402OpenAI(mnemonic="word1 word2 … word12")

Async

from x402_openai import AsyncX402OpenAI
from x402_openai.wallets import SvmWallet

client = AsyncX402OpenAI(wallet=SvmWallet(private_key="base58…"))

completion = await client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello!"}],
)

Streaming

stream = await client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Explain x402"}],
    stream=True,
)
async for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

Advanced: pre-built x402 client

from x402 import x402ClientSync
from x402_openai import X402OpenAI

x402 = x402ClientSync()
# ... register custom payment schemes ...

client = X402OpenAI(x402_client=x402)

Advanced: manual transport injection

import httpx
from openai import AsyncOpenAI
from x402_openai import AsyncX402Transport

transport = AsyncX402Transport(x402_client=x402_http)
client = AsyncOpenAI(
    api_key="x402",
    http_client=httpx.AsyncClient(transport=transport),
)

API

X402OpenAI(*, wallet=None, wallets=None, x402_client=None, **kwargs)

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

Parameter Description
wallet A single Wallet adapter (e.g. EvmWallet, SvmWallet)
wallets List of Wallet adapters for multi-chain support
private_key (legacy) EVM hex key ("0x…")
mnemonic (legacy) BIP-39 phrase (12/24 words)
x402_client Pre-configured x402HTTPClientSync

Legacy mnemonic companions: account_index (default 0), derivation_path, passphrase.

Default base_url is https://llm.qntx.fun/v1. All kwargs (base_url, timeout, max_retries, …) forward to openai.OpenAI.

AsyncX402OpenAI(*, ...)

Async version — identical parameters.

Wallet adapters

Class Chain Install extra
EvmWallet(private_key=…) EVM (Ethereum, Base, …) x402-openai[evm]
EvmWallet(mnemonic=…) EVM (BIP-39) x402-openai[evm]
SvmWallet(private_key=…) Solana x402-openai[svm]

All wallets implement the Wallet protocol. See wallets/_base.py to add support for a new chain.

X402Transport(x402_client, *, inner=None) / AsyncX402Transport(x402_client, *, inner=None)

Low-level httpx transports for manual wiring.

Examples

# EVM chat completion
EVM_PRIVATE_KEY="0x..." python examples/chat.py

# SVM (Solana) chat completion
SOLANA_PRIVATE_KEY="base58..." python examples/chat_svm.py

# Multi-chain (EVM + SVM)
EVM_PRIVATE_KEY="0x..." SOLANA_PRIVATE_KEY="base58..." python examples/chat_multichain.py

# Streaming
EVM_PRIVATE_KEY="0x..." python examples/streaming.py

# List models
MNEMONIC="word1 word2 ..." python examples/models.py

License

This project is licensed under either of the following licenses, at your option:

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dually licensed as above, without any additional terms or conditions.

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_openai-0.2.0.tar.gz (151.0 kB view details)

Uploaded Source

Built Distribution

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

x402_openai-0.2.0-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

Details for the file x402_openai-0.2.0.tar.gz.

File metadata

  • Download URL: x402_openai-0.2.0.tar.gz
  • Upload date:
  • Size: 151.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for x402_openai-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3b965227c43d9543fc9d632a1767d49d05dd1453b2e7ff203a14da518d9d35d6
MD5 7324b4088883e171dc8ecf6454b7f143
BLAKE2b-256 7295e923e630b02f8a48f7be124a81cb18d117ac775fcb16c4ca60b4667ec774

See more details on using hashes here.

File details

Details for the file x402_openai-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: x402_openai-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 16.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for x402_openai-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e9cc77042c258c6c72f953b1430a94aa584f2edde539d65fa577e86a11019952
MD5 e719c1535b8d6a663646365a1b81f931
BLAKE2b-256 3bda2567c03c093297d2dca80ecca7445d5e3eb140bcfb8c54a14fcdfce5180f

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