[BREAKGLASS] BTCPay NodeJS client
|
|
||
|---|---|---|
| .gitignore | ||
| client.js | ||
| cryptography.js | ||
| index.js | ||
| LICENSE | ||
| package.json | ||
| README.md | ||
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))