You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bumi f8355cf89e
3 years ago
bin hello world 4 years ago
lib Use lnrpc gem as dependency 3 years ago
spec hello world 4 years ago
.gitignore hello world 4 years ago
.rspec hello world 4 years ago
.travis.yml hello world 4 years ago
Gemfile hello world 4 years ago
LICENSE.txt hello world 4 years ago Update 3 years ago
Rakefile hello world 4 years ago
example.rb Add support to path macaraoon hex directly 4 years ago
faraday_ln_paywall.gemspec Use lnrpc gem as dependency 3 years ago

Faraday middleware to send lightning payments

This is a Faraday middleware that handles payment requests by the server and sends Bitcoin lightning payments.

How does it work?

This Faraday middleware checks if the server responds with a 402 Payment Required HTTP status code and a lightning invoice (BOLT11). If so it pays the invoice through the connected lnd node and performs a second request with the proof of payment.

How does the server side look like?

Have a look at the rack middleware rack-lightning for ruby or at @philippgille's middleware for Go.


The middleware uses the gRPC service provided by the Lightning Network Daemon(lnd). A running node with funded channels is required. Details about lnd can be found on their github page


Add this line to your application's Gemfile:

gem 'faraday_ln_paywall'


Simply use the FaradayLnPaywall::Middleware in your Faraday connection:

conn = => '') do |faraday|
  faraday.use FaradayLnPaywall::Middleware, { max_amount: 100 }
  faraday.adapter  Faraday.default_adapter
puts conn.get("/translate?text=Danke&to=en").body


The middleware accepts the following configuration options:

  • max_amount: the maximum amount of an invoice that will automatically be paid. Raises a FaradayLnPaywall::PaymentError if the server request a higher amount
  • address: the address of the lnd gRPC service( default: localhost:10009)
  • credentials_path: path to the tls.cert (default: ~/.lnd/tls.cert)
  • macaroon_path: path to the macaroon path (default: ~/.lnd/data/chain/bitcoin/testnet/admin.macaroon)
  • credentials: instead of configuring a credentials_path you can pass the content of the tls.cert directly
  • macaroon: instead of configuring a macaroon_path you can pass the hex content of the macaroon directly you can get the macaroon content like this:
    xxd -p -c2000 admin.macaroon or:"/path/to/admin.macaroon")) { |b| b.to_s(16).rjust(2,'0') }.join

What is the Lightning Network?

The Lightning Network allows to send real near-instant microtransactions with extremely low fees. It is a second layer on top of the Bitcoin network (and other crypto currencies). Thanks to this properties it can be used to monetize APIs.


Bug reports and pull requests are welcome on GitHub at


  • tests!!


The gem is available as open source under the terms of the MIT License.