X402 Python SDK 串接教學

Python SDK 適合後端服務、資料任務、自動化 Agent 和批次處理指令碼。acedatacloud 負責 API 呼叫,acedatacloud-x402 負責簽出 PAYMENT-SIGNATURE 請求標頭。

原始碼與套件位址:

安裝相依套件

pip install acedatacloud acedatacloud-x402

如果要使用 upto,還需要一次性呼叫 Permit2 approve CLI,該 CLI 相依於 web3:

pip install 'acedatacloud-x402[cli]'

乾淨 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.

結果說明:

  • acedatacloud 和 acedatacloud-x402 均可從 PyPI 安裝並匯入。
  • pip install 'acedatacloud-x402[cli]' 會附帶 approve-permit2 CLI,用於 upto 前置授權。

Base 範例

下面範例不需要 API Token。錢包私鑰只在本機簽名,不會傳送給 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"])

Python SDK 目前回傳的是 dict,所以範例使用 res["choices"][0]["message"]["content"]。不要直接假設它一定有 .choices 屬性。

Solana 範例

Solana 使用 base58 編碼的 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,
)

Solana 路徑會建構並提交 SPL USDC TransferChecked 交易,然後把交易簽名放入 PAYMENT-SIGNATURE envelope。

Solana paid retry 在生產 API 上已回傳 HTTP 200 和 ADC_SOLANA_E2E_OK。本次公開 RPC 查詢遇到限流,未穩定確認鏈上 signature;需要對帳時請使用你自己的 Solana RPC 查詢該次交易。

Async 用戶端

同一個 payment handler 可以用於 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,
)

使用 upto 後置計量

聊天補全、模型呼叫等 API 的實際成本可能需要等回應結束後才知道。此時 API 可能同時回傳 exact 和 upto。如果要優先使用 upto:

client = AceDataCloud(
    payment_handler=create_x402_payment_handler(
        network="base",
        evm_signer=signer,
        prefer_scheme="upto",
    )
)

Base upto 的程式執行結果:

payer 0x5d4f08D5c2bb60703284bc06671Eb680fA41B105
elapsed_ms 5104
content ADC_BASE_UPTO_OK
id chatcmpl-DlcbyS4IT8kUAMo4Ri97HiIHc9T8V
settlement tx 0x4b0b836ce1cd1171cdbc37df1637150b024214ec28e7f6f2d09122f15cbfc036
settled value 3 atomic USDC

鏈上確認:

explorer https://basescan.org/tx/0x4b0b836ce1cd1171cdbc37df1637150b024214ec28e7f6f2d09122f15cbfc036
block 46726437
transfer value 3 atomic USDC

結果說明:

  • content ADC_BASE_UPTO_OK 說明請求實際進入模型 API。
  • settled value 3 atomic USDC 說明 upto 按實際用量結算,而不是扣除完整上限。
  • settlement tx 可在 BaseScan 開啟,對帳時儲存 tx hash、payer、completion id 和請求摘要。

upto 使用 Permit2 授權一個上限,實際結算金額不能超過該上限。第一次使用前,需要對目標鏈上的 USDC 做一次 approve(Permit2, amount)。

CLI 方式:

X402_PRIVATE_KEY=0x... acedatacloud-x402 approve-permit2 --network base

程式方式:

from acedatacloud_x402 import EVMAccountSigner, approve_permit2

approve_permit2(
    rpc_url="https://mainnet.base.org",
    signer=EVMAccountSigner.from_private_key("0x..."),
    token_address="0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
)

該 helper 是冪等的。如果 allowance 已經足夠,會回傳 {"skipped": true},不會重複發送鏈上交易。

低階簽名

如果你不使用 SDK,也可以直接呼叫低階簽名函式:

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

低階函式適合測試、代理層、閘道整合或非官方 SDK。一般業務程式碼優先使用 create_x402_payment_handler。