Giter Site home page Giter Site logo

elementsproject / lightning-charge-client-js Goto Github PK

View Code? Open in Web Editor NEW
64.0 14.0 17.0 249 KB

JavaScript client for lightning-charge

JavaScript 100.00%
bitcoin lightning lightning-charge lightning-charge-client nodejs javascript lightning-network

lightning-charge-client-js's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.