Connecting A Bitcoin Dedicated Node Via Node.js


This tutorial shows a simple Node.js application that prints each new transaction as well as the total value of all the transactions outputs.

The following text assumes a basic working knowledge of Node.js and npm.

Install Node.js libraries

Install Node.js & NPM, downloads for your operating system can be found at
Install the Node.js library 'bitcoind-zmq'. We will use this library to listen to the bitcoin events:

mkdir zmq-project && cd zmq-project
npm install bitcoind-zmq

The above command will initialize a Node.js project and create a package.json file with the dependency

Copy script the following Node.js program into a file called index.denjs.

Update all “tcp://” to your <node endpoint>  from the node dashboard action menu>connect page.
(Example: line 3, hashtx: 'tcp://, becomes hashtx: ', )

const BitcoindZmq = require('bitcoind-zmq')
const btcd = new BitcoindZmq({
hashtx: 'tcp://',
rawblock: 'tcp://',
hashblock: 'tcp://',
rawtx: 'tcp://'


btcd.on('hashblock', (hash) => {
console.log('got block hash:', hash) // hash <Buffer ... />

btcd.on('hashtx', (hash) => {
console.log('got tx hash:', hash) // hash <Buffer ... />

btcd.on('rawblock', (block) => {
console.log('got raw block:', block) // block <Buffer ... />

btcd.on('rawtx', (tx) => {
console.log('got raw tx:', tx) // tx <Buffer ... />

btcd.on('connect:*', (uri, type) => {
console.log(`socket ${type} connected to ${uri}`)

btcd.on('error:*', (err, type) => {
console.error(`${type} had error:`, err)

btcd.on('retry:*', (type, attempt) => {
console.log(`type: ${type}, retry attempt: ${attempt}`)

btcd.on('close:*', (err, type) => {
console.log(`close ${type}`, err || '')

Start the Node.js script, to connect and subscribe to Bitcoin ZMQ events

node index.js

Further Reading

Bitcoin RPC Protocol Documentation

Bitcoind-zmq Documentation

0 out of 0 found this helpful



Please sign in to leave a comment.