Skip to main content
x402 has three parties. The buyer asks for a resource, the seller (the resource server) names a price, and the facilitator checks the payment and puts it on-chain. Rail402 is the facilitator. Buyers and sellers use the stock x402 packages and never talk to Stellar directly about fees. Rail402 settles two schemes: exact, a fixed price, and upto, a cap the buyer signs of which the seller settles what the call used.

The exact payment loop

1

The seller asks for payment

The buyer requests a paid route. The seller answers 402 Payment Required with a PAYMENT-REQUIRED header that lists what it accepts: scheme exact, network stellar:testnet, the asset’s contract address, the payTo address, the amount in the asset’s base units, maxTimeoutSeconds, and extra.areFeesSponsored: true, which the seller’s scheme reads from Rail402’s /supported.
2

The buyer signs an authorization

The buyer’s x402 client builds a Soroban invocation of the asset contract’s transfer(from, to, amount) and signs its authorization entry with the buyer’s key. The signature fixes the asset, the recipient and the amount. The client sends the payment as payload: {transaction} in the PAYMENT-SIGNATURE header.
3

The seller calls /verify

Rail402 runs its own checks first, before any simulation: protocol, operator policy, transaction shape, accounts, authorization and expiry. It then runs the unmodified @x402/stellar verifier, which simulates the transaction against the current ledger. When simulation fails, Rail402 names the on-chain cause, for example a missing trustline or a spent nonce.
4

The seller calls /settle

Rail402 runs its own checks again, claims the payer’s authorization and leases one of its channel accounts. Upstream @x402/stellar then runs its verifier again and rebuilds the transaction around the buyer’s signed authorization with the channel account as the source, and Rail402’s sponsor account wraps it in a fee bump and pays the fee. Rail402 records the signed envelope before broadcasting it, then waits for the ledger.
5

The seller serves the resource

The settle response carries the transaction hash. The seller returns it to the buyer in the PAYMENT-RESPONSE header together with the resource.

The upto payment loop

upto follows the same loop with three differences; upto payments has the details.
1

The seller names a cap and a settlement authority

The upto option’s amount is the cap, and extra.facilitatorAddress names the facilitator’s settlement authority, which the seller’s scheme copies from /supported.
2

The buyer signs terms, not a transfer

The buyer signs one authorization entry for settle(terms) on the upto settlement contract: the recipient, token, cap, settlement authority and an expiration ledger, with one nested token.approve(payer, settlement contract, cap, expiration). /verify simulates the settlement for the full cap.
3

The seller settles what it used

After serving, the seller names the amount, from zero to the cap, and calls /settle. Rail402 submits settle(payer, terms, amount) with the settlement authority as the operation source; the contract checks the buyer’s terms, the authority’s authorization and 0 ≤ amount ≤ cap, and pulls exactly the amount from the buyer to payTo. The sponsor pays the fee through a fee bump, as for exact.
The contract is immutable, has no admin and stores nothing; single use comes from the payer’s authorization nonce. A TTL keeper in the service keeps the contract’s instance and code entries live on the network, and /health reports their remaining TTL (upto contract).

What the facilitator can and cannot do

Rail402 is non-custodial. It holds one secret per network, the sponsor seed, and derives two kinds of account from it: its channel accounts and, when upto is served, one settlement authority.
  • The sponsor pays every settlement fee and the base reserves of the channel accounts and the authority. It needs only XLM.
  • Channel accounts are the source accounts of settlement transactions, one settlement at a time each, so concurrent settlements never compete for a sequence number. They hold no balance.
  • The settlement authority holds no funds either. It is the operation source of upto settlements, so it chooses the settled amount between zero and the cap the buyer signed, or declines to settle; it cannot exceed the cap, change the recipient or token, reuse an authorization or move anyone’s funds.
  • Verification refuses any payment in which a facilitator account (the sponsor, a channel account or the settlement authority) is the payer or a party to one of its authorizations, and an exact payment whose transaction or operation source is one; an upto payment’s payTo may not be a facilitator account either.
  • For exact, the payer’s signature covers the asset, recipient and amount. For upto, it covers the asset, recipient, cap, settlement authority and expiration. Changing any of them after signing fails signature verification.
The facilitator can therefore only spend its own XLM on fees. See Operations for key custody and the limits that protect the sponsor.

Settlement is idempotent and crash-safe

Each settlement is keyed by the payer’s authorization (network, payer, nonce).
  • A repeated /settle for the same payment returns the recorded result, and funds move once. The ledger is consulted first, so this holds even after the authorization expired or while the sponsor guard refuses new settlements. The repeat is marked for the seller in EXTENSION-RESPONSES (rail402.settlement: repeat, settledBy, and attempt, the attempt id the request holding the settlement’s claim when the repeat is answered carried in extensions.rail402.settlement.attempt: the one that started it, or the one that took its stale claim over. A seller can tell its own attempt’s settlement from another’s).
  • A different request for an authorization already held (another envelope, recipient, asset or amount) is refused (settle_exact_stellar_idempotency_conflict, or settle_upto_stellar_idempotency_conflict for an upto settlement asked for another amount).
  • If the transaction is not final within the confirmation window, /settle answers settlement_pending with the transaction hash, and a background reconciler finishes it from the recorded bytes.
  • After a crash or restart, only the recorded bytes are ever resubmitted, so there is never a second transaction for the same authorization.
The full list of checks, codes and tests is in Errors and verification rules.

The Bazaar

A payment can carry the x402 bazaar extension, which describes the paid resource: its input and output schema, method or tool name, and service metadata. When a payment with that extension settles, Rail402 adds the resource to its catalog, provided the metadata is valid and, for an http or https resource URL, the host is public. A payment without the extension is settled normally and not cataloged.
  • Settlement-gated. Nothing is listed without a settled payment to it. /verify only previews the outcome.
  • Bound to the payee. The listing belongs to the payTo the settlement paid. Another seller cannot change it.
  • Confirmed at the origin. An HTTP resource is listed, and any change to it published, only after the resource’s own 402 response confirms it.
  • Off the payment path. Cataloging never changes the payment result and adds no transaction. The seller learns the outcome from the EXTENSION-RESPONSES header of the facilitator’s response.
  • upto listings. An upto resource is listed at its cap, the most a call can cost. A settlement of zero, on chain or off, never creates or changes a listing: a listing needs a settlement that moved value to its payTo (cataloging rules).
Buyers and agents read the catalog through GET /discovery/resources and GET /discovery/search. The catalog lives in Postgres, not on-chain.

Networks

Rail402 knows the CAIP-2 networks stellar:testnet and stellar:pubnet. Which ones an instance serves is configuration (NETWORKS). The hosted instance serves stellar:testnet only. The tests and conformance runs exercise testnet. Pubnet can be configured, with stricter rules that the service checks at startup (see Configuration), but it has not been exercised end to end.

Components

Rail402 is one service backed by Postgres, built from workspace packages that can also be used on their own. Without Postgres (STORE=memory) the same service runs in one process with no durable state, which suits local development and tests. See Configuration. The service also runs background tasks: the reconciler that finishes unconfirmed settlements, the network poll behind /health and /ready, the upto settlement authority’s provisioning, the contract TTL monitor and keeper, and the Bazaar’s origin checks and search indexing. Buyers and agents can use the stock x402 clients, or the agent tools built on them: the buyer SDK (packages/sdk), the MCP server (packages/mcp) and the CLI (packages/cli). They discover resources through the Bazaar, pay under a spending policy, and reach sellers through packages/safe-fetch, which refuses private addresses and pins DNS (Agents).