Skip to content

JorgenVatle/blockchain-payments-node

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Blockchain Payments API for Node.js

A simple wrapper over Blockchain.com's Payments API. Create and receive Bitcoin addresses and payment notification webhooks in seconds!

Downloads Version Node Version

Installation

npm install blockchain-payments-node

Prerequisites

  • A Blockchain.com API Key (request one here).
  • A Bitcoin wallet where you have access to its xPub key.
    • You can grab one from your existing wallet if it has support for it. See this article for instructions for the more popular wallets out there.
    • You can also obtain it directly from Blockchain.com by visiting your wallet settings. (Settings -> Addresses -> Manage -> More Options -> Show xPub)

Usage

Setup

New up an instance of the Blockchain API wrapper.

import BlockchainPayments from 'blockchain-payments-node';

const Blockchain = new BlockchainPayments({
    xpub: 'xpub6DQC9f...',        // See above for instructions on creating one
    apiKey: '5b1ae17...',         // Your blockchain.com API key
    webhookSecret: 'supersecret', // A secret code of your choice to validate incoming webhooks
})

Create payment address

Simply supply a webhook URI to be notified on. Once a payment is received for the returned address, you'll promptly receive an HTTP request on the specified URL.

Blockchain.createAddress({
    webhookUrl: 'https://example.com/ipn/btc' // URL to receive payment notification requests on.
})

Watch any Bitcoin address for payments

Blockchain's API isn't limited to just watching addresses you've created. You can watch any Bitcoin address for incoming and/or outgoing payments.

Blockchain.watchAddress({
    address: '3HoDXkm5iY...',                  // Bitcoin address to watch.
    webhookUrl: 'https://example.com/ipn/btc', // URL to receive payment notification requests on.

    // Optional parameters:
    confirmations: 1,       // Number of confirmations to wait for before a notification is sent to your webhook URL. (default: 1)
    onNotification: 'KEEP', // (DELETE, KEEP) Whether to keep sending webhooks to the specified URL once requested confirmations has been reached. (Default: 'Keep')
    type: 'ALL'             // (ALL, RECEIVE, SPEND) The type of action to receive notifications for. (default: 'ALL') Useful if you only want to track incoming payments.
})

Stop watching an address

Use the ID returned from the watchAddress() method to stop watching the address.

const watcher = await Blockchain.watchAddress({
    address: '3HoDXkm5iY...',
    webhookUrl: 'https://example.com/ipn/btc',
});

await Blockchain.stopWatch(watcher.id); // { deleted: true/false }

Notes

While this wrapper takes care of most for you, it is still encouraged that you go over the API docs as there are some caveats with address generation. Particularly in instances where one creates more than 20 unused addresses - this will result in an error until one of the 20 addresses receive at least one transaction.

License

This repository is licensed under the ISC license.

Copyright (c) 2019, Jørgen Vatle.

About

🎫 Node.js wrapper for Blockchain.com's Payments API

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published