Skip to main content

Installation

Requirements: Python 3.11+, FastAPI or Flask (optional)

Soberano mode (you keep 100%)

Bring your own Lightning provider — payments go directly to your wallet, 0% fees.
Python also supports Managed mode — use ManagedProvider.from_address("you@blink.sv") (0.3% fee, no node needed). See the Providers section below.

Flask

Flask + Gunicorn with gevent/eventlet workers: l402_required calls the async Lightning provider from a sync Flask handler. If your Gunicorn worker uses gevent or eventlet monkey-patching (which creates a running event loop), the decorator detects this automatically and runs the async call in a dedicated thread — no action needed. Standard Gunicorn sync workers and uvicorn (FastAPI) are unaffected.

@l402_required — decorator

Parameters

Behavior

402 response


Providers

BlinkProvider

Blink — free custodial Lightning wallet, no KYC for small amounts.

LNbitsProvider

OpenNodeProvider

ManagedProvider — cloud mode (0.3% fee)

l402kit.com hosts the Lightning node. You receive 99.7% of each payment — no node setup required.

Replay protection

Default — in-memory (development)

Built-in, no configuration needed. Resets on process restart.

Redis (production — multi-instance)

For Gunicorn/uvicorn multi-worker deployments, share replay state via Redis:
RedisReplayAdapter uses SET key 1 NX EX ttl — atomic and race-condition free.

Standalone utilities


Custom provider


Testing


Running


L402Client — automatic payment

L402Client wraps httpx and handles the full 402 → pay → retry loop automatically.

Wallets

build_wallet(env=None) — auto-detect

Picks the wallet from environment variables. Priority: BLINK_API_KEY + BLINK_WALLET_ID → Blink, else ALBY_TOKEN → Alby. Reads os.environ by default.
Raises ValueError if no credentials are configured.

LAW-N adapter — behavioral telemetry (1.10.0+)

Forward L402 payment events to a LAW-N ingest endpoint. Same contract as TS/Rust/Go: POST JSON + HMAC-SHA256 in X-LAW-N-Signature + random X-LAW-N-Request-Id + fire-and-forget. Network errors are swallowed so behavioral writes never block payments.

AsyncL402Client — async/await

AsyncL402Client uses httpx.AsyncClient internally — ideal for FastAPI, asyncio, and AI agent frameworks that run in an async event loop.

Difference from L402Client


DevProvider + DevWallet — local development

Zero-config local development — no Lightning node, no real payments. Cryptographically identical to production: SHA256(preimage) === paymentHash.