[BREAKGLASS] BTCPay NodeJS client
Go to file
Nicolas Dorier 1ebc542be6
Merge pull request #32 from johnbailon/update_readme
Docs: Fix instructions on generating private key
2019-10-06 23:20:11 +09:00
.gitignore initial version 2018-04-16 18:05:21 +00:00
client.js fix get_rates (passing the store_id should not be needed as the call is authenticated via bitid) 2018-05-06 22:42:37 +09:00
cryptography.js initial version 2018-04-16 18:05:21 +00:00
index.js initial version 2018-04-16 18:05:21 +00:00
LICENSE initial version 2018-04-16 18:05:21 +00:00
package.json initial version 2018-04-16 18:05:21 +00:00
README.md Docs: Fix instructions on generating private key 2019-10-06 22:12:04 +08:00

node-btcpay

Install

npm install https://github.com/tanjalo/node-btcpay

Private key generation

  • Generate and save private key:
const btcpay = require('btcpay')
const privatekey = btcpay.crypto.generate_keypair().getPrivate('hex')

console.log(`PRIVATEKEY: ${privatekey}`)

Store the value of "priv" in a save place, e.g. environment variables

Pairing

After generating your private key, you have to pair your client with your BTCPay store:

  • On BTCPay Server > Stores > Settings > Access Tokens > Create a new token, (leave PublicKey blank) > Request pairing
  • Copy pairing code:
  • Pair client to server and save returned token:
const btcpay = require('btcpay')
const keypair = btcpay.crypto.load_keypair(new Buffer.from(<PRIVATEKEY>, 'hex'))
const client = new btcpay.BTCPayClient(<BTCPAYURL>, btcpay.crypto.load_keypair(Buffer.from(<PRIVATEKEY>, 'hex')))

// Pair client to server
client
  .pair_client(<PAIRINGCODE>)
  .then(res => console.log(res))
  .catch(err => console.log(err))

>>> { merchant: 'XXXXXX' }

Store the value of "merchant" in a save place, e.g. environment variables

Recreating a client

After pairing your client to the store, you can recreate the client as needed and use it in your code

const btcpay = require('btcpay')
const keypair = btcpay.crypto.load_keypair(new Buffer.from(<PRIVATEKEY>, 'hex'))

// Recreate client
const client = new btcpay.BTCPayClient(<BCTPAYURL>, keypair, {merchant: <MERCHANT>})

Get rates

Fetches current rates from BitcoinAverage (using your BTCPayServer)

client.get_rates('BTC_USD')
  .then(rates => console.log(rates))
  .catch(err => console.log(err))

Create invoice

See BitPay Invoice API documentation

client.create_invoice({price: 20, currency: 'USD'})
  .then(invoice => console.log(invoice.url))
  .catch(err => console.log(err))

Get invoice

client.get_invoice(<invoice-id>)
  .then(invoice => console.log(invoice.status))
  .catch(err => console.log(err))