Before you start
The party you are paid to needs a liveTransferPreapproval. Create it with the Node wallet, as on the merchant page:
preapproval --admin <admin> --id <id> once per token you price.
Go
github.com/x402-foundation/x402/go/v2 v2.31.0 or newer. Works with net/http, gin and echo v4.
Python
x402 2.28. Works with FastAPI and Starlette (ASGI) and Flask (WSGI).
Money safety
- A 4xx or 5xx from your handler is not charged. A 3xx becomes 502 and is not charged.
- A replay of a served payment gets 402
payment_already_redeemed. - 503
do NOT pay again; retry the same paymentmeans the payment may have moved. The client resends the same signature and is served once. The retry runs your handler again, so keep paid side effects idempotent. - Use Redis in production: Go
goredis.NewStore, PythonRedisStore.from_url. The default memory store forgets payments on restart. - Limit per payer (
canton.PayerFrom,payer_from), not only per IP, and let retries through (canton.IsRetry,is_retry).
Deployment
- Proxies must accept a 32 KiB
PAYMENT-SIGNATUREheader. nginx:large_client_header_buffers 4 32k;. - Set the proxy read timeout and the shutdown grace to at least the plugin’s request budget: Go
canton.RequestBudget(cfg), 505 s by default; Pythonrequest_budget(config), 465 s. - Python: gunicorn
--limit-request-field_size 32768 --timeout 470 --graceful-timeout 470, waitresschannel_request_lookahead=1, and TLS ended at a proxy. - Your handler must finish within the payment’s lifetime, 120 s by default. A slower one lets the payment expire, and nothing is charged.