Skip to main content
Learn how to transfer Bitcoin between Spark wallets. This guide covers Spark transfers, fee estimation, and best practices for sending Bitcoin on the Spark network.
Transfer Bitcoin

Transfer Bitcoin

Send Bitcoin to another Spark wallet using a simple transfer method. transfer(params) Transfers Bitcoin to another Spark wallet on the Spark network.
const transferResult = await wallet.transfer({
  receiverSparkAddress: "spark1p...", // Recipient's Spark address
  amountSats: 50000,                  // Amount in satoshis
});

console.log("Transfer successful:", transferResult);

What is a Spark Transfer?

A Spark Transfer is a Bitcoin transfer that occurs entirely within the Spark network. Unlike traditional Bitcoin transactions that go through the blockchain, Spark transfers are:
  • Instant - No waiting for blockchain confirmations
  • Low cost - Minimal fees compared to on-chain transactions
  • Private - Transfers are not visible on the public blockchain
  • Efficient - Uses Spark’s layer 2 infrastructure
Spark transfers are ideal for frequent Bitcoin transactions, micro-payments, and applications requiring instant settlement.

Check Transfer Status

Monitor your transfers and track their status using transfer queries and events. getTransfers(limit?, offset?) Gets all transfers for the wallet with optional pagination.
// Get recent transfers
const transfers = await wallet.getTransfers(10);
console.log("Recent transfers:", transfers.transfers);

// Check specific transfer status
const recentTransfer = transfers.transfers[0];
console.log("Transfer status:", recentTransfer.status);
console.log("Transaction ID:", recentTransfer.txid);

Real-time Transfer Monitoring

Monitor transfer status in real-time using event listeners.
// Listen for transfer events
wallet.on("transfer:claimed", (transferId, updatedBalance) => {
  console.log(`Transfer ${transferId} claimed! New balance: ${updatedBalance} sats`);
});

wallet.on("transfer:completed", (transferId) => {
  console.log(`Transfer ${transferId} completed successfully!`);
});

wallet.on("transfer:failed", (transferId, error) => {
  console.log(`Transfer ${transferId} failed:`, error);
});

Error Handling

Implement proper error handling for failed transfers and edge cases.
async function transferBitcoinSafely(params) {
  try {
    // Check if you have enough balance
    const balance = await wallet.getBalance();
    
    if (balance.balance < params.amountSats) {
      throw new Error("Insufficient balance");
    }

    // Validate recipient address format
    if (!params.receiverSparkAddress.startsWith('spark1p')) {
      throw new Error("Invalid Spark address format");
    }

    // Attempt the transfer
    const result = await wallet.transfer(params);
    console.log("Transfer successful:", result);
    return result;
    
  } catch (error) {
    console.error("Transfer failed:", error.message);
    
    // Handle specific error types
    if (error.message.includes("Insufficient")) {
      console.log("Please check your Bitcoin balance");
    } else if (error.message.includes("Invalid")) {
      console.log("Please verify the recipient address");
    } else if (error.message.includes("Network")) {
      console.log("Network error. Please try again.");
    }
    
    throw error;
  }
}

Best Practices

  • Verify addresses - Always validate recipient Spark addresses before sending
  • Check balances - Ensure you have sufficient Bitcoin balance before transferring
  • Handle errors - Implement proper error handling for failed transfers
  • Monitor events - Use event listeners for real-time transfer updates
  • Test first - Use regtest network for testing transfers
  • Keep records - Store transaction IDs for reference and support

Example: Complete Bitcoin Transfer Flow

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

async function completeBitcoinTransfer() {
  const { wallet } = await SparkWallet.initialize({
    options: { network: "REGTEST" }
  });

  try {
    // 1. Check current balance
    const balance = await wallet.getBalance();
    console.log("Current balance:", balance.balance, "sats");

    // 2. Set up event listeners
    wallet.on("transfer:claimed", (transferId, newBalance) => {
      console.log(`Transfer ${transferId} claimed! New balance: ${newBalance} sats`);
    });

    wallet.on("transfer:completed", (transferId) => {
      console.log(`Transfer ${transferId} completed!`);
    });

    // 3. Transfer Bitcoin
    const transferResult = await wallet.transfer({
      receiverSparkAddress: "spark1p...", // Replace with recipient address
      amountSats: 10000 // Transfer 10,000 sats
    });

    console.log("Transfer initiated:", transferResult);

    // 4. Check transfer status
    const transfers = await wallet.getTransfers(5);
    console.log("Recent transfers:", transfers.transfers);

  } catch (error) {
    console.error("Transfer failed:", error);
  }
}