Bitcoin

How do I send Bitcoin to someone


Sending Bitcoin is a straightforward process, but it requires a basic understanding of how transactions work on the Bitcoin network. Below is a detailed guide on how to send Bitcoin to someone, including sample code for programmatic transactions.

Steps to Send Bitcoin

  1. Set Up a Bitcoin Wallet:

    To send Bitcoin, you need a Bitcoin wallet. This wallet can be a software wallet (desktop or mobile), a hardware wallet, or an online wallet. Each wallet has a unique Bitcoin address that you will use to send and receive Bitcoin.

  2. Obtain the Recipient's Bitcoin Address:

    Ask the person you want to send Bitcoin to for their Bitcoin address. This address is a string of alphanumeric characters that identifies their wallet on the Bitcoin network.

  3. Initiate the Transaction:

    Using your wallet, initiate a transaction by entering the recipient's Bitcoin address and the amount of Bitcoin you want to send. Most wallets will also allow you to add a transaction fee, which incentivizes miners to include your transaction in the next block.

  4. Confirm the Transaction:

    Review the transaction details, including the recipient's address and the amount. Once you are sure everything is correct, confirm the transaction. Your wallet will then broadcast the transaction to the Bitcoin network.

  5. Wait for Confirmation:

    After sending, your transaction will be included in a block by miners. Depending on the network congestion and the fee you paid, this can take anywhere from a few minutes to several hours. You can track the transaction status using a blockchain explorer by entering your transaction ID.

Sample Code: Sending Bitcoin Programmatically

The following sample code demonstrates how to send Bitcoin using the BitcoinJS library in JavaScript. This example assumes you already have a wallet set up and have the necessary keys.


    const bitcoin = require('bitcoinjs-lib');
    const axios = require('axios');
    // Your Bitcoin wallet details
    const senderPrivateKey = 'YOUR_PRIVATE_KEY'; // Replace with your private key
    const senderAddress = 'YOUR_SENDER_ADDRESS'; // Replace with your sender address
    const recipientAddress = 'RECIPIENT_ADDRESS'; // Replace with recipient's address
    const amountToSend = 0.001; // Amount in BTC
    // Create a transaction
    async function sendBitcoin() {
        // Get the current network fee
        const feeRate = await getCurrentFeeRate();
        // Create a new transaction
        const keyPair = bitcoin.ECPair.fromPrivateKey(Buffer.from(senderPrivateKey, 'hex'));
        const txb = new bitcoin.TransactionBuilder(bitcoin.networks.bitcoin);
        
        // Fetch UTXOs (Unspent Transaction Outputs)
        const utxos = await getUtxos(senderAddress);
        
        let totalInput = 0;
        utxos.forEach(utxo => {
            txb.addInput(utxo.txid, utxo.vout);
            totalInput += utxo.value;
        });
        // Calculate the change
        const change = totalInput - (amountToSend * 1e8) - (feeRate * 1e8);
        
        // Add output for the recipient
        txb.addOutput(recipientAddress, amountToSend * 1e8);
        
        // Add output for the change
        if (change > 0) {
            txb.addOutput(senderAddress, change);
        }
        // Sign each input
        utxos.forEach((utxo, index) => {
            txb.sign(index, keyPair);
        });
        // Build the transaction and get the hex
        const tx = txb.build();
        const txHex = tx.toHex();
        // Broadcast the transaction
        const response = await broadcastTransaction(txHex);
        console.log(`Transaction ID: ${response.data.txid}`);
    }
    // Function to get UTXOs
    async function getUtxos(address) {
        const response = await axios.get(`https://api.blockcypher.com/v1/btc/main/addresses/${address}?unspentOnly=true`);
        return response .data.txrefs || [];
    }
    // Function to get the current fee rate
    async function getCurrentFeeRate() {
        const response = await axios.get('https://api.blockcypher.com/v1/btc/main');
        return response.data.medium_fee_per_kb; // Fee rate in satoshis per kilobyte
    }
    // Function to broadcast the transaction
    async function broadcastTransaction(txHex) {
        return await axios.post('https://api.blockcypher.com/v1/btc/main/txs/push', {
            tx: txHex
        });
    }
    // Call the sendBitcoin function to execute the transaction
    sendBitcoin().catch(console.error);
    

Conclusion

Sending Bitcoin involves a few simple steps, from setting up a wallet to confirming the transaction. With the right tools and knowledge, you can easily send Bitcoin to anyone around the world. The sample code provided demonstrates how to programmatically send Bitcoin using JavaScript, showcasing the power of the Bitcoin network and its underlying technology.

Written by Surfside Media

Senior Full Stack Developer specializing in Web Technologies.