Skip to main content

Overview

Testing an L402 API has two layers:
  1. Unit / integration tests — verify your middleware logic without real Lightning payments
  2. End-to-end tests — verify the full flow with a real wallet and real sats

Unit tests — mock the provider

Pass a mock LightningProvider 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:
Guard your provider selection by environment:

End-to-end test with real sats

For a full payment flow test (staging/pre-launch):
  1. Set priceSats: 1 — costs ~$0.0008 per test run
  2. Use OpenNode sandbox (testMode: true) to pay without real money:
  3. Or use your Blink wallet — 1 sat payments are practically free

Automated E2E with a test wallet


Checklist before production

1

Unit tests pass with mock provider

402 → pay → 200 flow verified. Replay protection verified (second use returns 401).
2

Token expiry tested

Set exp: Date.now() - 1 in your mock macaroon — verify the middleware returns 401.
3

Full E2E with real payment at priceSats: 1

Real wallet, real payment, real 200 OK. Use Wallet of Satoshi or Blink on your phone.
4

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.