JavaScript client for lightning-charge
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib Update transpiled lib Aug 11, 2018
src info() method Aug 11, 2018
test Add test for info() method. Refs #4. Aug 11, 2018
.babelrc initial commit, working PoC and some basic tests Nov 14, 2017
.gitignore initial commit, working PoC and some basic tests Nov 14, 2017
.npmignore initial commit, working PoC and some basic tests Nov 14, 2017
README.md Update README Jan 14, 2018
package-lock.json
package.json 0.1.10 Oct 30, 2018

README.md

lightning-charge-client-js

JavaScript client for the Lightning Charge REST API.

Install

$ npm install lightning-charge-client

Use

// Initialize the client
import ChargeClient from 'lightning-charge-client'
const charge = new ChargeClient('http://localhost:9112', '[API-TOKEN]')

// new is optional
const charge = require('lightning-charge-client')('http://localhost:9112', '[API-TOKEN]')

// Create invoice
const inv = await charge.invoice({ msatoshi: 50, metadata: { customer_id: 123, product_id: 456 } })

console.log(`invoice ${ inv.id } created with rhash=${ inv.rhash }, payreq=${ inv.payreq }`)

// Create invoice denominated in USD
const inv = await charge.invoice({ currency: 'USD', amount: 0.15 })

// Fetch invoice
const invoice = await charge.fetch('m51vlVWuIKGumTLbJ1RPb')

// Fetch all invoices
const invoices = await charge.fetchAll()

// Long poll payment updates for a specific invoice
do {
  const paid = await charge.wait(inv.id, /* timeout: */ 600 /* seconds */)

  if (paid) console.log(`invoice ${ paid.id } of ${ paid.msatoshi } paid, updated invoice:`, paid)
  else if (paid === false) console.log('invoice expired and can no longer be paid')
  else if (paid === null) console.log('timeout reached without payment, invoice is still payable')
} while (paid === null)

// Stream all incoming payments
const stream = charge.stream()
stream.on('payment', inv => console.log(`invoice ${ inv.id } of ${ inv.msatoshi } paid`))

See Lightning Charge's documentation for more information and a full list of invoice fields.

Test

$ CHARGE_URL=http://api-token:ACCESS-TOKEN@localhost:8009 npm test

License

MIT