@buildonspark/spark-sdk

中文文档
0.15.0发布于 2026/11/04Apache-2.0TypeScriptESM + CJS近 30 天下载 11.9万

Spark SDK

原始使用文档
文档目录10 节
  1. Installation
  2. Quick Start
  3. Initialize a Wallet
  4. Check Balance
  5. Static Deposit Address
  6. Send Bitcoin
  7. Lightning Payments
  8. Token Operations
  9. Event Handling
  10. Platform Support

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
# or
yarn add @buildonspark/spark-sdk
# or
pnpm add @buildonspark/spark-sdk

Quick Start

Initialize a Wallet

import { SparkWallet } from "@buildonspark/spark-sdk";

// Create a new wallet (generates a new mnemonic)
const { wallet, mnemonic } = await SparkWallet.initialize({
  options: {
    network: "MAINNET", // or "REGTEST" for testing
  },
});

// Or initialize with an existing mnemonic
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);

Static Deposit Address

Static deposit addresses are reusable and allow you to receive multiple deposits to the same address.

// Get a reusable static deposit address
const staticAddress = await wallet.getStaticDepositAddress();
console.log(`Static deposit address: ${staticAddress}`);

// After sending Bitcoin to the address from an external wallet,
// get a quote for claiming the deposit
const quote = await wallet.getClaimStaticDepositQuote(transactionId);
console.log(`Credit amount: ${quote.creditAmountSats} sats`);
console.log(`Fee: ${quote.feeSats} sats`);

// Claim the deposit with the quote
await wallet.claimStaticDeposit({
  transactionId,
  creditAmountSats: quote.creditAmountSats,
  sspSignature: quote.signature,
});

// Or use the convenience method with a max fee
await wallet.claimStaticDepositWithMaxFee({
  transactionId,
  maxFee: 500, // Will throw if fee exceeds this amount
});

// Query all your static deposit addresses
const addresses = await wallet.queryStaticDepositAddresses();

// Get confirmed UTXOs for a deposit address
const utxos = await wallet.getUtxosForDepositAddress(staticAddress);
for (const utxo of utxos) {
  console.log(`UTXO: ${utxo.txid}:${utxo.vout}`);
}

Send Bitcoin

// Transfer to another Spark address
const transfer = await wallet.transfer({
  receiverSparkAddress: "sp1q...",
  amountSats: 10000,
});

// Withdraw to on-chain Bitcoin address
const withdrawal = await wallet.withdraw({
  onchainAddress: "bc1q...",
  amountSats: 50000,
  exitSpeed: "FAST", // or "MEDIUM", "SLOW"
});

Lightning Payments

// Create a Lightning invoice
const invoice = await wallet.createLightningInvoice({
  amountSats: 1000,
  memo: "Payment for services",
});
console.log(`Invoice: ${invoice.invoice.encodedInvoice}`);

// Pay a Lightning invoice
const payment = await wallet.payLightningInvoice({
  invoice: "lnbc...",
  maxFeeSats: 100,
});

Token Operations

// Get token balances
const { tokenBalances } = await wallet.getBalance();
for (const [tokenId, info] of tokenBalances) {
  console.log(`${info.tokenMetadata.tokenName}: ${info.balance}`);
}

// Transfer tokens
const tokenTransfer = await wallet.transferTokens({
  tokenIdentifier: "spark1...",
  receiverSparkAddress: "sp1q...",
  tokenAmount: 100n,
});

Event Handling

import { SparkWalletEvent } from "@buildonspark/spark-sdk";

// Listen for incoming transfers
wallet.on(SparkWalletEvent.TransferClaimed, (transferId, newBalance) => {
  console.log(`Received transfer ${transferId}, new balance: ${newBalance}`);
});

// Listen for confirmed deposits
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

版本与发布记录

@buildonspark/spark-sdk 共发布 168 个版本,首次发布于 2025/02/26;过去 12 个月发布了 60 个版本。

版本发布日期距今
0.15.0latest2026/11/044 天前
0.14.02026/10/1524 天前
0.13.02026/09/301 个月前
0.12.12026/09/221 个月前
0.12.02026/09/121 个月前
0.11.02026/08/282 个月前

其他发布通道:experimental → 0.7.2-experimental.0。

关于 @buildonspark/spark-sdk 的常见问题

@buildonspark/spark-sdk 是什么?

Spark SDK

如何安装 @buildonspark/spark-sdk?

在项目目录运行 npm install @buildonspark/spark-sdk,或使用 pnpm add / yarn add / bun add。

@buildonspark/spark-sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@buildonspark/spark-sdk 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@buildonspark/spark-sdk 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@buildonspark/spark-sdk 可以商用吗?

@buildonspark/spark-sdk 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@buildonspark/spark-sdk 的下载量有多少?

根据 npm 官方下载统计,@buildonspark/spark-sdk 在 2026/10/09 – 2026/11/07 这 30 天内被下载 119,341 次,日均约 3,978 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@buildonspark/spark-sdk 还在维护吗?

最新版本 0.15.0 发布于 2026/11/04(4 天前);至今共发布 168 个版本,其中过去 12 个月发布了 60 个;首次发布于 2025/02/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。