ClawPay × x402 client + server SDK — China-localized payment schemes (clawpay-cny, clawpay-dcep, clawpay-cross) on top of the x402 HTTP 402 protocol.
Project description
clawpay-x402 — Python SDK
ClawPay × x402 client + server SDK for the
clawpay-cny/clawpay-dcep/clawpay-crossschemes. Implements the x402 HTTP 402 protocol with China-localized settlement.Spec: https://erc8004.cn/specs/clawpay-x402/ · License: CC0 · Python ≥ 3.10 · async-first
Install
uv add clawpay-x402 # client only
uv add 'clawpay-x402[fastapi]' # + FastAPI server dependency
# or: pip install clawpay-x402
Client — pay an x402 endpoint in 3 lines
import asyncio
from clawpay_x402 import x402_pay
async def main():
r = await x402_pay("https://erc8004.cn/api/x402/demo", from_="clawpay:my-agent")
print(r.body, r.receipt)
asyncio.run(main())
x402_pay returns an X402Result with: status, body, paid, payment_requirements, receipt, response. It:
GETs the URL.- If
200, returns the body (no payment needed). - If
402, parsesaccepts[], picks the first scheme matchingprefer(default order:clawpay-cny→clawpay-dcep→clawpay-cross→exact), builds aPaymentPayload, base64-encodes it asX-PAYMENT, retries.
Real settlement — pass on_build_payload to inject a real transactionId from your WeChat/Alipay/DCEP processor:
async def hook(req):
txn = await wechat_pay.charge(amount=req["maxAmountRequired"], payee=req["payTo"])
return {"transactionId": txn["id"], "rail": "wechat"}
r = await x402_pay(url, from_="clawpay:user_42", on_build_payload=hook)
For the exact (USDC on Base) scheme, this SDK raises — use Coinbase's official x402 SDK or filter exact out via prefer.
Server — protect a FastAPI endpoint in 1 line
from fastapi import FastAPI, Depends, Response
from clawpay_x402.server import x402_dependency
app = FastAPI()
pay = x402_dependency(
accepts=[{
"scheme": "clawpay-cny", "network": "clawpay-mainnet",
"maxAmountRequired": "150", "asset": "CNY", "payTo": "agent:31981",
"extra": {"facilitator": "https://erc8004.cn/api/x402/facilitator", "humanPrice": "¥1.50"},
}],
verify=lambda payload: True, # replace with real check (sync or async)
)
@app.get("/api/translate")
async def translate(ctx: dict = Depends(pay)) -> Response:
import json
return Response(
content=json.dumps({"output": "Hello, world"}),
media_type="application/json",
headers={"X-PAYMENT-RESPONSE": ctx["receipt_header"]},
)
The dependency:
- emits
402+accepts[]when noX-PAYMENTheader - decodes & shape-validates the header (rejects malformed / wrong scheme / under-amount / stale
signedAt) - calls your
verifyhook (sync or async) - on success, returns
{"payload", "paymentRequirements", "receipt", "receipt_header"}to your route
For framework-agnostic use, the lower-level building blocks are exported too:
from clawpay_x402 import build_402_body, decode_payment, verify_shape, encode_receipt
Test
uv pip install '.[test]'
uv run pytest tests/ -v # unit tests (no network)
LIVE=1 uv run pytest tests/test_live.py -v # hits the real erc8004.cn demo
What this SDK is and isn't
| ✅ | ❌ |
|---|---|
| HTTP layer: 402 ↔ X-PAYMENT ↔ X-PAYMENT-RESPONSE | Real WeChat/Alipay/DCEP settlement |
| Shape & freshness validation | Real fraud check (delegate to facilitator) |
| Spec-compliant PaymentPayload encoding | On-chain USDC signing |
| FastAPI dependency + framework-free building blocks | Crypto custody (illegal for CN domestic entities anyway) |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file clawpay_x402-0.1.0.tar.gz.
File metadata
- Download URL: clawpay_x402-0.1.0.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b860ee776d1dcf8e374ab33c5d4de6f0530756cc3f96441597281f39f04f0c3
|
|
| MD5 |
3f4ca9370015b323ae0a69f1718c0c97
|
|
| BLAKE2b-256 |
95e46f1229de2503308b47ff40ce0c54f1550ec671e7accede43a665bd47b4c9
|
File details
Details for the file clawpay_x402-0.1.0-py3-none-any.whl.
File metadata
- Download URL: clawpay_x402-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9d53741df4ef30c4f4b1675733b1bd6c921b6f6d3b6bc8af5cf833d7db67266a
|
|
| MD5 |
c42d99ead1100d0dc3791eb3dc9511a8
|
|
| BLAKE2b-256 |
d538214863753ac60de4a91fa92206a5f68e2645182bce2cff884ae05c02f94e
|