Skip to main content
Send Bitcoin instantly between Spark wallets with zero fees.
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.
getTransfers() includes Spark transfers, Lightning sends/receives, and cooperative exits. For token transaction details, use queryTokenTransactions().
// 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 ID:", recentTransfer.id);
console.log("Transfer status:", recentTransfer.status);
console.log("Amount:", recentTransfer.totalValue, "sats");

Real-time Transfer Monitoring

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

// Note: There are no events for outgoing transfers.
// The transfer() method returns immediately when the transfer completes.
The transfer:claimed event only fires for incoming transfers. For outgoing transfers, the transfer() method returns a WalletTransfer object when complete.

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('spark1') && 
        !params.receiverSparkAddress.startsWith('sparkrt1')) {
      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;
  }
}

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 for incoming transfers
    wallet.on("transfer:claimed", (transferId, newBalance) => {
      console.log(`Incoming transfer ${transferId} claimed! New balance: ${newBalance} sats`);
    });

    // 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);
  }
}