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")

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
x402_client Pre-configured x402HTTPClientSync

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 — private key
EVM_PRIVATE_KEY="0x..." python examples/chat_evm.py

# EVM — mnemonic phrase
MNEMONIC="word1 word2 ..." python examples/chat_evm_mnemonic.py

# SVM (Solana) — private key
SOLANA_PRIVATE_KEY="base58..." python examples/chat_svm.py

# Async streaming — EVM
EVM_PRIVATE_KEY="0x..." python examples/streaming_evm.py

# Async streaming — EVM mnemonic
MNEMONIC="word1 word2 ..." python examples/streaming_evm_mnemonic.py

# Async streaming — SVM
SOLANA_PRIVATE_KEY="base58..." python examples/streaming_svm.py

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

# List models
EVM_PRIVATE_KEY="0x..." python examples/models.py

# List models — mnemonic
MNEMONIC="word1 word2 ..." python examples/models_mnemonic.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.1.tar.gz (150.6 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.1-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: x402_openai-0.2.1.tar.gz
  • Upload date:
  • Size: 150.6 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.1.tar.gz
Algorithm Hash digest
SHA256 035d7a5a11d72fa8e4b9ef5c963ea6ee33543cae7091d433c0107b5ba461ac1b
MD5 611ebda865915da1590aa7cd2cb83366
BLAKE2b-256 81cb50ab98cbe889824d3d1a7fbc35e8ff9a4e65072170c50de51c213f1016fb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: x402_openai-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 16.2 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 83132474d5ba5998517f8ae10487cf8e8603e60153ea888817af6535cc3cdc7b
MD5 738440845754b2e4b2133aa201a071c1
BLAKE2b-256 9d8b748810d32ff49a0f563b33a6f89f1412e072bdb7381a72efe5692837c216

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