paypercall
Charge for HTTP APIs on a pay-per-call basis with Bitcoin and Lightning. Available as an express middleware and as a reverse proxy.
Powered by ⚡️ Lightning Charge.
Install
$ npm install paypercall
Server Setup
As a middleware
paypercall
can be used as an express middleware to charge payments directly in your node.js apps.
Below is an example app that charges 0.1 USD to send out tweets:
app.js
const pay = chargeUrl: ... chargeToken: ... twt = consumer_key: ... consumer_secret: ... ... app = app app app
See ifpaytt
for a more full-fledged application using paypercall
as a middleware.
As a reverse proxy
Alternatively, you can develop your HTTP server with no payments awareness
and use paypercall
as a reverse proxy to handle payments.
An example with a python app:
app.py
from flask import Flask, requestimport twitter app = twt = return
Run the python app and the paypercall
proxy:
$ FLASK_APP=app.py flask run* Running on http://localhost:4001/ $ paypercall --charge-token mySecretToken --upstream-url http://localhost:4001 \ --port 4000 --rates-yaml '{ POST /tweet: 0.1 USD }'HTTP reverse proxy running on http://localhost:4000, proxying to http://localhost:4001
You will now have the python app running on port 4001 (providing API calls free of charge)
and the paypercall
reverse proxy running on port 4000 (charging on a per-call basis).
Paying for API calls
-
Send an empty request (no body) to the resource to get the BOLT11 payment request and the
X-Token
header:$ curl -i -X POST http://localhost:4000/tweetHTTP/1.1 402 Payment RequiredContent-Type: application/vnd.lightning.bolt11X-Token: lmbdmJeoSQ0ZCB5egtnph.af1eupleFBVuhN2vrbRuDLTlsnnUPYRzDWdL5HtWykYlnbcrt8925560p1pdfh7n2pp54g5avyupe70l988h30u0hy8agpj2z7qsveu7ejhys97j98rgez0... -
Make the payment:
$ lightning-cli pay lnbcrt8925560p1pdfh7n2pp54g5avyupe70l988h30u0hy8agpj2z7qsveu7ejhys97j98rgez0... -
Send the request again, this time with the request body and with the
X-Token
header echoed back:$ curl -i -X POST http://localhost:4000/tweet \-H 'X-Token: lmbdmJeoSQ0ZCB5egtnph.af1eupleFBVuhN2vrbRuDLTlsnnUPYRzDWdL5HtWykY' \-d message='I got lightning working and all I got was this tweet'HTTP/1.1 200 OKContent-Type: application/json{"id_str":"123123123","text":"I got lightning working...",...}
Documentation
Middleware
const pay = options
Returns a new payment middleware factory. options
can contain the following fields:
chargeUrl
: Lightning Charge server URL (optional, defaults tohttp://localhost:9112
)chargeToken
: Lightning Charge access token (required)dbPath
: Path to sqlite database (optional, defaults to./paypercall.db
)currency
: Default currency if none is specified (optional, defaults toBTC
)secret
: Secret key used for HMAC tokens (optional, generated based onchargeToken
by default)invoiceExp
: How long should invoices be payable for (optional, defaults to 1 hour)accessExp
: How long should paid access tokens remain valid for (optional, defaults to 1 hour)
const payware =
Returns an express middleware that requires a payment of amount
units of currency
(or the default currency if none provided) before letting requests pass through.
Can be used as following:
const pay = chargeToken: 'myToken' currency: 'EUR' app = // charge requests to a specific routeapp // charge all requests to /paid-apis/*app // dynamic pricing (should only be based on the method and path)app
Reverse proxy
$ paypercall --help Charge
License
MIT