Charge for an HTTP response without taking custody.
LDGR402 uses HTTP 402 Payment Required to describe an exact LDGR payment, then asks the wallet to approve an ordinary signed transfer before the client retries.
If you came here from x402
x402 is the wider name used for HTTP payment challenges. LDGR’s native flow is called LDGR402: it settles on LDGR, uses the existing self-custody wallet, and does not require a facilitator, a hosted wallet, or a new ledger action. An adapter for external x402 schemes is future work, not a shipped promise.
Four exact pieces make the flow safe.
- Requirement
- The merchant publishes the ledger, asset, decimal amount, recipient, unpredictable challenge, expiry, and optional description.
- Wallet approval
- The customer reviews and signs the ordinary LDGR transfer in a self-custody wallet. The merchant never receives the private key.
- Payment proof
- The browser retries with a versioned, base64url-encoded
LDGR402-Paymentheader containing the challenge and settled transaction hash. - Merchant verification
- The backend loads its persisted terms and checks the hash against the authoritative payment endpoint before granting access or fulfilling an order.
The request loop
Return 402 with an exact requirement and persist the challenge before sending it to the browser.
Parse the requirement and let the connected wallet show the final asset, amount, and recipient for confirmation.
Send the same request again with the transaction-hash proof in the LDGR402-Payment header.
Verify settlement, atomically record the outcome, and return the protected response. Same-hash retries are safe.
Browser: pay and retry.
The browser SDK validates the requirement before it reaches the wallet. Use t0 on testnet and t1 on mainnet for the native public asset IDs.
import { LDGR402_PAYMENT_HEADER, encodeLdgr402Payment, parseLdgr402Requirement, payLdgr402 } from "@ldgr/wallet-sdk/browser";
const requirement = parseLdgr402Requirement(await response.json());
const payment = await payLdgr402(requirement);
const retry = await fetch(request, {
headers: { [LDGR402_PAYMENT_HEADER]: encodeLdgr402Payment(payment) },
});Backend: verify before fulfilment.
Store the expected requirement with the order or resource. On a retry, verify the proof against those stored terms, then make the challenge transition and entitlement update one idempotent write.
import { createLdgrClient } from "@ldgr/client";
const client = createLdgrClient({ network: "mainnet" });
const verification = await client.payments.verifyLdgr402(
request.headers.get("LDGR402-Payment") ?? "",
storedRequirement,
);
if (verification.status !== "confirmed") {
return new Response("Payment not settled", { status: 422 });
}
await challengeStore.fulfill(storedRequirement.challenge, verification.transactionHash);Express / Node.js: one protected payment endpoint.
This small server issues the requirement, verifies the retry, and returns the paid response from the same route. It defaults to testnet, so no API endpoint is needed in the client configuration.
npm install express @ldgr/client @ldgr/protocol
# LDGR_NETWORK=testnet (default) or mainnet
# LDGR_MERCHANT_ADDRESS must be the full Bech32 address that receives paymentimport express from "express";
import { createLdgrClient } from "@ldgr/client";
import {
LDGR402_PAYMENT_HEADER,
decodeLdgr402Payment,
parseLdgr402Requirement,
} from "@ldgr/protocol";
const app = express();
const network = process.env.LDGR_NETWORK === "mainnet" ? "mainnet" : "testnet";
const ledgerId = network === "mainnet" ? 1 : 0;
const asset = ledgerId === 1 ? "t1" : "t0";
const merchantAddress = process.env.LDGR_MERCHANT_ADDRESS;
if (!merchantAddress) throw new Error("LDGR_MERCHANT_ADDRESS is required");
const client = createLdgrClient({ network });
const challenges = new Map(); // Replace with a database table in production.
app.get("/api/report", async (req, res) => {
const paymentHeader = req.get(LDGR402_PAYMENT_HEADER);
if (!paymentHeader) {
const requirement = parseLdgr402Requirement({
version: 1,
ledgerId,
asset, // Use your exact public FT code here for a token payment.
amount: "0.25",
recipient: merchantAddress,
challenge: crypto.randomUUID().replaceAll("-", ""),
expiresAt: Date.now() + 10 * 60_000,
description: "One protected API response",
});
challenges.set(requirement.challenge, { requirement });
return res.status(402).type("application/ldgr402+json").json(requirement);
}
let payment;
try {
payment = decodeLdgr402Payment(paymentHeader);
} catch {
return res.status(400).json({ error: "Invalid LDGR402 payment" });
}
const record = challenges.get(payment.challenge);
if (!record) return res.status(404).json({ error: "Unknown LDGR402 challenge" });
// Same-hash retries return the original result; another hash is a conflict.
if (record.fulfillment) {
if (record.fulfillment.transactionHash === payment.transactionHash) {
return res.json(record.fulfillment.response);
}
return res.status(409).json({ error: "Challenge was fulfilled by another payment" });
}
const verification = await client.payments.verifyLdgr402(
paymentHeader,
record.requirement,
);
if (verification.status === "expired") {
return res.status(410).json({ error: "LDGR402 challenge has expired" });
}
if (verification.status !== "confirmed") {
return res.status(422).json({ error: "Payment does not match requirement" });
}
const response = {
paid: true,
transactionHash: verification.transactionHash,
};
// In production, make this compare-and-set and update the entitlement/order
// in the same database transaction.
record.fulfillment = { transactionHash: verification.transactionHash, response };
return res.json(response);
});
app.listen(3000, () => console.log("LDGR402 merchant listening on :3000"));The browser pays the returned requirement and retries the same URL with LDGR402-Payment. For a production service, replace the Map with durable challenge records and make fulfillment an atomic compare-and-set. SetLDGR_NETWORK=mainnet to use t1; for a fungible token, keep the selected ledger and use that token's public code as asset.
A wallet callback, browser event, or client-side status is not access proof. Use the transaction hash and authoritative payment verification before delivering content. For a retry, the same challenge and hash should return the original result; a different hash is a conflict.