const request = require('request');
const http = require('http');
// Define our custom values
var my_address = "1LisLsZd3bx8U1NYzpNHqpo8Q6UCXKMJ4z";
var my_address_type = "bech32"; // bech32 | legacy | p2sh-segwit
var my_secret = "H78SY8DIJIDAID8JHD";
var my_callback = "http://example.com/callback?invoice_id=1234&secret=" + my_secret;
// BlockchainAPI endpoint
let endpoint = 'https://blockchainapi.org/api/btc?method=create&address=' + my_address + "&address_type=" + my_address_type + "&callback=" + my_callback;
let options = {json: true};
// Define server variables
var hostname = "localhost";
var estimated_transaction_fee = "";
request(endpoint, options, (error, res, body) => {
return console.log(error)
if (!error && res.statusCode == 200) {
// do something with JSON, using the 'body' variable
var response = JSON.parse(JSON.stringify(body));
// Good, we managed to create an address
console.log('Good, we managed to get a success response');
// Now dig into the values
input_address = response.success.input_address;
destination = response.success.destination;
fee_percentage = response.success.fee_percent;
callback_url = response.success.callback_url;
estimated_transaction_fee = response.success.estimated_transaction_fee;
miners_fee = response.success.miners_fee;
tor = response.success.tor;
setting = response.success.setting;
// Failed to get success response
console.log('Bad, failed to get success response');
// Initialise the server on port 8181
const server = http.createServer((req, res) => {
res.setHeader('Content-type', 'text/html');
// Show the "Please send the payment to..."
if(input_address !== null) {
res.write('<p>Please send the payment to the following Bitcoin address: ' + input_address + '</p>');
res.write('<p>Failed to create a new address to pay to</p>');
res.end('Page fully loaded');
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);