Skip to content

Lightning (sats)

Payment wires: every paid endpoint accepts x402 and MPP (Machine Payments Protocol) on the same 402 - see Paying with x402 and Paying with MPP. Lightning rides on both, and on L402.

On a server that turns them on, Agent402 takes bitcoin over Lightning on three wires. Each one has its own switch and is off by default; a server offers a wire only while its switch is on, so a 402 with no Lightning option means that server does not take it for that call.

Wire Switch Spec
MPP lightning charge MPP_LIGHTNING=on draft-lightning-charge-00
x402 exact on lnbtc X402_LNBTC=on (Lightning mainnet only) the x402 exact scheme for lnbtc
L402 L402=on L402 (the earlier LSAT name is read the same way)

All three need the same wallet service, network, challenge secret and state database; the settings are under LIGHTNING in Configuration.

Whether a server offers the MPP method is in /.well-known/x402 under mpp.lightning (with its price floor) and per route in /openapi.json under x-payment-info.offers, and /.well-known/agent402-conformance.json has a row for each wire that is on.

Which routes take it

  • This server's own tools priced at or above the Lightning floor (LIGHTNING_MIN_PRICE_USD, default $0.005: a whole-sat amount would round a cheaper call up by more than it costs).
  • Never an identity-bound route (Lightning names no payer, and those routes are keyed on one), a route that buys from an outside seller, a route whose effect outlives the answer, a long-running route or a metered route.
  • x402 lnbtc only on a GET with no body: the scheme's http:1 profile binds the method, the URL (query string included) and the body hash into the invoice, and the 402's resource.url is that exact URL. A POST's 402 offers the MPP method and L402.
  • When the server cannot make an invoice in time (its wallet or its BTC/USD rate is unavailable, or the per-minute invoice budget is spent), the 402 goes out without the Lightning option. Every other way to pay on that 402 is unchanged.

One invoice, paid before the run

The 402 carries one BOLT11 invoice for the route's price, in whole sats at the current BTC/USD rate rounded up, payable for LIGHTNING_INVOICE_EXPIRY_SECONDS (default 600). Every Lightning option on that 402 offers the same invoice: pay it once, then present the preimage on one wire.

  • Final before the run. Your wallet's payment is complete before the request is sent again, so Lightning is charged before the tool runs (unlike x402, which settles after a final 200). Each invoice pays for one run; the same proof sent again is refused (on x402, duplicate_settlement).
  • Only invoices this server issued pay. An invoice signed by the same key but not issued here for that offer pays nothing.
  • A proof refused before its invoice is spent (on a route Lightning does not pay, or one priced above the invoice's quote) leaves the invoice unspent: present it on a route it pays before it expires. An lnbtc proof pays only the request its invoice is bound to.
  • No payer. The MPP lightning and x402 lnbtc receipts name the payment hash and no payer (L402 sends no receipt). This server keeps the preimage out of its logs and its stores, and no answer echoes it.

What the 402 carries, and what you send

Wire The 402 carries You send A 200 carries
MPP lightning WWW-Authenticate: Payment with method="lightning", intent="charge"; the request holds amount (sats), currency: "sat" and methodDetails { invoice, paymentHash, network }, and the challenge expires with the invoice Authorization: Payment, a credential for that challenge with payload: { preimage } Payment-Receipt with method: "lightning" and reference, the payment hash
x402 lnbtc an accepts entry after the stablecoin ones: scheme: "exact", network: "lnbtc:000000000019d6689c085ae165831e93", amount in millisats, asset: "BTC", payTo (the key that signed the invoice), maxTimeoutSeconds (the invoice's expiry) and extra { assetTransferMethod: "bolt11", paymentFlow: "upfront", requestHash, requestBindingProfile: "http:1", requestBindingParams: { headers: [] }, invoice, refundToken } PAYMENT-SIGNATURE: base64 JSON with x402Version: 2, accepted and payload.preimage PAYMENT-RESPONSE with success, transaction (the payment hash) and network, and no payer
L402 WWW-Authenticate: L402 macaroon="...", invoice="...", first in the header so an MPP client still reads every Payment challenge after it Authorization: L402 <macaroon>:<preimage> no receipt header: the macaroon and the preimage are your record

A refused x402 proof answers 402 again with the scheme's reason in error; a refused MPP credential answers with a problem body naming why, beside fresh challenges.

Clients

payInvoice(bolt11) stands for your Lightning wallet: it pays the invoice and returns the payment's preimage, 64 lowercase hex characters. Each example pays one GET route priced at the floor. A 402 that carries no Lightning option means the server has Lightning off, and each example stops with that message.

// MPP lightning, with mppx
import { Challenge, Credential } from "mppx";

const url = "https://agent402.tools/api/x402-trending";
const quote = await fetch(url); // 402
const challenge = Challenge.deserializeList(quote.headers.get("www-authenticate"))
  .find((c) => c.method === "lightning");
if (!challenge) throw new Error("No Lightning option on this 402: the server has Lightning off");
const preimage = await payInvoice(challenge.request.methodDetails.invoice);
const res = await fetch(url, {
  headers: { Authorization: Credential.serialize(Credential.from({ challenge, payload: { preimage } })) },
});
console.log(res.status, res.headers.get("payment-receipt"));
// a refund claim's offerProof: challenge.id
// x402 exact on lnbtc (a GET with no body)
const url = "https://agent402.tools/api/x402-trending";
const quote = await fetch(url); // 402
const required = JSON.parse(Buffer.from(quote.headers.get("payment-required"), "base64").toString());
const accepted = required.accepts.find((a) => a.network.startsWith("lnbtc:"));
if (!accepted) throw new Error("No Lightning option on this 402: the server has Lightning off");
const preimage = await payInvoice(accepted.extra.invoice);
const payment = Buffer.from(JSON.stringify({ x402Version: 2, accepted, payload: { preimage } })).toString("base64");
const res = await fetch(url, { headers: { "PAYMENT-SIGNATURE": payment } });
console.log(res.status, res.headers.get("payment-response"));
// a refund claim's offerProof: accepted.extra.refundToken
// L402
const url = "https://agent402.tools/api/x402-trending";
const quote = await fetch(url); // 402
const l402 = /L402 macaroon="([^"]+)", invoice="([^"]+)"/.exec(quote.headers.get("www-authenticate") || "");
if (!l402) throw new Error("No Lightning option on this 402: the server has Lightning off");
const [, macaroon, invoice] = l402;
const preimage = await payInvoice(invoice);
const res = await fetch(url, { headers: { Authorization: `L402 ${macaroon}:${preimage}` } });
console.log(res.status);
// a refund claim's offerProof: the macaroon

The served copy of these examples is on /how-to-pay#lightning, with only the wires that server has on.

When the run fails: claimed back in sats

Because the payment is final before the run, a run that answers 400 or more, or whose buyer left before its first byte, is recorded as owed in the refund ledger under the payment hash. Its failure body says so: charged: true, refundOwed: true, the paymentHash, the refundClaim URL, and a payment sentence naming what to send. An invoice that was paid and never presented, or presented after it expired, is owed the same way.

A Lightning payment names no payer, so the refund waits for you to say where to send it:

curl -X POST https://agent402.tools/api/lightning/refund-claim \
  -H 'Content-Type: application/json' \
  -d '{"preimage":"<64 hex>","offerProof":"<from your 402>","invoice":"<BOLT11 for the sats>"}'
# {"claimed":true,"paymentHash":"...","sats":...,"refund":"owed","note":"Refunds are sent by hand, in sats, to this invoice."}

curl "https://agent402.tools/api/lightning/refund-claim?paymentHash=<hex>"
# {"paymentHash":"...","refund":"owed","claimed":true}
  • preimage: the 64 hex characters your wallet returned. Every node that forwarded the payment also learned it, so it claims nothing alone.
  • offerProof: a value only your own 402 carried, by the wire you paid on: the id of the Payment challenge you paid (MPP), extra.refundToken of the accepts entry you paid (x402), or the macaroon you paid with (L402; its expiry does not matter here). It is in neither the invoice nor the payment, and no answer repeats it.
  • invoice: a BOLT11 invoice on the server's Lightning network, for exactly the sats paid (or with no amount), payable for at least 24 hours.

The claim answers 200 with claimed: true; 400 when the preimage or the invoice does not meet the rules above; 403 when offerProof does not match; 404 when nothing is owed for that payment; 409 when an invoice is already on file (an expired one may be replaced) or the refund is no longer owed. Refunds are sent by hand, in sats, to the invoice on file, and the status read shows refund and whether it is claimed.

A proof refused by the region check is held, not owed: its claim answers 409 with refund: "held", and it is not repaid without a person's decision.