Lightning Payments
Create Lightning invoices, pay invoices, and inspect payment status.
This guide explains how to create a Lightning invoice, pay a Lightning invoice, estimate Lightning fees, and fetch a Lightning send request.
Create a Lightning Invoice
- Choose an amount in satoshis and an optional memo.
- Call
account.createLightningInvoice().
You can create a BOLT11 invoice using account.createLightningInvoice():
const invoice = await account.createLightningInvoice({
amountSats: 50000,
memo: 'Payment for services'
})
console.log('Lightning invoice:', invoice.invoice)Pay a Lightning Invoice
- Obtain a BOLT11
encodedInvoicestring. - Set
maxFeeSatsto cap routing fees. - Call
account.payLightningInvoice().
You can pay an invoice using account.payLightningInvoice():
const payment = await account.payLightningInvoice({
invoice: 'lnbc500u1p...',
maxFeeSats: 1000
})
console.log('Payment result:', payment)If you enable syncAndRetry, the wallet syncs state after a payment failure and retries account.payLightningInvoice() once only for a stale-leaf error. Both attempts reuse the same transferId. The wallet generates an ID when you omit it.
You can enable the stale-leaf retry when constructing the wallet:
const wallet = new WalletManagerSpark(seedPhrase, {
network: 'MAINNET',
syncAndRetry: true,
})
const account = await wallet.getAccount(0)
await account.payLightningInvoice({
invoice: 'lnbc500u1p...',
maxFeeSats: 1000,
})If a payment attempt fails with LightningPaymentError, keep its transferId with the original invoice and payment options. If you later choose to retry that payment, pass the same ID rather than starting another payment. The error ID is a Spark transfer ID; it is not the Lightning request id accepted by getLightningSendRequest(). See typed error handling.
Estimate Lightning Fees
You can estimate the routing fee before paying using account.quotePayLightningInvoice():
const feeEstimate = await account.quotePayLightningInvoice({
encodedInvoice: 'lnbc500u1p...'
})
console.log('Fee estimate:', Number(feeEstimate), 'satoshis')Older references to getLightningSendFeeEstimate() map to quotePayLightningInvoice().
Fetch a Lightning Send Request
You can load a prior send request by id using account.getLightningSendRequest():
const sendRequest = await account.getLightningSendRequest(payment.id)
if (sendRequest) {
console.log('Payment status:', sendRequest.status)
}Use the id from the object returned by payLightningInvoice().
Next Steps
Learn how to handle Bitcoin layer 1 deposits and withdrawals.