Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(x402-server): fall back to X402_FEE_PAYER_SOLANA when a route omits feePayer

nirholas27145205d106
diff --git a/packages/x402-server/README.md b/packages/x402-server/README.md
index 0c749302d..81410cca1 100644
--- a/packages/x402-server/README.md
+++ b/packages/x402-server/README.md
@@ -101,6 +101,10 @@ USDC. The first unpaid `GET`/`POST` returns the challenge; the buyer pays and
re-sends with `X-PAYMENT`; your handler runs once, settlement lands, and the
response carries the on-chain receipt.
+The Solana lane also needs the facilitator's sponsor account: set
+`X402_FEE_PAYER_SOLANA` in the environment (or pass `feePayer`), or the
+challenge throws `missing_fee_payer`. The EVM lane needs neither.
+
### A fuller route — both lanes, a fee, a receipt
```js
@@ -189,6 +193,7 @@ function — mount it as Express/Connect middleware, a Vercel function, or a Nod
| `asset` | `'usdc' \| { solana?, base? }` | `'usdc'` | Settlement asset. A string resolves the canonical USDC mint/contract per chain; an object pins explicit addresses. |
| `payTo` | `{ solana?, base? }` | — (**required**) | Pay-to address per lane. At least one chain is required. |
| `network` | `('solana' \| 'base')[]` | from `payTo` | Which accepts to advertise. Solana leads when both are present. |
+| `feePayer` | `string` | `X402_FEE_PAYER_SOLANA` | The facilitator's Solana sponsor account, advertised as `extra.feePayer`. **Required for the Solana lane** (option or env), otherwise building the challenge throws `missing_fee_payer`. Ignored on EVM. |
| `feeBps` | `number` | `0` | Platform fee in basis points, **split out of `price`** (≤ `1000` / 10%). `0` = no fee. |
| `feeTo` | `string` | — | Fee recipient. Required when `feeBps > 0` — no recipient, no fee. |
| `facilitator` | `string` | platform default | Override the x402 facilitator base URL used for `/verify` + `/settle`. |
@@ -231,6 +236,10 @@ header. Accepts the canonical accept shape:
extra: { name, decimals, feePayer? } } // feePayer required on Solana
```
+The Solana `feePayer` comes from the `feePayer` option, or from
+`X402_FEE_PAYER_SOLANA` in the environment when the option is omitted, so a
+deployment can keep the sponsor account out of its route code.
+
### `verifyPayment({ paymentHeader, requirements }) → Promise<Verified>`
Decode the base64 `X-PAYMENT` header and verify it against `requirements` (your
diff --git a/packages/x402-server/examples/README.md b/packages/x402-server/examples/README.md
index e2c7d0096..c924828b6 100644
--- a/packages/x402-server/examples/README.md
+++ b/packages/x402-server/examples/README.md
@@ -9,6 +9,14 @@ Runnable examples for the seller half of x402. Each is a standalone `.mjs` file.
| [`raw-primitives.mjs`](raw-primitives.mjs) | Drive `buildChallenge` / `verifyPayment` / `settlePayment` / `feeSplit` directly, no middleware. | `node examples/raw-primitives.mjs` |
| [`streaming-download.mjs`](streaming-download.mjs) | A paid binary/stream download with `streaming: true` (settle-then-stream). | `node examples/streaming-download.mjs` |
+The two server examples listen on a fixed port (3000 and 3001). Set `PORT` to
+move either one when that port is taken:
+
+```bash
+PORT=3100 node examples/express-metered-api.mjs
+PORT=3111 node examples/streaming-download.mjs
+```
+
## The two response modes
`paid()` guarantees the buyer never receives the good before settlement. It does
diff --git a/packages/x402-server/src/index.d.ts b/packages/x402-server/src/index.d.ts
index 073517dd3..f38cca68e 100644
--- a/packages/x402-server/src/index.d.ts
+++ b/packages/x402-server/src/index.d.ts
@@ -71,7 +71,10 @@ export interface BuildChallengeOptions {
payTo?: PayTo;
/** Which lanes to advertise. Defaults to every lane present in `payTo`. */
network?: Lane | Lane[];
- /** The Solana facilitator sponsor account (required for a Solana accept). */
+ /**
+ * The Solana facilitator sponsor account (required for a Solana accept).
+ * Falls back to the X402_FEE_PAYER_SOLANA env var when omitted.
+ */
feePayer?: string;
/**
* Advertise $THREE alongside USDC on the Solana lane (a second accept, pushed
@@ -215,3 +218,9 @@ export declare function paid(opts: PaidOptions, handler: Function): (...args: an
* (rate 0, no recipient, or a sub-atomic fee). `bps` is clamped to [0, 1000].
*/
export declare function feeSplit(priceAtomics: bigint | number | string, bps: number, recipient: string): FeeSplit | null;
+
+/**
+ * Resolve the Solana sponsor account: the explicit value, else the
+ * X402_FEE_PAYER_SOLANA env var, else null.
+ */
+export declare function resolveFeePayer(feePayer?: string | null): string | null;
diff --git a/packages/x402-server/src/index.js b/packages/x402-server/src/index.js
index cd517d1d6..cddccdd3f 100644
--- a/packages/x402-server/src/index.js
+++ b/packages/x402-server/src/index.js
@@ -225,7 +225,20 @@ function buildAccepts({ price, asset, payTo, network, feePayer, maxTimeoutSecond
return out;
}
-function buildAccept({ lane, amount, asset, payTo, feePayer, maxTimeoutSeconds, resourceUrl }) {
+/**
+ * Resolve the Solana sponsor account: explicit `feePayer` option, else the
+ * X402_FEE_PAYER_SOLANA env var. Deployments keep the sponsor in the
+ * environment (the platform rails do), so a route that omits the option still
+ * advertises a payable Solana accept instead of throwing missing_fee_payer.
+ */
+export function resolveFeePayer(feePayer) {
+ if (typeof feePayer === 'string' && feePayer.trim()) return feePayer.trim();
+ const env = typeof process !== 'undefined' && process.env ? process.env.X402_FEE_PAYER_SOLANA : null;
+ return typeof env === 'string' && env.trim() ? env.trim() : null;
+}
+
+function buildAccept({ lane, amount, asset, payTo, feePayer: explicitFeePayer, maxTimeoutSeconds, resourceUrl }) {
+ const feePayer = resolveFeePayer(explicitFeePayer);
const network = LANE_NETWORK[lane];
const assetAddress = resolveAsset(asset, lane);
const accept = {
diff --git a/packages/x402-server/test/x402-server.test.js b/packages/x402-server/test/x402-server.test.js
index 1d61801a8..05317c6e5 100644
--- a/packages/x402-server/test/x402-server.test.js
+++ b/packages/x402-server/test/x402-server.test.js
@@ -187,6 +187,34 @@ test('a Solana accept without a feePayer is rejected with missing_fee_payer', ()
);
});
+test('the Solana sponsor falls back to X402_FEE_PAYER_SOLANA, and the option wins', () => {
+ const previous = process.env.X402_FEE_PAYER_SOLANA;
+ try {
+ process.env.X402_FEE_PAYER_SOLANA = SYNTH_SOLANA_FEEPAYER;
+ // Same call that throws with no env set: the sponsor now comes from the
+ // environment, which is where a deployment keeps it.
+ const fromEnv = buildChallenge({ price: '1000', payTo: { solana: SYNTH_SOLANA_PAYTO } });
+ assert.equal(fromEnv.accepts[0].extra.feePayer, SYNTH_SOLANA_FEEPAYER);
+
+ const explicit = buildChallenge({
+ price: '1000',
+ payTo: { solana: SYNTH_SOLANA_PAYTO },
+ feePayer: 'THREEsynthetic11111111111111111111Explicit',
+ });
+ assert.equal(explicit.accepts[0].extra.feePayer, 'THREEsynthetic11111111111111111111Explicit');
+
+ // A blank env is not a sponsor.
+ process.env.X402_FEE_PAYER_SOLANA = ' ';
+ assert.throws(
+ () => buildChallenge({ price: '1000', payTo: { solana: SYNTH_SOLANA_PAYTO } }),
+ (e) => e instanceof ThreeWsError && e.code === 'missing_fee_payer',
+ );
+ } finally {
+ if (previous === undefined) delete process.env.X402_FEE_PAYER_SOLANA;
+ else process.env.X402_FEE_PAYER_SOLANA = previous;
+ }
+});
+
test('buildChallenge() validates price + payTo before building', () => {
assert.throws(() => buildChallenge({ payTo: { base: SYNTH_BASE_PAYTO } }), /needs a `price`/);
assert.throws(() => buildChallenge({ price: '1.5', payTo: { base: SYNTH_BASE_PAYTO } }), /whole atomic amount/);