Add Settle to your website
Settle works the way a card gateway does. Your server creates an order, Settle Checkout opens over your page, and your server confirms the payment. Your customer pays in USDT with an email address, and needs no wallet and no gas.
01Install
The SDK runs on your server, on Node 20 or later.
npm install @akhr2405/settle-pay02Get your keys
Log in to Settle and open Developers. Your Key ID is public. Your Key Secret can move your account's money, like a card gateway's secret key, so keep it on your server and use one Settle account per store. Settle never sees or stores it.
03Create an order
Create the order on your server when the customer clicks Buy. receipt is your own order number: only you can read it, and it comes back when the order is paid.
import Settle from "@akhr2405/settle-pay";
const settle = new Settle({
keyId: process.env.SETTLE_KEY_ID,
keySecret: process.env.SETTLE_KEY_SECRET,
businessName: "Your business",
baseUrl: "https://<your Settle>",
});
// Create an order when the customer clicks Buy.
app.post("/orders", async (req, res) => {
const order = await settle.orders.create({
amount: "3.00",
description: "Masala chai blend",
receipt: "order_42",
});
res.json(order);
});
// Confirm the payment before you ship anything.
app.post("/verify", async (req, res) => {
const payment = await settle.payments.verify(req.body);
res.json(payment);
});04Open checkout
Load checkout.js and open checkout from a click. It opens in its own window, so your customer stays on your page. If a browser blocks the window, the whole page goes to Settle and comes back to the order's callbackUrl.
<script src="https://<your Settle>/checkout.js"></script>
<script>
const post = (url, body) =>
fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
}).then((response) => response.json());
buyButton.onclick = () =>
new Settle.Checkout({
orderId: () => post("/orders").then((order) => order.id),
handler: (response) => post("/verify", response),
}).open();
</script>| Option | What it does |
|---|---|
| orderId | An order's id, or a function that creates the order and gives its id. Required. |
| handler(response) | Runs after payment with settle_order_id and settle_payment_id. |
| onDismiss() | Runs when the customer closes checkout without paying. |
| onError(error) | Runs when your orderId function fails. |
| redirect | true skips the window and uses the whole page. |
05Verify the payment
Never trust the browser on its own. payments.verify reads Whitechain, not Settle. It throws unless the payment exists, paid that order, and the order is yours. It needs no shared secret, because the chain is the proof. The result includes what reached you after the network fee, your order number, and a link to your customer's receipt.
06Webhooks
A webhook reaches your server even if your customer closes their browser. Add your address on the Developers page and copy its signing secret. Answer with any 2xx status. Settle tries 3 times, and a failed delivery can be resent from the Developers page. An event can arrive more than once, so use its id to act only once.
// Keep the raw body: the signature covers the exact bytes.
app.post(
"/webhooks/settle",
express.raw({ type: "application/json" }),
(req, res) => {
const event = settle.webhooks.verify(
req.body,
req.headers["x-settle-signature"],
process.env.SETTLE_WEBHOOK_SECRET,
);
if (event.type === "order.paid") fulfil(event.data.receipt);
res.sendStatus(200);
},
);| Event | Sent when | Its data holds |
|---|---|---|
| order.paid | An order is paid, by any route. | orderId, paymentId, amount, received, networkFee, receipt, payer, receiptUrl |
| refund.created | You refund, in part or in full. | orderId, refundId, amount, totalRefunded, receipt, payer, receiptUrl |
| webhook.test | You press Send test event. | message |
Sell to AI agents
Agents can buy from you too, with no checkout page. Put a price on a route: an agent that calls it gets a 402 with the price, pays over x402, and repeats the request. It needs no account, no API key and no gas, and any standard x402 client works.
// One route, and agents can buy from you. They need no account, no API key and no gas.
export async function POST(request) {
const { product, quantity, requestId } = await request.json();
const sale = await settle.agents.charge(request, {
amount: priceOf(product, quantity),
description: `${quantity} x ${product}`,
// The agent's own id for this order, so a retry is never charged twice.
receipt: `order-${requestId}`,
});
// No payment yet: this is the 402 with your price.
if (!sale.paid) return sale.response;
return sale.respond({ order: sale.receipt, proof: sale.proofUrl });
}// Any standard x402 client can buy. This is Coinbase's, with a limit on each payment.
import { wrapFetchWithPaymentFromConfig } from "@x402/fetch";
import { ExactEvmScheme } from "@x402/evm";
const pay = wrapFetchWithPaymentFromConfig(fetch, {
schemes: [{ network: "eip155:1874", client: new ExactEvmScheme(account) }],
spendControls: {
allowedAssets: [{
network: "eip155:1874",
asset: "0xcedfB56113143d55b2358b8CFbB0dEB5Ad2f55a7",
maxAmountPerPayment: "5000000", // 5.00 USDT
}],
},
});
await pay("https://your-store.example/api/agent/buy", {
method: "POST",
body: JSON.stringify({ product: "masala", quantity: 3, requestId: crypto.randomUUID() }),
});- Each order lands in your dashboard under Agent sales, in your CSV export, and at your webhook as
order.paidwithagent: true. - Build the order number from an id the agent sends. A retry with the same id gets the same order back and is never charged twice.
- The network fee comes out of the sale, in the same transaction.
- An agent pays you directly, so its order has no refund button. To refund one, send the money back from your Account page.
- The cap above is per payment. An agent's total budget is what its wallet holds.
Refunds and payment links
A refund goes back to whoever paid and shows on their receipt. Everything you make with the SDK also appears in your Settle dashboard, where you can refund and export it.
// Send part back, or leave out the amount to refund all of it.
await settle.refunds.create({ orderId: order.id, amount: "1.00" });
// No website? A payment link is an invoice with a due date.
const link = await settle.paymentLinks.create({
amount: "250.00",
description: "Website hosting, November",
customer: "ops@acme.example",
});
console.log(link.url);Reference
| Call | What it does |
|---|---|
| orders.create(order) | Creates an order to pay at checkout. |
| orders.fetch(id) | One of your orders, read from the chain. |
| orders.all() | All your orders and payment links, newest first. |
| orders.cancel(id) | Cancels an order nobody has paid. |
| orders.waitForPayment(id, options) | Resolves once it is paid. For scripts. |
| orders.pay(id) | Pays any open order from this account. For a script or an agent. |
| payments.verify(response) | Confirms a payment against Whitechain. |
| refunds.create({ orderId, amount }) | Refunds in part, or in full with no amount. |
| paymentLinks.create(link) | An invoice with a due date and a link to share. |
| agents.charge(request, price) | Puts a price on a route, so AI agents can buy over x402. |
| webhooks.verify(body, signature, secret) | Checks a webhook came from Settle. |
| account.balance() | This account's USDT. |
| account.withdraw({ to, amount }) | Sends USDT to any address, with no gas. |
| fees() | Current network fees and the smallest order. |
Errors
A failed call throws SettleError. Its message says what went wrong, and its code is stable, so you can branch on it.
| Code | Meaning |
|---|---|
| order_not_paid | The order hasn't been paid. |
| payment_mismatch | That payment didn't pay this order. |
| not_your_order | The order belongs to another account. |
| invalid_amount | Not a positive amount with up to 6 decimals. |
| invoice_too_small | Below the smallest order. See fees(). |
| RefundTooLarge | More than can still be refunded. |
| too_many_requests | Over 30 requests a minute from one address. |
Fees and limits
- Settle adds no fee. A flat network fee repays the cost of sending each payment.
- The smallest order is 1 USDT.
- A new account can hold 5 unpaid orders until its first payment arrives.
- An account can create 50 orders a day, and send 30 requests a minute.
- This is a test version on Whitechain Sepolia. Customers can add free test USDT.