Overview
Testing an L402 API has two layers:- Unit / integration tests — verify your middleware logic without real Lightning payments
- End-to-end tests — verify the full flow with a real wallet and real sats
Unit tests — mock the provider
Pass a mockLightningProvider to bypass Lightning entirely:
Generating a valid preimage for tests
Python — pytest with mock provider
Testing replay protection
Verify that a preimage cannot be reused:CI pipeline
Use the mock provider in CI — no Lightning node or API key needed:End-to-end test with real sats
For a full payment flow test (staging/pre-launch):- Set
priceSats: 1— costs ~$0.0008 per test run - Use OpenNode sandbox (
testMode: true) to pay without real money: - Or use your Blink wallet — 1 sat payments are practically free
Automated E2E with a test wallet
Checklist before production
Unit tests pass with mock provider
402 → pay → 200 flow verified. Replay protection verified (second use returns 401).
Token expiry tested
Set
exp: Date.now() - 1 in your mock macaroon — verify the middleware returns 401.Full E2E with real payment at priceSats: 1
Real wallet, real payment, real 200 OK. Use Wallet of Satoshi or Blink on your phone.
Replay protection correct for your deployment
Single process: default in-memory adapter is fine. Multi-process (Kubernetes, PM2 cluster): use Supabase or Redis adapter. See Production Guide.