Simple Bitcoin Transaction 1–1 2019–2020 | NodeJs

JavaScript Tutorials by Ivan Cabral

Ivan Cabral
Dec 22, 2019 · 3 min read

I create my first wallet in 2017 and I must confess what this is hard at begin, but learning and with practice this is easy

Different developers search how to make bitcoin transactions but they have insufficient information and leaves that work for this, I will explain to you how to create that transaction and provide you my own module for making these transactions

First, we need to install bitcoinjs-lib and axios packages

$ npm i axios && npm i bitcoinjs-lib


This is the module what I make to show you how to send a simple transaction

For making the transaction we need the private key, public key (address) and the last transaction

When we have the transaction hex string we will send them by

The transactions have a commission of 10000 Satoshi in this time are 0,74 USD

const bitcoin = require("bitcoinjs-lib");
const axios = require("axios");
const BITCOIN_FEE = 10000;
function getInputs(utxos){
return new Promise(res=>{
let inputs = [];
let utxosCount = utxos.length;
let count = 0;
utxos.forEach(async utxo => {
if (utxo.confirmations >= 6) {
let urlTxes = await axios.get(""+utxo.tx_hash_big_endian+"?cors=true&format=hex");
urlTxes =
hash: utxo.tx_hash_big_endian,
index: utxo.tx_output_n,
nonWitnessUtxo: Buffer.from(urlTxes, 'hex')
if (count === utxosCount){
function transaction(privateKey, toPublicKey, fromPublicKey, amount){
return new Promise((resolve, reject) => {
let amtSatoshi = Math.floor(amount * BITCOIN_SAT_MULT);
let bitcoinNetwork = bitcoin.networks.bitcoin;
axios.get("" + fromPublicKey).then(async resp=> {
try {
const utxos =
const key = bitcoin.ECPair.fromWIF(privateKey)
let tx = new bitcoin.Psbt(bitcoinNetwork);
let availableSat = 0;
utxos.forEach(input => {
availableSat += input.value;

let tx_inputs = await getInputs(utxos);
tx_inputs.forEach(input => {

address: toPublicKey,
value: amtSatoshi

address: fromPublicKey,
value: availableSat - amtSatoshi - BITCOIN_FEE

tx.signInput(0, key)

if (availableSat < amtSatoshi + BITCOIN_FEE) {
reject({ data: "You do not have enough satoshis available to send this transaction."})
}'', { tx: tx.extractTransaction().toHex() }).then( resp => {
resolve({ msg: "Transaction submited", txId: tx.extractTransaction().toHex()})
}).catch( err => {
reject({ data:});
} catch (err) {
reject({ data: "Error - (87)" });
}).catch(err=> {
reject({ data:})

module.exports = { transaction };


We generate a simple transaction with this test file.

const bitcoinTxs = require("./bitcoinTransaction.js")bitcoinTxs.transaction("MY PRIVATE KEY", "TO ADDRESS", "MY ADDRESS", "MY AMOUNT TO SEND").then(res => {
}).catch(err=> {

and now we can test it

$ node testTransaction.js

Remember if you have any questions or inquiry tell me, I would be very glad to help you.

Thank you for seeing my post! I hope I’ve been helpful 😁


    Ivan Cabral

    Written by

    I‘m a daily coder FullStack developer with more than 10 years of experience, creator of gambling web sites, writer for JavaScript and Cryptocurrencies

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade