Send a payment
Sending is two steps: prepare (shows the fee and validates), then send.
Future<breez.Payment?> payInvoice(breez.BreezSdk sdk, String bolt11, {BigInt? amountSats, required String idempotencyKey}) async {
final parsed = await sdk.parse(input: bolt11);
if (parsed is! breez.InputType_Bolt11Invoice) throw StateError('not a BOLT11 invoice');
if (parsed.field0.amountMsat == null && amountSats == null) throw StateError('invoice has no amount');
final prep = await sdk.prepareSendPayment(
request: breez.PrepareSendPaymentRequest(
paymentRequest: breez.PaymentRequest.input(input: bolt11),
amount: amountSats, // only for amount-less invoices
),
);
if (prep.amount < BigInt.from(330)) throw StateError('minimum is 330 sats');
final m = prep.paymentMethod;
final fee = m is breez.SendPaymentMethod_Bolt11Invoice ? m.lightningFeeSats : BigInt.zero;
// show or log: prep.amount + fee; check the balance covers both before sending
final res = await sdk.sendPayment(
request: breez.SendPaymentRequest(
prepareResponse: prep,
options: const breez.SendPaymentOptions.bolt11Invoice(preferSpark: false, completionTimeoutSecs: 30),
idempotencyKey: idempotencyKey, // a UUID you store BEFORE the call: a retry must reuse it
),
);
return res.payment; // status completed | pending | failed
}
Rules that matter for money:
- Idempotency. Create the key, save it with the payout in your database, then call. After a crash or a timeout, retry with the same key. Never create a new key for the same payout.
pendingis notfailed. After the 30 second wait the payment may still complete. PolllistPaymentsorgetPaymentfor the final status before you refund or retry. Onlyfailedmeans the funds did not leave.- Check the balance first:
getInfo(request: GetInfoRequest(ensureSynced: false))returnsbalanceSats. For a fresh figure callsyncWalletfirst. If the sync cannot reach the operators it throws; treat that as "unknown", not as zero. - Lightning routing can fail (no route, recipient offline). Show the user the real error and let them retry later.
prepareSendPaymentcan fail withLIQUIDITY_RESERVEDwhen the SSP's Lightning capacity is low and the wallet has no priority (section 5.4). No funds have moved. Handle it as described in section 9.