Spark SDK
Spark is the fastest, cheapest, and most UX-friendly way to build financial apps and launch assets natively on Bitcoin. It’s a Bitcoin L2 that lets developers move Bitcoin and Bitcoin-native assets (including stablecoins) instantly, at near-zero cost, while staying fully connected to Bitcoin’s infrastructure.
For complete documentation, visit https://docs.spark.money
Installation
npm install @buildonspark/spark-sdk
yarn add @buildonspark/spark-sdk
pnpm add @buildonspark/spark-sdk
Quick Start
Initialize a Wallet
import { SparkWallet } from "@buildonspark/spark-sdk";
const { wallet, mnemonic } = await SparkWallet.initialize({
options: {
network: "MAINNET",
},
});
const { wallet } = await SparkWallet.initialize({
mnemonicOrSeed: "your twelve word mnemonic phrase here ...",
options: {
network: "MAINNET",
},
});
Check Balance
const balance = await wallet.getBalance();
console.log(`Bitcoin balance: ${balance.balance} sats`);
console.log(`Token balances:`, balance.tokenBalances);
Get Deposit Address
const address = await wallet.getSingleUseDepositAddress();
console.log(`Deposit Bitcoin to: ${address}`);
Static Deposit Address
Static deposit addresses are reusable and allow you to receive multiple deposits to the same address.
const staticAddress = await wallet.getStaticDepositAddress();
console.log(`Static deposit address: ${staticAddress}`);
const quote = await wallet.getClaimStaticDepositQuote(transactionId);
console.log(`Credit amount: ${quote.creditAmountSats} sats`);
console.log(`Fee: ${quote.feeSats} sats`);
await wallet.claimStaticDeposit({
transactionId,
creditAmountSats: quote.creditAmountSats,
sspSignature: quote.signature,
});
await wallet.claimStaticDepositWithMaxFee({
transactionId,
maxFee: 500,
});
const addresses = await wallet.queryStaticDepositAddresses();
const utxos = await wallet.getUtxosForDepositAddress(staticAddress);
for (const utxo of utxos) {
console.log(`UTXO: ${utxo.txid}:${utxo.vout}`);
}
Send Bitcoin
const transfer = await wallet.transfer({
receiverSparkAddress: "sp1q...",
amountSats: 10000,
});
const withdrawal = await wallet.withdraw({
onchainAddress: "bc1q...",
amountSats: 50000,
exitSpeed: "FAST",
});
Lightning Payments
const invoice = await wallet.createLightningInvoice({
amountSats: 1000,
memo: "Payment for services",
});
console.log(`Invoice: ${invoice.invoice.encodedInvoice}`);
const payment = await wallet.payLightningInvoice({
invoice: "lnbc...",
maxFeeSats: 100,
});
Token Operations
const { tokenBalances } = await wallet.getBalance();
for (const [tokenId, info] of tokenBalances) {
console.log(`${info.tokenMetadata.tokenName}: ${info.balance}`);
}
const tokenTransfer = await wallet.transferTokens({
tokenIdentifier: "spark1...",
receiverSparkAddress: "sp1q...",
tokenAmount: 100n,
});
Event Handling
import { SparkWalletEvent } from "@buildonspark/spark-sdk";
wallet.on(SparkWalletEvent.TransferClaimed, (transferId, newBalance) => {
console.log(`Received transfer ${transferId}, new balance: ${newBalance}`);
});
wallet.on(SparkWalletEvent.DepositConfirmed, (depositId, newBalance) => {
console.log(`Deposit ${depositId} confirmed, new balance: ${newBalance}`);
});
Platform Support
The SDK supports multiple JavaScript runtimes:
- Browser
- Node.js
- React Native