> ## Documentation Index
> Fetch the complete documentation index at: https://docs.rail402.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Seller helpers

> Declare paid HTTP routes and MCP tools with discovery metadata the Rail402 catalog accepts as is, validate it before the first sale, and check a live seller the way the catalog will.

`@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).

```sh theme={"theme":{"light":"github-light","dark":"github-dark"}}
npm init -y && npm pkg set type=module
npm install express@5 @rail402.dev/sdk@0.5.7 @x402/core@2.30.0 @x402/express@2.30.0 @x402/extensions@2.30.0 @x402/stellar@2.30.0
```

The examples run on Node.js 24, which runs `.ts` files directly (`PAY_TO=G... node seller.ts`).

## An HTTP route

```ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
import express from "express";
import { HTTPFacilitatorClient, x402ResourceServer } from "@x402/core/server";
import { paymentMiddleware } from "@x402/express";
import { bazaarResourceServerExtension } from "@x402/extensions/bazaar";
import { ExactStellarScheme } from "@x402/stellar/exact/server";
import { exact, httpRoute, param, withSettleDedupe } from "@rail402.dev/sdk/seller";

const NETWORK = "stellar:testnet";
const forecast = httpRoute({
  method: "GET",
  path: "/forecast/:city",
  description: "Seven-day weather forecast for a city",
  payment: exact({ network: NETWORK, payTo: process.env.PAY_TO!, price: "$0.01" }),
  pathParams: { city: param.string({ description: "City name", example: "Ankara", maxLength: 64 }) },
  query: { days: param.integer({ description: "Number of days", minimum: 1, maximum: 7, example: 3 }) },
  output: { example: { city: "Ankara", days: [{ high: 21, low: 9 }] } },
});

// Serves each settled payment once: see "Serve each payment once" below.
const facilitator = withSettleDedupe(new HTTPFacilitatorClient({ url: "https://testnet.rail402.dev" }));
const resourceServer = new x402ResourceServer(facilitator)
  .register(NETWORK, new ExactStellarScheme())
  .registerExtension(bazaarResourceServerExtension);

const app = express();
app.use(paymentMiddleware({ [forecast.key]: forecast.config }, resourceServer));
app.get("/forecast/:city", (req, res) => res.json({ city: req.params.city, days: [{ high: 21, low: 9 }] }));
app.listen(4021);
```

* `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](#serve-each-payment-once)).

## 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`.

```ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { createPaymentWrapper } from "@x402/mcp";
import { mcpPaymentConfig, mcpTool } from "@rail402.dev/sdk/seller";

const airQuality = mcpTool({
  endpoint: "https://api.example.com/mcp",
  toolName: "air_quality",
  description: "Current air quality index (AQI) for a city",
  payment: exact({ network: NETWORK, payTo: process.env.PAY_TO!, price: "$0.001" }),
  arguments: {
    city: param.string({ description: "City name", required: true, example: "Izmir" }),
    pollutant: param.enum(["pm25", "pm10", "o3"], { description: "Pollutant to report", example: "pm25" }),
  },
  output: { example: { city: "Izmir", aqi: 42 } },
  transport: "streamable-http",
});

await resourceServer.initialize();
const paid = createPaymentWrapper(resourceServer, await mcpPaymentConfig(resourceServer, airQuality));

const server = new McpServer({ name: "air", version: "1.0.0" });
server.registerTool(
  airQuality.toolName,
  { description: airQuality.description, inputSchema: airQuality.zodShape },
  paid((args) => ({ content: [{ type: "text", text: JSON.stringify({ city: args.city, aqi: 42 }) }] })),
);
```

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](https://docs.rail402.dev/bazaar/cataloging.md#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.

```ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
import { UptoStellarServerScheme, httpRoute, setUptoCharge, upto } from "@rail402.dev/sdk/seller";

const report = httpRoute({
  method: "GET",
  path: "/report",
  description: "Report rows, billed per row up to 0.05 USDC",
  payment: upto({ network: NETWORK, payTo: process.env.PAY_TO!, maxPrice: "$0.05", maxTimeoutSeconds: 120 }),
  query: { rows: param.integer({ description: "Rows to return", minimum: 1, maximum: 500, example: 20 }) },
  output: { example: { rows: [] } },
});

resourceServer.register(NETWORK, new UptoStellarServerScheme());
app.use(paymentMiddleware({ [report.key]: report.config }, resourceServer));
app.get("/report", (req, res) => {
  const rows = Number(req.query.rows ?? 20);
  setUptoCharge(res, rows * 1_000); // base units: 0.0001 USDC per row
  res.json({ rows: [] });
});
```

`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:

```ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
import { chargeUpto, meteredToolServer } from "@rail402.dev/sdk/seller";

const metered = createPaymentWrapper(
  meteredToolServer(resourceServer),
  await mcpPaymentConfig(resourceServer, summarize), // declared with mcpTool({ payment: upto(…), … })
);
server.registerTool(
  summarize.toolName,
  { description: summarize.description, inputSchema: summarize.zodShape },
  metered((args) => {
    const text = summarizeText(args.text);
    return chargeUpto({ content: [{ type: "text", text }] }, text.length * 10);
  }),
);
```

The Rail402 catalog lists an upto resource at its cap.

## Validate before the first sale

```ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
import { validateListing } from "@rail402.dev/sdk/seller";

const report = await validateListing(forecast, { network: "stellar:testnet" });
report.catalogable; // the catalog would list it after a settlement
report.dropped; // metadata the catalog would leave out
report.lint; // advice: missing descriptions or output example
report.preview; // resource, type, method or toolName, payTo, asset, amount
```

`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

```ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
import { checkSellerOrigin } from "@rail402.dev/sdk/seller";

const report = await checkSellerOrigin("https://api.example.com/forecast/Ankara", {
  network: "stellar:testnet",
  rpcUrl: "https://soroban-testnet.stellar.org",
  facilitatorUrl: "https://testnet.rail402.dev",
});
```

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](https://docs.rail402.dev/reference/egress.md).
`rail402 seller check <url>` runs the same check from the [CLI](https://docs.rail402.dev/agents/cli.md).

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:

| Option | Effect |
| - | - |
| `allowLoopback` | The check's own requests may reach loopback URLs and a plain-http RPC, for a seller on your machine. It does not change the verdict. |
| `discoveryAllowLoopback` | Judge as a testnet catalog run with `DISCOVERY_ALLOW_LOOPBACK=true` ([Try the Bazaar locally](https://docs.rail402.dev/facilitator/docker.md#try-the-bazaar-locally)), which also lists loopback hosts. Private addresses stay refused. On pubnet it throws `seller_network_unsupported`. |

`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:

```ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
import { withSettleDedupe } from "@rail402.dev/sdk/seller";

const facilitator = withSettleDedupe(new HTTPFacilitatorClient({ url: "https://testnet.rail402.dev" }));
```

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](https://docs.rail402.dev/reference/error-codes.md#seller-helpers).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.