L402 Middleware is a Ruby gem that seamlessly integrates with your Rails application, enabling L402 protocol functionality. Build microservices, APIs, or payment gateways with pay-per-request authentication using the Lightning Network — effortlessly.
- Plug-and-Play: Integrate with Rails with just a single line of configuration.
- Lightning Network Ready: Enforces payment and authentication standards based on L402.
- Highly Configurable: Customize request handling, payments, and caveats.
- Lightweight & Fast: Minimal dependencies for optimal performance.
Add the gem to your application's Gemfile
:
gem 'l402_middleware'
Then install:
bundle install
- Add the middleware to your Rails stack in
application.rb
:
# config/application.rb
config.middleware.use L402Middleware, config.l402_middleware
- Define configuration options:
config.l402_middleware = {
network_type: :lnd,
root_key: 'your_root_key',
caveats: [],
lnd: {
address: '<lnd-node-address>',
tls_certificate_path: '<tls-cert-path>',
macaroon_path: '<macaroon_path>'
},
invoice: {
millisatoshis: 100,
description: 'Payment required to access the API',
payable: 'once' # or 'indefinitely'
},
endpoints: ['/protected'] # Endpoints to protect with L402 paywall
}
Once configured, the middleware will:
- Inspect incoming HTTP headers for L402-compliant payment tokens.
- Validate the token with the configured validator.
- Reject requests that don’t meet the payment threshold with HTTP 402 Payment Required.
- Client sends a request:
GET /protected-resource HTTP/1.1
Host: example.com
Authorization: L402 <token>
-
Middleware validates the token and processes payment.
-
Rails app continues processing if the payment is successful.
Clone the repository:
git clone https://github.com/rits1272/l402_middleware.git
cd l402_middleware
Run tests:
bundle exec rspec
If you have questions, feedback, or need support:
- Email: ritikjain1272@gmail.com
- Twitter: Ritik Jain
Bring the power of the Lightning Network to your Rails apps in minutes. Happy coding! ⚡