Deposits and Withdrawals
Fund Spark from Bitcoin layer 1 and withdraw back on-chain.
This guide explains how to get a single-use deposit address, claim deposits, query static deposit addresses, query UTXOs for a deposit address, and withdraw to Bitcoin layer 1.
Get a Single-Use Deposit Address
- Call
account.getSingleUseDepositAddress(). - Send Bitcoin to the returned on-chain address and wait for confirmation.
You can generate a one-time Bitcoin deposit address using account.getSingleUseDepositAddress():
const depositAddress = await account.getSingleUseDepositAddress()
console.log('Send Bitcoin to:', depositAddress)Claim Deposits
- Identify the Bitcoin transaction id that funded the deposit.
- Call
account.claimDeposit()with that id.
You can credit the wallet after the deposit confirms using account.claimDeposit():
const txId = 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16'
const walletLeaves = await account.claimDeposit(txId)
console.log('Deposit claimed:', walletLeaves)1. (optional) Use a static deposit address
Get a reusable Bitcoin address with account.getStaticDepositAddress(). After the deposit confirms, identify the output paying this address and verify its amount in satoshis.
Request a claim quote. The example uses the transaction ID, output index and value that your application verified against the Bitcoin transaction.
const quote = await account.getClaimStaticDepositQuote({
txId: verifiedDeposit.txId,
outputIndex: verifiedDeposit.outputIndex,
depositAmountSats: verifiedDeposit.amountSats
})
if (quote.transactionId !== verifiedDeposit.txId ||
quote.outputIndex !== verifiedDeposit.outputIndex ||
!Number.isSafeInteger(quote.creditAmountSats) ||
quote.creditAmountSats <= 0 ||
quote.creditAmountSats > verifiedDeposit.amountSats) {
throw new Error('Claim quote does not match the verified deposit')
}
console.log('Credit in satoshis:', quote.creditAmountSats)
console.log('Claim fee in satoshis:', quote.claimFeeSats)The displayed fee depends on the output amount you supplied. At claim time the SDK reads the Bitcoin output value and subtracts the freshly quoted credit to check the fee ceiling. The final credit can differ from the preview.
Claiming gives the Spark Service Provider the deposit's secret key and cannot be undone. The claim selects the first output paying any of the account's static deposit addresses. Use this flow for a transaction with one such output.
Pass the fee ceiling your caller approved when invoking claimStaticDeposit(). The returned transfer ID is a Spark identifier.
if (!Number.isSafeInteger(approvedMaxFeeSats) || approvedMaxFeeSats < 0) {
throw new Error('Provide an approved fee cap in whole satoshis')
}
const { transferId } = await account.claimStaticDeposit(verifiedDeposit.txId, {
maxFeeSats: approvedMaxFeeSats
})
console.log('Spark transfer ID:', transferId)If the fresh fee exceeds the cap, the claim throws StaticDepositFeeExceededError. Review the new fee with the caller before retrying.
You can list unused single-use addresses with account.getUnusedDepositAddresses(). The method returns a paginated result with depositAddresses and offset fields.
Query Static Deposit Addresses
You can list all existing static deposit addresses using account.getStaticDepositAddresses():
const addresses = await account.getStaticDepositAddresses()
console.log('Static deposit addresses:', addresses)Query UTXOs for a Deposit Address
You can check confirmed UTXOs for a specific deposit address using account.getUtxosForDepositAddress():
const result = await account.getUtxosForDepositAddress({
depositAddress: 'bc1q...'
})
console.log('Confirmed UTXOs:', result.utxos)
console.log('Offset:', result.offset)Withdraw to Bitcoin Layer 1
- Choose a Bitcoin
onchainAddressandamountSats. - Set
withdrawMaxFeewhen creating the wallet if you need a quoted-fee limit, in satoshis. - Request a cooperative exit quote with
account.quoteWithdraw(). - Call
account.withdraw()with the destination, amount, and exit speed.
You can request a withdrawal fee quote using account.quoteWithdraw():
const feeQuote = await account.quoteWithdraw({
withdrawalAddress: 'bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh',
amountSats: 100000
})
console.log('Withdrawal fee quote:', feeQuote)You can initiate the withdrawal using account.withdraw():
const withdrawal = await account.withdraw({
onchainAddress: 'bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh',
amountSats: 100000,
exitSpeed: 'MEDIUM'
})
console.log('Withdrawal request:', withdrawal)withdraw() fetches a new quote and checks the selected speed's L1 broadcast fee plus user fee against withdrawMaxFee. A greater fee throws MaximumFeeExceededError; equality is allowed. The earlier quote is for inspection and is not the quote used for this check.
Next Steps
Learn how to handle errors and follow operational best practices.