> ## Documentation Index
> Fetch the complete documentation index at: https://shinydapps-bd9fa40b.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Introduction

> Charge for your API in Bitcoin Lightning. 3 lines of code. TypeScript, Python, Go, Rust. No account. No bank. No permission.

## What is l402-kit?

**l402-kit** is an open-source middleware that implements the [L402 protocol](https://docs.lightning.engineering/the-lightning-network/l402) � the open standard for pay-per-call API monetization using the Bitcoin Lightning Network.

Add 3 lines of code. Callers pay in sats. You receive in seconds. Works with TypeScript, Python, Go, and Rust.

```bash theme={null}
npm install l402-kit    # TypeScript / Express
pip install l402kit     # Python / FastAPI / Flask
cargo add l402kit       # Rust / axum
```

```bash theme={null}
# Go � see SDK / Go for setup
go get github.com/shinydapps/l402-kit/go@v1.10.0
```

> [card] [? Watch end-to-end demo](https://l402kit.com/#live-demo)
>
> See the full L402 flow in action � from `npm install` to first paid API call. Interactive terminal animation, 45 seconds.

> [card] [npm](https://npmjs.com/package/l402-kit)
>
> l402-kit on npm

> [card] [PyPI](https://pypi.org/project/l402kit)
>
> l402kit on PyPI

> [card] [Go](https://pkg.go.dev/github.com/shinydapps/l402-kit/go)
>
> Go SDK (net/http)

> [card] [Rust](https://crates.io/crates/l402kit)
>
> Rust SDK (axum)

> [card] [llms.txt](https://l402kit.com/llms.txt)
>
> For AI agents

> [card] [Whitepaper](https://l402kit.com/docs/whitepaper)
>
> Technical design

***

## Lightning Network — 30-second primer

> [!NOTE]
>
> **New to Bitcoin Lightning?** Here's what you need to know:
>
> * **Lightning Network** is a fast payment layer on top of Bitcoin. Payments settle in under a second, anywhere in the world, for fractions of a cent.
> * **A Lightning Address** (like `you@blink.sv`) is your receive address — it looks like an email. You don't need to run a node. Wallets like [Blink](https://dashboard.blink.sv), [Alby](https://getalby.com), and [Phoenix](https://phoenix.acinq.co) give you one for free in 2 minutes.
> * **A sat** (satoshi) is the smallest unit of Bitcoin — roughly $0.0006 at $60k/BTC. A typical API call costs 1–100 sats.
> * **Managed vs Soberano** — "Managed" means l402kit.com hosts the Lightning node for you (0.3% fee, zero setup). "Soberano" (Spanish for *sovereign*) means you connect your own wallet (0% fee, \~5 min setup with Blink or Alby).

## How it works � full flow

The diagram below shows **managed mode** (`ManagedProvider`), where l402kit.com hosts the Lightning node for you. In **soberano mode** (Blink, Alby, BTCPay, etc.) payments go directly to your wallet � **0% fee**.

```
+-----------------------------------------------------------------+
�                                                                 �
�  Client / AI Agent                                              �
�         �                                                       �
�         � 1. GET /your-api                                      �
�         ?                                                       �
�  Your API (l402-kit)  --- 2. Creates invoice --? ShinyDapps API �
�         �                                        (Blink wallet) �
�         � 3. Returns: 402 + BOLT11 invoice                      �
�         ?                                                       �
�  Client pays -------------------------------? Lightning Network �
�         �                       (< 1 second)                    �
�         � 4. Sends preimage as cryptographic proof              �
�         ?                                                       �
�  Your API verifies: SHA256(preimage) == hash ?                  �
�         �                                                       �
�         � 5. Automatic split:                                   �
�         �    ? 99.7% -----------------------? Your Lightning Addr�
�         �    ? 0.3%  -----------------------? ShinyDapps (fee)  �
�         �                                                       �
�         � 6. Your API responds: 200 OK + data                   �
�         ?                                                       �
�  Client receives content                                        �
+-----------------------------------------------------------------+
```

> [!NOTE]
>
> **Full transparency:** ShinyDapps receives the payment and forwards **99.7%** to your Lightning Address automatically. The **0.3%** fee keeps the project alive. The split is deterministic and verifiable � no secrets. For zero-intermediary operation, use soberano mode (Blink, BTCPay, Alby).

***

## Quickstart in 2 minutes

### TypeScript

```typescript theme={null}
import express from "express";
import { l402, ManagedProvider } from "l402-kit";

const app = express();

app.get("/premium", l402({
  priceSats: 100,
  lightning: ManagedProvider.fromAddress("you@blink.sv"), // managed mode � no node needed
}), (_req, res) => {
  res.json({ data: "Payment confirmed." });
});

app.listen(3000);
```

### Python

```python theme={null}
from fastapi import FastAPI, Request
from l402kit import l402_required

app = FastAPI()

@app.get("/premium")
@l402_required(
    price_sats=100,
    lightning=ManagedProvider.from_address("you@blink.sv"),
)
async def premium(request: Request):
    return {"data": "Payment confirmed."}
```

### Go

```go theme={null}
package main

import (
    "fmt"
    "net/http"
    l402kit "github.com/shinydapps/l402-kit/go"
)

func main() {
    http.Handle("/premium", l402kit.Middleware(l402kit.Options{
        PriceSats: 100,
        Lightning: l402kit.NewManagedProvider("you@yourdomain.com"),
    }, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintln(w, `{"data":"Payment confirmed."}`)
    })))
    http.ListenAndServe(":8080", nil)
}
```

### Rust (axum)

```rust theme={null}
use axum::{middleware, routing::get, Router};
use l402kit::{l402_middleware, Options, ManagedProvider};
use std::sync::Arc;

#[tokio::main]
async fn main() {
    let provider = ManagedProvider::new("you@yourdomain.com".into());
    let opts = Arc::new(Options::new(100, provider));

    let app = Router::new()
        .route("/premium", get(|| async { r#"{"data":"Payment confirmed."}"# }))
        .route_layer(middleware::from_fn_with_state(opts, l402_middleware));

    let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}
```

***

## Why not Stripe?

| | Stripe | **l402-kit** |
| - | - | - |
| Minimum fee | **\$0.30** | **1 sat (\~\$0.0006)** |
| Settlement | 2–7 days | **\< 1 second** |
| Chargebacks | Yes | **Impossible** |
| Requires account | Yes | **No** |
| AI agent support | No | **Yes — native** |
| Countries blocked | \~50 | **0 — global** |
| Open source | No | **Yes — MIT** |
| Censurable | Yes | **No** |

***

## Security

Every payment is verified mathematically:

```
SHA256(preimage) == paymentHash
```

* Impossible to fake without actually paying
* Each preimage works exactly once (anti-replay)
* Tokens expire after 1 hour
* All logic is deterministic and verifiable � no hidden fees, no intermediaries
* **600+ automated tests** across 5 runtimes � production-grade reliability for autonomous agent workflows

***

## Who is it for?

> [card] API developers
>
> Charge per call instead of monthly subscriptions. Start in 1 line.

> [card] AI agent builders
>
> Agents pay APIs autonomously � no credit card, no human in the loop.

> [card] Devs outside the financial system
>
> Argentina, Nigeria, Iran � Bitcoin has no borders or gatekeepers.

> [card] Data providers
>
> Charge 1 sat per query. Impossible with Stripe.

***

## Created by

**ShinyDapps** � [github.com/ShinyDapps](https://github.com/ShinyDapps)

Lightning: `shinydapps@blink.sv`


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.