[BREAKGLASS] BTCPay NodeJS client
Go to file
Jonathan Underwood 095114ede8 Add git repo (#18)
* Add Github link to package.json

* 0.2.2
2019-05-20 15:01:50 +00:00
src Fix TS (#15) 2019-05-17 02:59:33 +00:00
tests Fix TS (#15) 2019-05-17 02:59:33 +00:00
.gitignore Typescript (#13) 2019-05-16 00:13:24 +00:00
LICENSE initial version 2018-04-16 18:05:21 +00:00
package-lock.json Add git repo (#18) 2019-05-20 15:01:50 +00:00
package.json Add git repo (#18) 2019-05-20 15:01:50 +00:00
README.md fix documentation (#17) 2019-05-17 03:45:42 +00:00
tsconfig.json Fix TS (#15) 2019-05-17 02:59:33 +00:00
tslint.json Fix TS (#15) 2019-05-17 02:59:33 +00:00

node-btcpay

Install

npm i btcpay

Private key generation

  • Generate and save private key:
$ node -p "require('btcpay').crypto.generate_keypair()"

>>> <Key priv: XXXXXXX pub: null >

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:
# Replace the BTCPAY_XXX envirnoment variables with your values and run:

$ [space] BTCPAY_URL=https://mydomain.com/ BTCPAY_KEY=... BTCPAY_PAIRCODE=... node -e "const btcpay=require('btcpay'); new btcpay.BTCPayClient(process.env.BTCPAY_URL, btcpay.crypto.load_keypair(Buffer.from(process.env.BTCPAY_KEY, 'hex'))).pair_client(process.env.BTCPAY_PAIRCODE).then(console.log).catch(console.error)"

# (prepend the line with a space to prevent BTCPAY_KEY from being saved to your bash history)

>>> { 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', <STOREID>)
  .then(rates => console.log(rates))
  .catch(err => console.log(err))

The first argument accepts a comma-separated list of currency pair.

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))