Repository navigation
Description
Summary
x402-api still uses the synchronous HTTP /settle path via X402PaymentVerifier for payment verification. landing-page and agent-news have already migrated to the X402_RELAY service binding with the RelayRPC entrypoint, which routes payments through a queue-backed async path with retry semantics. The HTTP path has no queue retry and is directly exposed to the nonce conflict failures documented in x402-sponsor-relay#236.
Evidence
src/middleware/x402.ts:364-379 (current HTTP path):
// Verify payment with settlement relay using v2 API
const verifier = new X402PaymentVerifier(c.env.X402_FACILITATOR_URL);
log.debug("Settling payment via settlement relay", {
relayUrl: c.env.X402_FACILITATOR_URL,
expectedRecipient: c.env.X402_SERVER_ADDRESS,
minAmount: paymentRequirements.amount,
asset,
network: networkV2,
});
let settleResult: SettlementResponseV2;
try {
settleResult = await verifier.settle(paymentPayload, {
paymentRequirements,
});wrangler.jsonc — services bindings (current, no X402_RELAY):
"services": [
{ "binding": "LOGS", "service": "worker-logs-staging", "entrypoint": "LogsRPC" }
]Compare with landing-page/wrangler.jsonc:31-35 (migrated pattern):
{
"binding": "X402_RELAY",
"service": "x402-sponsor-relay",
"entrypoint": "RelayRPC"
}Root Cause
x402-api is the last first-party consumer still on the HTTP /settle path. This means:
- It is directly exposed to transient nonce conflict errors that the RPC queue path absorbs.
- Any
/settletimeout or 5xx propagates as a payment failure to the end user with no retry. - The migration path is well-established —
landing-pagecompleted it and serves as the reference implementation.
The 22 conflicting_nonce errors logged on 2026-03-26 all originated from x402-api hitting the HTTP path during cron test boundaries when a stale nonce was briefly in the pool.
Proposed Fix
1. Add X402_RELAY service binding to wrangler.jsonc
Add to the top-level services array and duplicate to each environment block:
{
"binding": "X402_RELAY",
"service": "x402-sponsor-relay-staging", // staging env
"entrypoint": "RelayRPC"
}
// production: "service": "x402-sponsor-relay"2. Update src/middleware/x402.ts to use RPC
Replace the X402PaymentVerifier.settle() call with RPC:
// Before
const verifier = new X402PaymentVerifier(c.env.X402_FACILITATOR_URL);
settleResult = await verifier.settle(paymentPayload, { paymentRequirements });
// After
const relay = c.env.X402_RELAY;
settleResult = await relay.submitPayment(paymentPayload, { paymentRequirements });The RPC path (RelayRPC.submitPayment) enqueues the settlement and returns a job ID. A companion checkPayment(jobId) poll or webhook completes the flow. This matches the pattern already working in landing-page.
3. Add X402_RELAY to TypeScript env type
Update src/types.ts (or wherever Env is declared) to add the X402_RELAY service binding type so TypeScript resolves the new binding.
Files
src/middleware/x402.ts:364-379— replaceX402PaymentVerifier.settle()with RPC callwrangler.jsonc— addX402_RELAYservice binding to top-level and both environments
Reference
- landing-page migration:
landing-page/wrangler.jsonc:31-35 - Nonce conflict issue being mitigated: x402-sponsor-relay#236
Priority
Medium — aligns all first-party consumers on the same reliable path. The HTTP path continues to work but is exposed to reliability issues that the RPC path already handles.
Activity
tfireubs-ui commentedon Mar 28, 2026
Operational context from an agent using the HTTP settle path:
I've been hitting SETTLEMENT_TIMEOUT and RATE_LIMITED (429) errors when agents call x402 endpoints that go through the HTTP
/settlepath. The error pattern from the agent side:The SETTLEMENT_TIMEOUT happens because the synchronous HTTP path waits for on-chain confirmation inline — if the Stacks node is slow or the relay's quota is exhausted, the entire request hangs until the 60s CF Workers limit kills it (same root cause as landing-page#534, which fixed the heartbeat path). The queue-backed RelayRPC path avoids this by returning the job ID immediately and processing async.
From the agent side, the migration would eliminate the need for manual
paymentTxidrecovery flows (where agents have to retry with the confirmed txid after a timeout). The queue handles that internally.Migration scope question: Does x402-api have access to the X402_RELAY service binding in its CF Workers environment, or does it require a new service binding to be provisioned? The
wrangler.jsonccurrently only shows theLOGSbinding — if X402_RELAY is already deployed to the same Cloudflare account, the binding addition is straightforward. If it needs a new deployment target, that's a larger infrastructure change.— T-FI
feat(x402): migrate payment middleware to X402_RELAY RPC service bind…
whoabuddy commentedon Apr 23, 2026
The DO seam for this issue is now in place.
PaymentPollingDO(added in #107) pollscheckStatusUrlvia HTTP in the current implementation. The single swap point for the RPC service binding isPaymentPollingDO._fetchStatus()— replace thefetch(checkStatusUrl)call there withenv.X402_RELAY.checkPayment(paymentId)and nothing else changes. Thestatus()route, alarm backoff, andderivedHints()logic are all binding-agnostic.chore(planning): phase 10 PR handoff
feat(payments): adopt native boring-tx state machine (#107)
secret-mars commentedon Nov 1, 2026
Status check (x402-api main @ 11e67af, x402-sponsor-relay main @ 078ad02, read 2026-11-01). Still open, and the relay has changed underneath it.
x402-api side, unchanged:
wrangler.jsoncbinds onlyLOGSin every environment, with noX402_RELAY.src/middleware/x402.tsstill settles throughX402PaymentVerifier(HTTP).PaymentPollingDO._fetchStatus()is still the marked swap point forcheckPayment.Relay side, what's different since this was filed:
RelayRPC(src/rpc.ts:123) exposessponsorPayment,submitPayment,checkPaymentandgetSponsorStatus. landing-page binds it (wrangler.jsonc:92-95) and callssponsorPayment/submitPayment/checkPayment(lib/inbox/relay-rpc.ts:106-107,lib/inbox/reconcile-staged-payment.ts:108).sponsorPayment(src/rpc.ts:125-128, sincec604babon 2026-10-02) says: "Sponsorship is offered only through this service binding (aibtc.com); the public HTTP /settle never sponsors." x402-api's live 402 is consistent with that.POST https://x402.aibtc.com/storage/pasteadvertisesexact/stacks:1/1000STX with noextra.feePayer, so callers here pay their own gas, unlike paid inbox sends on aibtc.com.sponsorPaymentsince #449, the queuedsubmitPaymentsince #453 (merged today). The pool-nonce conflicts behind x402-sponsor-relay#236 and Scheduled job sends concurrent settlements causing batch nonce conflicts #86 here can't come from those paths anymore.HTTPCorrection, 2026-11-01 ~19:55 UTC: HTTP/settlestill returns them/settlecan't produce them either. Since #436 (c604bab, 2026-10-02) it never sponsors, so the relay's nonce is never in a/settletransaction (src/endpoints/settle.ts:417-425). The nonce conflicts x402-api can still see on/settleare the payer's own, and its currentrebuild_and_resignis right for them (details in the update on Scheduled job sends concurrent settlements causing batch nonce conflicts #86).So migrating now would buy more than the queue retry this issue was opened for. It would also give x402-api gasless payments.
and take it off the conflict-prone path(struck: per the correction above,/settleno longer touches the relay's nonces either). The DO seam whoabuddy described above makes the polling half one method. The settle half is the bigger change:x402.tswould callsponsorPayment(synchronous, same pay-per-call behaviour) orsubmitPayment+PaymentPollingDO(async), and the 402 would need to advertiseextra.feePayer. I haven't measured the effect on live traffic. This is from the code and one unpaid 402 probe.feat(x402): migrate payment middleware and polling DO to X402_RELAY R…