Using WebHooks

This sample starts a simple node.js logging web server and registers a WebHook for all unconfirmed transactions. This will print all the transactions on the Bitcoin network right as they are broadcasted in near-realtime.

Because the WebHook requires your web server to be reachable from the outside internet, make sure that your IP address in the sample is correct and that an open port is used.

var http = require("http");

// Setup the http server we'll get the callbacks on
var server = http.createServer(function(req, res) {
  console.log("Received request.");
  // just aggregates the request data
  var data = "";
  req.on('data', function(chunk) {
    data += chunk.toString();
  req.on('error', function(e) {
    console.log('error', e);

  // logs the JSON body
  req.on('end', function() {
    console.log("Transaction:\n", JSON.parse(data));

// listent on port 51413, the same one we'll use for the webhook
console.log("Server is listening");

// Prepare the request to create the webhook, filtering on new pool (unconfirmed) transactions
// CHANGE TO YOUR IP ADDRESS FOR TESTING and make sure it's reachable from the outside
var data = JSON.stringify({url: "", "event": "new-pool-tx"});

// Set up the request
var options = {
  host: '',
  port: '80',
  path: '/v1/btc/main/hooks',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': data.length

// Creates the webhook
var req = http.request(options, function(res) {
  if (res.statusCode != 201) {
    log.Println("Error when creating webhook:", res.statusCode)
  res.on('data', function (chunk) {
      console.log('Response: ' + chunk);

// Post the data

console.log("WebHook created, expecting callbacks...");