The LNbits API provides core Lightning Network functionality including user authentication, wallet management, payment processing, and invoice generation.
Import
import lnbits from '@/api/lnbits'
Authentication
register
Register a new user account and create a default wallet with paylink.
const accessToken = await lnbits . register ( username , email , password )
Unique username for the account
Email address for the account
JWT access token for authenticated requests
import lnbits from '@/api/lnbits'
try {
const accessToken = await lnbits . register (
'alice' ,
'[email protected] ' ,
'securePassword123'
)
// Store accessToken securely
} catch ( error ) {
console . error ( 'Registration failed:' , error . message )
}
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...'
login
Authenticate an existing user and obtain an access token.
const accessToken = await lnbits . login ( username , password )
JWT access token for authenticated requests
import lnbits from '@/api/lnbits'
try {
const accessToken = await lnbits . login ( 'alice' , 'securePassword123' )
// Store accessToken securely
} catch ( error ) {
console . error ( 'Login failed:' , error . message )
}
getUser
Retrieve user information including all wallets and balances.
const user = await lnbits . getUser ( accessToken )
JWT access token from login or register
User information and wallets Array of wallet objects with id, name, balance, adminkey, and inkey
Total balance across all wallets in satoshis
import lnbits from '@/api/lnbits'
const user = await lnbits . getUser ( accessToken )
console . log ( `Total balance: ${ user . totalBalance } sats` )
console . log ( `Wallets: ${ user . wallets . length } ` )
import useUser from '@/hooks/query/useUser'
function ProfileComponent () {
const { data : user , isLoading } = useUser ( accessToken )
if ( isLoading ) return < Loading />
return (
< div >
< h1 >{user. username } </ h1 >
< p > Balance : { user . totalBalance } sats </ p >
</ div >
)
}
Wallet Management
createWallet
Create a new wallet for the authenticated user.
const wallet = await lnbits . createWallet ( name , adminkey )
Display name for the new wallet
Admin key from an existing wallet
Newly created wallet with id, name, balance, adminkey, and inkey
updateWalletName
Update the display name of an existing wallet.
const wallet = await lnbits . updateWalletName ( name , adminkey )
New display name for the wallet
Admin key of the wallet to update
deleteWallet
Permanently delete a wallet.
await lnbits . deleteWallet ( walletId , adminkey )
ID of the wallet to delete
Admin key with permission to delete wallets
Payments
getPayments
Retrieve all successful payments for a wallet with historical fiat prices.
const payments = await lnbits . getPayments ( inkey )
Invoice key of the wallet
Array of payment transactions with fiat snapshots
const payments = await lnbits . getPayments ( inkey )
payments . forEach ( payment => {
console . log ( ` ${ payment . sats } sats - ${ payment . fiatSnapshot . USD } USD` )
})
import usePayments from '@/hooks/query/usePayments'
function PaymentsComponent ({ inkeys }) {
const { data : payments , isLoading } = usePayments ( inkeys , true )
if ( isLoading ) return < Loading />
return (
< ul >
{ payments [0]?. map ( payment => (
< li key = {payment. id } >
{ payment . sats } sats - { payment . fiatSnapshot . USD } USD
</ li >
))}
</ ul >
)
}
getPaginatedPayments
Retrieve paginated payment history with limit and offset support.
const payments = await lnbits . getPaginatedPayments (
inkey ,
{ limit: 20 , offset: 0 , walletId: 'wallet_id' },
snapshots ,
addSnapshot
)
Invoice key of the wallet
Number of payments to fetch
Number of payments to skip
Filter by specific wallet ID
snapshots
Record<string, FiatSnapshot>
required
Cache of historical price snapshots
Callback to add snapshot to cache
Invoices
createInvoice
Generate a Lightning invoice for receiving payments.
const invoice = await lnbits . createInvoice ( amount , inkey , memo )
Invoice key of the receiving wallet
Optional description for the invoice
Invoice object with payment_request and payment_hash
const invoice = await lnbits . createInvoice (
1000 ,
inkey ,
'Payment for coffee'
)
console . log ( 'Invoice:' , invoice . payment_request )
console . log ( 'Hash:' , invoice . payment_hash )
import useCreateInvoice from '@/hooks/mutation/useCreateInvoice'
function ReceiveComponent ({ inkey }) {
const createInvoice = useCreateInvoice ( inkey , 1000 , 'Coffee' )
return (
< button onClick = {() => createInvoice.mutate()} >
Create Invoice
</ button >
)
}
pay
Pay a Lightning invoice (bolt11) or LNURL.
await lnbits . pay ( payData , adminkey )
Payment data object type
'bolt11' | 'lnurl'
required
Payment type
Bolt11 invoice string (for type=‘bolt11’)
LNURL callback URL (for type=‘lnurl’)
Amount in sats (for type=‘lnurl’)
Optional comment (for type=‘lnurl’)
Description hash (for type=‘lnurl’)
Description (for type=‘lnurl’)
Admin key of the paying wallet
const payData = {
type: 'bolt11' ,
invoice: 'lnbc1000n1...'
}
await lnbits . pay ( payData , adminkey )
const payData = {
type: 'lnurl' ,
callback: 'https://...' ,
amount: 1000 ,
comment: 'Thanks!' ,
descriptionHash: '...' ,
description: 'Coffee payment'
}
await lnbits . pay ( payData , adminkey )
import usePay from '@/hooks/mutation/usePay'
function PayComponent ({ adminkey }) {
const pay = usePay ( adminkey )
const handlePay = () => {
pay . mutate ({
type: 'bolt11' ,
invoice: 'lnbc1000n1...'
})
}
return < button onClick ={ handlePay }> Pay Invoice </ button >
}
payInvoice
Directly pay a bolt11 Lightning invoice.
const payment = await lnbits . payInvoice ( invoice , adminkey )
Admin key of the paying wallet
payLnurl
Pay an LNURL invoice.
const payment = await lnbits . payLnurl ( payload , adminkey )
Admin key of the paying wallet
LNURL & Paylinks
createPaylink
Create an LNURL paylink for receiving payments.
const lnurl = await lnbits . createPaylink ( adminkey )
LNURL string for receiving payments
getPaylinks
Retrieve all paylinks for a wallet.
const paylinks = await lnbits . getPaylinks ( inkey )
Invoice key of the wallet
Conversion & Rates
convert
Convert satoshis to fiat currency using current exchange rate.
const fiatAmount = await lnbits . convert ( sats , fiat )
Amount in satoshis to convert
fiat
SupportedFiatCurrencies
required
Target fiat currency (e.g., ‘USD’, ‘EUR’, ‘GBP’)
Converted amount in the specified fiat currency
rate
Get the current Bitcoin exchange rate for a fiat currency.
const exchangeRate = await lnbits . rate ( fiat )
fiat
SupportedFiatCurrencies
required
Fiat currency code
Current Bitcoin price in the specified fiat currency
WebSocket Subscriptions
subscribePaymentWs
Subscribe to payment status updates via WebSocket.
const webSocket = lnbits . subscribePaymentWs ( paymentHash , callback )
Callback function invoked when payment is confirmed
WebSocket connection object
const invoice = await lnbits . createInvoice ( 1000 , inkey )
const ws = lnbits . subscribePaymentWs (
invoice . payment_hash ,
() => {
console . log ( 'Payment received!' )
}
)
// Cleanup
ws . close ()
subscribeInkeyWs
Subscribe to all incoming payments for a wallet via WebSocket.
const webSocket = lnbits . subscribeInkeyWs ( inkey , callback )
Invoice key of the wallet to monitor
Callback function invoked with payment amount when received
WebSocket connection object
const ws = lnbits . subscribeInkeyWs (
inkey ,
( amount ) => {
console . log ( `Received ${ amount } sats` )
}
)
// Cleanup
ws . close ()