X402 Python SDK Integration Guide
The Python SDK is suitable for backend services, data tasks, automation Agents, and batch processing scripts. acedatacloud is responsible for API calls, while acedatacloud-x402 is responsible for producing the PAYMENT-SIGNATURE request header.
Source code and package URLs:
- SDK repository: https://github.com/AceDataCloud/SDK
- X402 Client repository: https://github.com/AceDataCloud/X402Client
- PyPI SDK: https://pypi.org/project/acedatacloud/
- PyPI X402 Client: https://pypi.org/project/acedatacloud-x402/
¶ Install Dependencies
pip install acedatacloud acedatacloud-x402
To use upto, you also need to run the Permit2 approve CLI once. This CLI depends on web3:
pip install 'acedatacloud-x402[cli]'
Installation and import check output in a clean Python venv:
acedatacloud 2026.4.26.1
acedatacloud-x402 2026.5.31.3
imports_ok True True True True True True
usage: acedatacloud-x402 [-h] {approve-permit2} ...
approve-permit2 One-time ERC-20 approve(Permit2, amount) needed before signing upto payments.
Result description:
- Both
acedatacloudandacedatacloud-x402can be installed and imported from PyPI. pip install 'acedatacloud-x402[cli]'includes theapprove-permit2CLI, which is used for prerequisite authorization forupto.
¶ Base Example
The example below does not require an API Token. The wallet private key is only used for local signing and will not be sent to Ace Data Cloud.
import os
from acedatacloud import AceDataCloud
from acedatacloud_x402 import EVMAccountSigner, create_x402_payment_handler
signer = EVMAccountSigner.from_private_key(os.environ["EVM_PRIVATE_KEY"])
client = AceDataCloud(
payment_handler=create_x402_payment_handler(
network="base",
evm_signer=signer,
)
)
res = client.openai.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Say hi in 3 words"}],
max_tokens=10,
)
print(res["choices"][0]["message"]["content"])
The Python SDK currently returns a dict, so the example uses res["choices"][0]["message"]["content"]. Do not directly assume that it necessarily has a .choices attribute.
¶ Solana Example
Solana uses a base58-encoded secret key:
import os
from acedatacloud import AceDataCloud
from acedatacloud_x402 import SolanaKeypairSigner, create_x402_payment_handler
client = AceDataCloud(
payment_handler=create_x402_payment_handler(
network="solana",
solana_signer=SolanaKeypairSigner.from_base58(os.environ["SOLANA_SECRET_KEY"]),
)
)
res = client.openai.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Say hi in 3 words"}],
max_tokens=10,
)
The Solana path constructs and submits an SPL USDC TransferChecked transaction, then puts the transaction signature into the PAYMENT-SIGNATURE envelope.
The Solana paid retry has returned HTTP 200 and ADC_SOLANA_E2E_OK on the production API. This public RPC query encountered rate limiting and did not reliably confirm the on-chain signature; use your own Solana RPC to query the transaction when reconciliation is needed.
¶ Async Client
The same payment handler can be used with AsyncAceDataCloud:
import os
from acedatacloud import AsyncAceDataCloud
from acedatacloud_x402 import EVMAccountSigner, create_x402_payment_handler
signer = EVMAccountSigner.from_private_key(os.environ["EVM_PRIVATE_KEY"])
client = AsyncAceDataCloud(
payment_handler=create_x402_payment_handler(
network="base",
evm_signer=signer,
)
)
res = await client.openai.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Say hi in 3 words"}],
max_tokens=10,
)
¶ Using upto Post-Payment Metering
The actual cost of APIs such as chat completions and model invocations may only be known after the response has completed. In this case, the API may return both exact and upto. To prioritize using upto:
client = AceDataCloud(
payment_handler=create_x402_payment_handler(
network="base",
evm_signer=signer,
prefer_scheme="upto",
)
)
Base upto program execution result:
payer 0x5d4f08D5c2bb60703284bc06671Eb680fA41B105
elapsed_ms 5104
content ADC_BASE_UPTO_OK
id chatcmpl-DlcbyS4IT8kUAMo4Ri97HiIHc9T8V
settlement tx 0x4b0b836ce1cd1171cdbc37df1637150b024214ec28e7f6f2d09122f15cbfc036
settled value 3 atomic USDC
On-chain confirmation:
explorer https://basescan.org/tx/0x4b0b836ce1cd1171cdbc37df1637150b024214ec28e7f6f2d09122f15cbfc036
block 46726437
transfer value 3 atomic USDC
Result description:
content ADC_BASE_UPTO_OKindicates that the request actually reached the model API.settled value 3 atomic USDCindicates thatuptosettles based on actual usage rather than charging the full limit.settlement txcan be opened in BaseScan. Save the tx hash, payer, completion id, and request summary for reconciliation.
upto uses Permit2 to authorize a limit, and the actual settlement amount cannot exceed this limit. Before first use, you need to perform a one-time approve(Permit2, amount) for USDC on the target chain.
CLI method:
X402_PRIVATE_KEY=0x... acedatacloud-x402 approve-permit2 --network base
Programmatic method:
from acedatacloud_x402 import EVMAccountSigner, approve_permit2
approve_permit2(
rpc_url="https://mainnet.base.org",
signer=EVMAccountSigner.from_private_key("0x..."),
token_address="0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
)
This helper is idempotent. If the allowance is already sufficient, it returns {"skipped": true} and does not send another on-chain transaction.
¶ Low-Level Signing
If you do not use the SDK, you can also directly call the low-level signing function:
import base64
import json
from acedatacloud_x402 import EVMAccountSigner, sign_evm_payment
envelope = sign_evm_payment(requirement, EVMAccountSigner.from_private_key("0x..."))
x_payment = base64.b64encode(json.dumps(envelope, separators=(",", ":")).encode()).decode()
Low-level functions are suitable for testing, proxy layers, gateway integrations, or non-official SDKs. For regular business code, prioritize using create_x402_payment_handler.
