Skip to main content
The Boltz Swap API enables seamless swaps between Lightning Network and on-chain Bitcoin. You can create submarine swaps (Lightning → on-chain), reverse swaps (on-chain → Lightning), and configure automatic reverse swaps.

Import

import lnbits from '@/api/lnbits'
All swap functions are part of the LNbits API module and require Boltz extension to be enabled on the LNbits server.

Configuration

getBoltzConfig

Retrieve Boltz service configuration including fees and limits.
const config = await lnbits.getBoltzConfig()
config
object
Boltz configuration with service limits and fees
const config = await lnbits.getBoltzConfig()

console.log(`Swap limits:`)
console.log(`  Min: ${config.minimal} sats`)
console.log(`  Max: ${config.maximal} sats`)

Submarine Swaps (Lightning → On-chain)

Submarine swaps allow you to send Lightning funds and receive on-chain Bitcoin.

createSwap

Create a submarine swap or reverse swap.
const swap = await lnbits.createSwap(data, adminkey)
data
CreateSwapData
required
Swap configuration
adminkey
string
required
Admin key of the wallet
swap
object
Created swap object with status and details
// Lightning → On-chain
const swap = await lnbits.createSwap(
  {
    walletId: 'wallet_abc123',
    address: 'bc1q...',  // Your Bitcoin address
    amount: 100000,       // 100k sats
    direction: 'in',
    amountOption: 'send'
  },
  adminkey
)

console.log('Pay this invoice to complete swap:')
console.log(swap.bip21)

Swap Management

listSwaps

Retrieve all submarine swaps (Lightning → on-chain) for the user.
const swaps = await lnbits.listSwaps(adminkey)
adminkey
string
required
Admin key with permission to view swaps across all wallets
swaps
array
Array of submarine swap objects

listReverseSwaps

Retrieve all reverse swaps (on-chain → Lightning) for the user.
const swaps = await lnbits.listReverseSwaps(adminkey)
adminkey
string
required
Admin key with permission to view swaps across all wallets
swaps
array
Array of reverse swap objects
const [submarine, reverse] = await Promise.all([
  lnbits.listSwaps(adminkey),
  lnbits.listReverseSwaps(adminkey)
])

console.log(`Submarine swaps: ${submarine.length}`)
console.log(`Reverse swaps: ${reverse.length}`)

Auto Swaps

Automatic reverse swaps allow you to automatically convert incoming Lightning payments to on-chain Bitcoin when your wallet balance exceeds a threshold.

createAutoSwap

Configure automatic reverse swaps for a wallet.
const autoSwap = await lnbits.createAutoSwap(data, adminkey)
data
CreateAutoSwapData
required
Auto-swap configuration
adminkey
string
required
Admin key of the wallet
autoSwap
object
Created auto-swap configuration
// Auto-swap when balance exceeds 500k sats
const autoSwap = await lnbits.createAutoSwap(
  {
    walletId: 'wallet_abc123',
    amount: 500000,
    address: 'bc1q...'
  },
  adminkey
)

console.log('Auto-swap configured')
console.log(`Trigger: ${autoSwap.balance} sats`)
console.log(`Destination: ${autoSwap.onchain_address}`)

listAutoReverseSwaps

Retrieve all configured auto-swap settings.
const autoSwaps = await lnbits.listAutoReverseSwaps(adminkey)
adminkey
string
required
Admin key with permission to view auto-swaps across all wallets
autoSwaps
array
Array of auto-swap configuration objects
const autoSwaps = await lnbits.listAutoReverseSwaps(adminkey)

autoSwaps.forEach(config => {
  console.log(`Wallet: ${config.wallet}`)
  console.log(`Threshold: ${config.balance} sats`)
  console.log(`Address: ${config.onchain_address}`)
})

deleteAutoSwap

Remove an auto-swap configuration.
await lnbits.deleteAutoSwap(id, adminkey)
id
string
required
Auto-swap configuration ID to delete
adminkey
string
required
Admin key of the wallet
await lnbits.deleteAutoSwap('config_id_123', adminkey)
console.log('Auto-swap disabled')

Swap Types Explained

Submarine Swaps (Lightning → On-chain)

  1. Create a submarine swap with direction: 'in'
  2. Pay the Lightning invoice provided in the response
  3. Receive on-chain Bitcoin at your specified address
  4. Use for: Cashing out to on-chain, cold storage, exchanges

Reverse Swaps (On-chain → Lightning)

  1. Create a reverse swap with direction: 'out'
  2. Send on-chain Bitcoin to the provided address
  3. Receive Lightning funds in your wallet
  4. Use for: Topping up Lightning wallet, instant liquidity

Auto Swaps

  1. Configure auto-swap with a balance threshold
  2. Automatically triggered when wallet balance exceeds threshold
  3. Converts excess Lightning balance to on-chain Bitcoin
  4. Use for: Automatic treasury management, risk reduction

Best Practices

Fee Awareness

const config = await lnbits.getBoltzConfig()

// Check fees before creating swap
const swapAmount = 100000
if (swapAmount < config.minimal || swapAmount > config.maximal) {
  throw new Error('Amount outside allowed range')
}

Error Handling

try {
  const swap = await lnbits.createSwap(data, adminkey)
  console.log('Swap created:', swap.id)
} catch (error) {
  console.error('Swap failed:', error.message)
  // Handle insufficient balance, invalid address, etc.
}

Monitoring Swaps

// Poll swap status
const checkSwapStatus = async () => {
  const swaps = await lnbits.listSwaps(adminkey)
  const pendingSwaps = swaps.filter(s => s.status === 'pending')
  
  console.log(`${pendingSwaps.length} swaps pending`)
}

// Check every 30 seconds
setInterval(checkSwapStatus, 30000)
  • See /api/lnbits for payment and wallet management functions
  • Swap fees are paid from the swap amount automatically
  • All swaps use instant settlement for reverse swaps when enabled

Build docs developers (and LLMs) love