Skip to content

pfergi42/l402-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

L402 Client Examples

Working code examples for consuming L402 APIs using the Lightning Network.

These examples use the public L402 endpoints at Lightning Faucet as the demo server.

What is L402?

L402 is an API monetization protocol that combines HTTP 402 (Payment Required) status codes with Lightning Network micropayments. It allows API providers to charge per-request using instant Bitcoin payments.

The protocol works like this:

  1. Request -- The client makes a request to a protected API endpoint.
  2. 402 Response -- The server returns HTTP 402 with a WWW-Authenticate header containing a macaroon (credential) and a Lightning invoice (payment request).
  3. Pay -- The client pays the Lightning invoice using any Lightning wallet. Payment returns a preimage (proof of payment).
  4. Retry with proof -- The client retries the original request with an Authorization: L402 <macaroon>:<preimage> header.
  5. Access granted -- The server validates the macaroon and preimage, then returns the requested resource.
Client                              Server
  |                                    |
  |  GET /api/l402/joke                |
  | ---------------------------------> |
  |                                    |
  |  402 Payment Required              |
  |  WWW-Authenticate: L402            |
  |    macaroon="<base64>"             |
  |    invoice="<bolt11>"              |
  | <--------------------------------- |
  |                                    |
  |  [Pay invoice via Lightning]       |
  |  [Receive preimage]               |
  |                                    |
  |  GET /api/l402/joke                |
  |  Authorization: L402               |
  |    <macaroon>:<preimage>           |
  | ---------------------------------> |
  |                                    |
  |  200 OK                            |
  |  { "joke": "..." }                |
  | <--------------------------------- |

Available Demo Endpoints

These public L402 endpoints are provided by Lightning Faucet for testing and development:

Endpoint Price Description
https://lightningfaucet.com/api/l402/joke 50 sats Returns a random joke
https://lightningfaucet.com/api/l402/fortune 50 sats Returns a fortune
https://lightningfaucet.com/api/l402/quote 10 sats Returns an inspirational quote

Browse the full catalog at the L402 API Registry.

Examples

Python

cd python
pip install -r requirements.txt
python l402_client.py

See python/l402_client.py for the full example.

Node.js

Requires Node.js 18+ (uses built-in fetch).

cd node
npm install
node l402_client.js

See node/l402_client.js for the full example.

Integrating L402 Payment

The examples above demonstrate the full L402 handshake but use a placeholder for the Lightning payment step. To actually pay invoices programmatically, you need a Lightning wallet with an API. Common options:

  • LND -- Use lncli payinvoice or the gRPC/REST API (docs)
  • CLN (Core Lightning) -- Use lightning-cli pay or the JSON-RPC API
  • LDK -- Embed a Lightning node in your application (docs)
  • Alby -- Browser extension and API for Lightning payments (getalby.com)
  • Lightning Wallet MCP Server -- An AI-agent-friendly wallet for automated L402 payments:
    npm install -g lightning-wallet-mcp
    See the lightning-wallet-mcp package for setup instructions.

How Macaroons Work in L402

The macaroon in L402 is a bearer credential that encodes:

  • Identifier -- A unique token ID
  • Location -- The issuing server (e.g., lightningfaucet.com)
  • Caveats -- Restrictions such as:
    • payment_hash -- Links the macaroon to a specific Lightning invoice
    • expiry -- When the credential expires
    • service -- Which API service the macaroon grants access to
    • capabilities -- What operations are allowed
  • Signature -- HMAC proof that the server issued this macaroon

The preimage (obtained by paying the invoice) serves as proof of payment. The server verifies that the preimage hashes to the payment_hash caveat in the macaroon, confirming the client paid.

Resources

License

MIT

About

Code examples for consuming L402 APIs with Lightning payments

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors