Skip to main content
Pay Lightning invoices using any token tradeable on Flashnet. The SDK automatically swaps your tokens to BTC via the AMM and uses the BTC to pay the invoice, all in a single function call.
Available in SDK version 0.4.1+. Rollback feature and improved defaults in 0.4.2+.

How It Works

  1. Decode invoice - Extract the payment amount from the BOLT11 invoice
  2. Estimate fees - Get Lightning routing fee estimate from Spark
  3. Find best pool - Select the pool with optimal liquidity for your token → BTC swap
  4. Execute swap - Swap tokens to BTC via Flashnet AMM
  5. Pay invoice - Use the received BTC to pay the Lightning invoice
The entire flow is bundled into two simple functions: getPayLightningWithTokenQuote() for quotes and payLightningWithToken() for execution.

Getting a Quote

Always get a quote first to see the total cost:

Quote Response

Executing Payment

After reviewing the quote, execute the payment:

Payment Options

Payment Result

Complete Example

Fee Breakdown

The total token cost includes multiple fee components:
  1. Lightning routing fee - Network fee to route the payment (~0.1-0.5%)
  2. AMM LP fee - Liquidity provider fee (typically 20 bps)
  3. Integrator fee - Optional fee for integrators (if specified)
  4. BTC variable fee - Up to 63 sats adjustment for AMM leaf management
All fees are bundled into tokenAmountRequired in the quote—what you see is what you pay.

Fee Calculation Example

For a 5000 sat invoice with 20 bps LP fee:

Minimum Amounts

Flashnet enforces minimum amounts for swaps:
  • BTC output: 1,000 sats minimum
  • Token input: Varies by token (typically 1 USD equivalent)
Invoices below the BTC minimum will throw an error with a clear message.

Instant Payment with Existing BTC

If you already have BTC in your wallet, you can pay the invoice immediately without waiting for the swap to complete:
When enabled, this:
  1. Checks if you have enough BTC to cover the invoice + Lightning fee
  2. If yes: executes swap → pays immediately → swap BTC arrives async
  3. If no: falls back to normal flow (waits for swap to complete)
This gracefully falls back to waiting if BTC balance is insufficient—no error is thrown.

Rollback on Failure

If the Lightning payment fails after the swap succeeds, you can enable automatic rollback to swap the BTC back to your original token:
Rollback incurs an additional swap fee and may result in slightly fewer tokens due to price movement.

Error Handling

Next Steps

  • Swaps - Learn more about AMM swaps
  • Fees - Understand the fee structure
  • Pools - View available liquidity pools