Skip to main content
@rail402.dev/sdk/seller builds the configuration the stock x402 middleware and @x402/mcp take, with Bazaar discovery metadata that is complete and valid: every parameter described, examples that satisfy their schemas, self-contained schemas without $ref, and nothing the catalog would drop. A declaration the Rail402 catalog would reject throws when the route or tool is declared, so it never reaches production. The demo seller in this repository (apps/demo-seller) is built with these helpers: three paid HTTP routes and the paid MCP tools forecast and air_quality at /mcp, plus a metered upto route (GET /history, charged per day returned) and a metered upto tool (hourly_forecast, charged per forecast hour), each capped at 0.01 USDC. The metered pair is served only while its facilitator serves upto (METERED=false leaves it out).
The examples run on Node.js 24, which runs .ts files directly (PAY_TO=G... node seller.ts).

An HTTP route

  • param.string, param.number, param.integer, param.boolean, param.enum, param.array and param.object each need a description (1-300 characters) at every depth; a missing one throws seller_param_description_required. required defaults to false, and a required HTTP parameter needs an example.
  • Query parameters are for GET, HEAD and DELETE; POST, PUT and PATCH declare body: { type: "json" | "form-data" | "text", params }. Path parameters are strings or enums and always required.
  • route.config is the stock x402 route configuration (tested with @x402/express). The resource server must register bazaarResourceServerExtension, which adds the method and route template to each 402.
  • serviceName, tags (up to 5), iconUrl and mimeType are checked the way the catalog checks them.

Prices

exact({ network, payTo, price }) returns a payment option priced in base units, converted without rounding. payTo is a G… account or C… contract; a muxed M… address throws seller_declaration_invalid, because the exact verifier cannot match a muxed transfer to it.
  • "$0.01" is Circle’s USDC on the option’s network (7 decimals);
  • { amount: "0.5", asset: "USDC" } is the same with an explicit amount;
  • { amount: "0.5", asset: "C…", decimals: 6 } is any SEP-41 token. Read its decimals from the network with await assetDecimals("C…", { network, rpcUrl }) instead of hard-coding them.
A price with more decimals than its asset, a non-positive price or a custom token without decimals throws seller_price_invalid. maxTimeoutSeconds (10-300) sets the authorization lifetime. paymentFlow: "upfront" settles before the handler runs. In either flow, wrap the facilitator client with withSettleDedupe (below).

An MCP tool

This continues the HTTP example (NETWORK, resourceServer, exact and param) and also needs npm install @x402/mcp@2.30.0 @modelcontextprotocol/sdk@1.32.1.
One declaration yields both the tool’s JSON Schema (inputSchema, identical to the listing’s) and the zod shape the MCP server validates arguments with, so the two never drift. The Rail402 catalog keys MCP listings by endpoint and tool name, scoped to the payTo that settled. After the first settled call, it lists the tool once the tool’s own answer to an unpaid tools/call confirms it, sent with the declaration’s example arguments: give every required argument an example, and keep the endpoint reachable from the public internet (see Origin verification). endpoint must be https on pubnet (seller_endpoint_invalid).

Charge what a call used (upto)

upto({ network, payTo, maxPrice, maxTimeoutSeconds? }) is a payment option whose price is a cap: the buyer authorizes at most maxPrice, and the seller settles what the call used, from zero up to the cap, after serving it. Register the upto server scheme next to (or instead of) exact; it copies the facilitator’s settlement authority into the requirements. payTo must be a G… or C… address.
setUptoCharge(res, amount) takes base units (a bigint, safe integer or integer string), a percentage of the cap ("25%") or a dollar price ("$0.002"), and throws seller_declaration_invalid for anything else. The stock middleware settles that amount after the handler; without it, it settles the full cap. The facilitator refuses a charge above the cap, and a charge of zero settles without moving funds. The authorization must outlive the handler and the settlement, so give maxTimeoutSeconds room. For MCP tools, the stock @x402/mcp wrapper passes no settlement amount, so on its own it settles the full cap of every upto tool. To charge what a tool call used, wrap the resource server with meteredToolServer and return the charge with chargeUpto; the charge is removed from the result before it reaches the buyer:
The Rail402 catalog lists an upto resource at its cap.

Validate before the first sale

validateListing runs the catalog’s own extraction, schema sandbox and soft-drop rules on a declaration or on a live PAYMENT-REQUIRED. The helper test suite checks every declaration shape against it with no drops.

Check a running seller

It makes the unpaid request (an unpaid tools/call with toolName), validates the live 402 like validateListing, and reports findings with codes: payTo cannot receive the asset (seller_asset_not_receivable, when rpcUrl is given), the facilitator does not settle this scheme and network with sponsored fees and the bazaar extension (seller_facilitator_unsupported, when facilitatorUrl is given), no x402 v2 402 (seller_origin_not_payment_required), and whether the domain’s stellar.toml claims payTo and (when rpcUrl is given) whether payTo sets the host as its home_domain (domain.homeDomainMatches); both together make the listing domain_verified. The request goes through safe-fetch. rail402 seller check <url> runs the same check from the CLI. The verdict is that of a catalog with the default setting, such as the hosted one: a resource URL on a loopback, private or otherwise non-public host is refused with validation.code bazaar_resource_unsafe and the catalog’s reason (Host "localhost" is not a public host.), and ok is false. Reaching a host and judging it are separate options: validateListing takes that catalog setting as allowLoopback, with the same pubnet rule.

Serve each payment once

An idempotent facilitator answers a repeated /settle of the same signed payment with the original success, and stock middleware would then serve the resource again. That happens in both payment flows. With paymentFlow: "upfront", every copy of a payment goes straight to /settle. With the default authorization flow, verification refuses a copy sent after the payment settled, because its nonce is used, but copies sent at the same time all pass /verify before the first one settles. Whichever flow your routes and tools use, wrap the facilitator client the resource servers are built on:
A repeat, recognised by Rail402’s extensionResponses.rail402.settlement.repeat marker or by a transaction hash the wrapper has already seen, becomes a settlement failure (seller_settlement_repeat): the HTTP middleware answers 402, and a paid MCP tool call returns the failure instead of the tool’s result. Each /settle the wrapper sends carries a new random attempt id (extensions.rail402.settlement.attempt). Rail402 records the id of the request that holds the settlement’s claim and echoes it in every repeat’s marker, as the settlement stands when the repeat is answered: the request that started the settlement, or, when that request’s claim went stale (its process died) and another request took it over and settled, the request that settled. The marker is set aside for one case only: a marked success that echoes one of the wrapper’s own attempts which may have settled the payment without a success served for it. That success is served once.
  • An attempt may have started a settlement without a success when the facilitator answered settlement_pending (the stock resource server settles it again at once), or when /settle brought no answer of the facilitator’s own. Anything but the facilitator’s x402 settle body counts as no answer: a timeout, a reset connection, an empty 502 from a proxy while the facilitator restarts, an HTML block or rate-limit page from a CDN or firewall, a reply that is not x402 JSON. A refusal in the facilitator’s own x402 body, a rate limit included, is an answer: nothing settled in that call.
  • A marked success that echoes another id is a payment someone else settled: another seller or replica that accepts the same payment, an earlier run of this process, or a request that took over this wrapper’s stale claim, also while one of this wrapper’s requests was waiting on it. It is refused, whatever the timing, and so is a second success for an attempt already served.
  • A marker without an id, from a facilitator that does not record it, is never set aside: such a seller refuses a buyer whose answer was lost or pending, as before. The recovery needs a Rail402 service that records and echoes the attempt id, so upgrade the service with or before the SDK.
The recovery helps upfront routes: with the default authorization flow /verify refuses a payment that is being settled or was, so a buyer’s second send never reaches settlement. A facilitator other than Rail402 receives the rail402 extension key too, as one it does not know. Pass attemptIds: false if it should not see it. That also turns the recovery off: with no attempt id to echo, a buyer whose settlement was pending or whose answer was lost is refused with seller_settlement_repeat when the facilitator marks the repeat, as before the attempt ids. When the facilitator sends no marker, the wrapper’s memory alone recognises repeats: give every wrapper that can accept the same payment (replicas, or other routes or apps with the same recipient, asset and amount) one shared memory, such as a SettlementMemory backed by a table with a unique key. The demo seller (apps/demo-seller) wraps its client this way: every route and tool, exact and upto, settles through one wrapper, so they share one memory. Every seller helper code is listed in the error-code reference.