L402 API
L402 is pay-per-request API access over Lightning: the server answers 402 Payment Required with an invoice and a macaroon, the client pays, and it retries with proof of payment. No account or card is needed to pay.
Lightning Enable speaks two schemes on the same 402: classic L402 (macaroon + preimage) and the HTTP Payment scheme (MPP, Machine Payments Protocol). One invoice backs both, so a client uses whichever it implements. See Payment (MPP) credentials.
To sell access, either put your API behind an L402 proxy or mint challenges yourself with the Producer API.
L402 Protected Proxy
* /l402/proxy/{proxyId}/{path}
Public: the proxy needs no API key. Requests without a valid credential get a 402; requests with one are forwarded to your API.
Step 1: Request the resource
curl -i https://api.lightningenable.com/l402/proxy/my-api/data
Step 2: Receive the 402 challenge
HTTP/1.1 402 Payment Required
WWW-Authenticate: L402 macaroon="AgEL...", invoice="lnbc100n1p..."
WWW-Authenticate: Payment id="k9Q3...", realm="lightning-enable", method="lightning", intent="charge", ...
Link: </l402/proxy/my-api/.well-known/l402-manifest.json>; rel="l402-manifest"
Cache-Control: no-store
{
"error": "Payment Required",
"message": "Pay the Lightning invoice to access this API",
"proxy": { "id": "my-api", "name": "My API", "description": "Weather data" },
"l402": {
"macaroon": "AgEL...",
"invoice": "lnbc100n1p...",
"amount_sats": 10,
"payment_hash": "abc123...",
"expires_at": "2026-11-03T13:00:00.0000000+00:00"
},
"instructions": {
"step1": "Pay the Lightning invoice using any Lightning wallet",
"step2": "Copy the preimage (proof of payment) from your wallet",
"step3": "Include in request: Authorization: L402 <macaroon>:<preimage> (or Authorization: Payment method=\"lightning\", preimage=\"<preimage>\")"
}
}
The Link header appears when the proxy publishes a manifest.
Step 3: Pay the invoice
Pay with any Lightning wallet that returns the preimage (proof of payment). Strike, LND, Alby Hub, CoinOS and CLINK wallets return it; OpenNode and Primal don't.
Step 4: Retry with the credential
curl https://api.lightningenable.com/l402/proxy/my-api/data \
-H "Authorization: L402 AgEL...:7f8a9b2c..."
The proxy forwards the request to your API and returns its response.
When a credential is refused
A missing, malformed, expired, or mismatched credential never gets a 401 or 403 on the proxy. It gets a fresh 402 challenge, with the reason in the X-L402-Error header and in message:
HTTP/1.1 402 Payment Required
X-L402-Error: Preimage does not match payment hash
See Invalid L402 Credential for the common reasons. Other proxy errors (404 for an unknown proxy, 502/504 when your API can't be reached, 503 when the seller's payment provider can't issue an invoice) are in the Error Reference.
Credential Format
Authorization: L402 <macaroon>:<preimage>
| Component | Format | Description |
|---|---|---|
macaroon | Base64 | Bearer token with caveats |
preimage | Hex (64 characters) | 32-byte proof of payment |
LSAT is accepted as an alias for L402.
Token Reuse
A classic L402 token is reusable until its expires caveat (1 hour by default), but only for the path, account, and price it was issued for. Key cached credentials by the full path, not the host: a token for /l402/proxy/api-a/data is refused on /l402/proxy/api-b/data.
Macaroon Caveats
Every macaroon Lightning Enable issues carries these first-party caveats, and all must hold for the token to verify. An unrecognized caveat fails verification.
| Caveat | Example | Meaning |
|---|---|---|
services | services = lightning-enable:0 | Service identifier and tier |
path | path = /l402/proxy/my-api/data | The path the token was issued for. A trailing /* allows sub-paths |
merchant_id | merchant_id = 42 | The selling account. A token from one seller can't be replayed against another |
charge_id | charge_id = 8f6c3f5e-... | The payment provider's charge ID for the invoice |
amount_sats | amount_sats = 100 | The price paid. The endpoint's current price must match, so a cheap token can't open a pricier endpoint |
method | method = GET | On proxy tokens that carry it: the HTTP method that drew the challenge |
expires | expires = 1762174800 | Unix time after which the token is refused |
Verification checks that SHA256(preimage) equals the payment hash, then the macaroon signature, then each caveat against the request.
Payment (MPP) credentials
Alongside L402, every 402 also advertises the HTTP Payment authentication scheme, the format shared by draft-httpauth-payment-00 (the core scheme) and draft-lightning-charge-00 (the Lightning charge method). This is usually called MPP (Machine Payments Protocol). Three credential profiles are accepted, and all three are proofs of the same invoice:
| Profile | Authorization header | Reusable? | Best for |
|---|---|---|---|
| Classic L402 | L402 <macaroon>:<preimage> | Yes, until the macaroon expires | Existing L402 clients, LND lnget, Aperture-style tooling |
Legacy Payment (auth-params) | Payment method="lightning", preimage="<hex>" | Yes, until the challenge expires | Clients that only want to send a preimage |
Modern Payment (bearer token) | Payment <base64url(JSON)> | No. Single use | Clients built on the current drafts (mppx, l402-requests, the Lightning Enable MCP server v1.24+) |
You never opt in per merchant. The server emits both schemes for every L402-gated resource, and a client uses whichever one it implements.
The 402 challenge
A single 402 carries one WWW-Authenticate header per scheme. The Payment header is a superset: it holds the modern parameters and the legacy invoice / amount / currency parameters in the same value, and both drafts require clients to ignore parameters they don't know.
HTTP/1.1 402 Payment Required
WWW-Authenticate: L402 macaroon="AgEL...", invoice="lnbc1u1p..."
WWW-Authenticate: Payment id="k9Q3...", realm="lightning-enable", method="lightning", intent="charge", request="eyJhbW91bnQiOiIxMDAiLC...", expires="2026-09-11T18:30:00Z", invoice="lnbc1u1p...", amount="100", currency="sat"
Cache-Control: no-store
| Parameter | Meaning |
|---|---|
id | Server-computed binding over every other parameter. Verification recomputes it from the fields you echo back, so any edit to the challenge invalidates the credential. |
realm | Always lightning-enable on the hosted API. |
method / intent | Always lightning / charge. |
request | base64url of a JCS-canonical JSON object: {"amount":"100","currency":"sat","methodDetails":{"invoice":"lnbc...","network":"mainnet","paymentHash":"<hex>"}}. Echo it byte for byte. |
expires | RFC 3339 UTC. Never later than the BOLT11 invoice expiry. A modern credential must be redeemed before this instant. |
digest | Present only on proxied POST / PUT / PATCH requests with a body: the RFC 9530 Content-Digest of that body, bound into id. Resend the identical body when you redeem. |
invoice, amount, currency | Legacy parameters for clients that predate the drafts. Same invoice as request.methodDetails.invoice. |
Send Accept-Payment: lightning/charge;q=0 on the initial request if you want the Payment header suppressed and only the L402 header returned.
Redeem with a modern credential
Pay the invoice, then build the credential the drafts describe: a JSON object with the challenge you received (echoed exactly, including request) and a payload holding the preimage as 64 lowercase hex characters. base64url-encode it and send it as a bearer token.
{
"challenge": {
"id": "k9Q3...",
"realm": "lightning-enable",
"method": "lightning",
"intent": "charge",
"request": "eyJhbW91bnQiOiIxMDAiLC...",
"expires": "2026-09-11T18:30:00Z"
},
"payload": { "preimage": "7f8a9b2c...e9f0a" }
}
GET /l402/proxy/abc123/api/data HTTP/1.1
Authorization: Payment eyJjaGFsbGVuZ2UiOnsiaWQiOiJrOVEz...
On success the response includes a receipt you can store or forward:
HTTP/1.1 200 OK
Payment-Receipt: eyJjaGFsbGVuZ2VJZCI6Ims5UTMuLi4iLCJtZXRob2QiOiJsaWdodG5pbmciLC...
Cache-Control: private
The receipt decodes to {"challengeId":"k9Q3...","method":"lightning","reference":"<payment hash>","status":"success","timestamp":"2026-09-11T18:12:04Z"}. The reference is the payment hash, never the preimage.
Single use. A modern credential is consumed atomically the first time it verifies. A second request with the same token gets a fresh 402. If the server accepted the credential but failed to deliver the upstream response, the consumption is released so you can retry with the same token. Redeem before expires. Classic L402 and legacy Payment credentials keep their reuse-until-expiry behaviour; nothing about them changed.
Redeem with a legacy Payment credential
Authorization: Payment method="lightning", preimage="7f8a9b2c...e9f0a"
No macaroon and no challenge echo. The server matches the preimage to a challenge it minted for that resource and price. Reusable until the challenge expires.
Modern-path errors
Failures on the modern path return 402 with application/problem+json, a fresh challenge in WWW-Authenticate, and Cache-Control: no-store. The type is https://paymentauth.org/problems/<slug>:
type slug | Meaning |
|---|---|
invalid-challenge | The echoed challenge doesn't decode, has a currency other than sat, or is missing its expiry or payment hash. |
verification-failed | The preimage doesn't hash to the challenge's payment hash, the challenge belongs to another merchant, the credential is past expires, or it was already redeemed (single use). The detail member says which. |
payment-insufficient | The challenge amount doesn't match the resource's price. |
method-unsupported | Modern credentials are switched off on this server. Fall back to L402. |
A malformed bearer token is rejected outright. It is never reinterpreted as a legacy credential.
Verify credentials as a producer
If you mint challenges with the Producer API, verify each profile with the matching endpoint:
- Classic L402 and legacy
Paymentcredentials:POST /api/l402/challenges/verify. - Modern bearer credentials:
POST /api/l402/challenges/verify-credential, which consumes the token and returns a ready-to-servePayment-Receiptvalue.
L402 Producer API
The Producer API lets merchants create L402 challenges programmatically — enabling agent-to-agent commerce where your AI agent charges other agents for access to resources.
Every plan can mint challenges. The Free Producer Sandbox is capped at 3 endpoints, 200 challenges a month, and 1,000 sats per challenge; Agentic Commerce ($49/mo) lifts the caps. Paying for L402 resources needs no Lightning Enable account at all.
This is the supply side of agentic commerce. Your agent creates L402 challenges; other agents pay them. See the L402 Producer API guide for the full walkthrough.
Create L402 Challenge
Create a Lightning invoice + macaroon challenge for a resource.
POST /api/l402/challenges
Headers
| Header | Required | Description |
|---|---|---|
X-API-Key | Yes | Merchant API key |
Idempotency-Key | No | Retry-safe key, max 200 characters. The same key with the same resource and price returns the same challenge for the life of that invoice; a different resource or price under the same key is a 409 while that invoice is still live, and a fresh mint once it has expired. See Idempotency. |
X-Idempotency-Key | No | The spelling this API shipped with. Identical behaviour; Idempotency-Key wins if you send both. |
Request Body
| Field | Type | Required | Description |
|---|---|---|---|
resource | string | Yes | Resource identifier (URL, service name, or description) |
priceSats | long | Yes | Price in satoshis (minimum 1) |
description | string | No | Description shown on the Lightning invoice |
idempotencyKey | string | No | Same meaning as the Idempotency-Key header, for clients that can't set headers |
Example
curl -X POST https://api.lightningenable.com/api/l402/challenges \
-H "X-API-Key: YOUR_MERCHANT_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"resource": "/api/weather/forecast",
"priceSats": 50,
"description": "7-day weather forecast"
}'
Response (200 OK)
{
"invoice": "lnbc500n1p3xyza...",
"macaroon": "AgELbGlnaHRuaW5n...",
"paymentHash": "abc123def456...",
"expiresAt": "2026-03-13T14:30:00Z",
"resource": "/api/weather/forecast",
"priceSats": 50,
"mppChallenge": "id=\"k9Q3...\", realm=\"lightning-enable\", method=\"lightning\", intent=\"charge\", ..."
}
mppChallenge is the Payment header value for the same invoice: send WWW-Authenticate: Payment <mppChallenge> beside your L402 header so MPP clients can pay you too. It's null when MPP is switched off.
Error Responses
Returned as application/problem+json with a stable type URI. See Error format for the full body and the complete code list.
| Status | Description |
|---|---|
| 400 | Invalid body (invalid_request), a reserved renew: or signup: resource, a blank or over-long idempotency key, or no payment provider credential on your account (payment_provider_not_configured) |
| 401 | Missing or invalid API key |
| 402 | A plan cap was reached: plan_price_limit, plan_volume_limit, or plan_endpoint_limit |
| 403 | L402 not enabled on your account (l402_not_enabled) |
| 409 | idempotency_key_reuse (the key is bound to a different live resource or price), or endpoint_retired |
| 415 | No JSON body, or a Content-Type other than application/json |
| 502 | Your payment provider refused the invoice (provider_rejected), usually a key problem |
| 503 | The provider is down (provider_unavailable), your NWC wallet is busy (payment_provider_busy), or the challenge couldn't be recorded (challenge_persist_failed). Nothing was invoiced |
List Your Challenges
List the challenges you have minted, with payment status. Scoped to the account behind your API key.
GET /api/l402/challenges?status=paid&since=2026-09-01T00:00:00Z&limit=50&offset=0
| Query parameter | Default | Description |
|---|---|---|
status | none | paid, unpaid, or expired |
since | none | ISO 8601 lower bound on createdAt |
limit | 50 | Clamped to 1..200 |
offset | 0 | Clamped to at least 0 |
Returns { challenges, total, limit, offset, status, since }; the unpaged total is also in the X-Total-Count header. Each challenge carries paymentHash, resource, amountSats, status, createdAt, paidAt, expiresAt, and idempotencyKey — never a macaroon or a preimage.
curl "https://api.lightningenable.com/api/l402/challenges?status=paid&limit=25" \
-H "X-API-Key: YOUR_MERCHANT_API_KEY"
Get One Challenge
GET /api/l402/challenges/{paymentHash}
Returns the same object as one element of the list. A payment hash belonging to another account returns 404, not 403.
Verify L402 Token
Verify a classic L402 token (macaroon + preimage), or a legacy Payment credential (preimage only).
POST /api/l402/challenges/verify
Headers
| Header | Required | Description |
|---|---|---|
X-API-Key | Yes | Merchant API key |
Request Body
| Field | Type | Required | Description |
|---|---|---|---|
macaroon | string | For L402 | Base64-encoded macaroon. Omit it to verify a preimage-only Payment credential |
preimage | string | Yes | Hex-encoded preimage (64 characters) |
resource | string | No | Reject unless the challenge was minted for this resource |
Example
curl -X POST https://api.lightningenable.com/api/l402/challenges/verify \
-H "X-API-Key: YOUR_MERCHANT_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"macaroon": "AgELbGlnaHRuaW5n...",
"preimage": "7f8a9b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a"
}'
Response (200 OK — valid)
{
"valid": true,
"resource": "/api/weather/forecast",
"merchantId": 42,
"amountSats": 50,
"paymentHash": "abc123def456..."
}
Response (200 OK — invalid)
{
"valid": false,
"error": "Preimage does not match payment hash"
}
The first successful verification of a credential records the underlying challenge as paid — it starts showing as status: "paid" in the listing above, and fires the l402.challenge.paid webhook to your callback URL if you have one configured. Later verifications of the same credential do not re-fire it. See Payment webhooks.
Verify Payment Credential (single-use)
Verify a modern Payment bearer credential (the base64url JSON token described in Payment (MPP) credentials). Verifying consumes the credential: a second call with the same token returns valid: false.
POST /api/l402/challenges/verify-credential
Headers
| Header | Required | Description |
|---|---|---|
X-API-Key | Yes | Merchant API key |
Request Body
| Field | Type | Required | Description |
|---|---|---|---|
credential | string | Yes | The token exactly as received, with or without the leading Payment scheme word |
resource | string | No | Reject unless the challenge was minted for this resource |
amountSats | integer | No | Reject unless the challenge was minted for this price |
Example
curl -X POST https://api.lightningenable.com/api/l402/challenges/verify-credential \
-H "X-API-Key: YOUR_MERCHANT_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"credential": "<the base64url token from the Authorization header>",
"resource": "/api/weather/forecast",
"amountSats": 50
}'
Response (200 OK — valid)
{
"valid": true,
"consumed": true,
"resource": "/api/weather/forecast",
"merchantId": 42,
"amountSats": 50,
"paymentHash": "abc123def456...",
"receipt": "eyJjaGFsbGVuZ2VJZCI6Ims5UTMuLi4iLC..."
}
Serve receipt back to the payer as the Payment-Receipt response header on the resource you gate.
Response (200 OK — invalid)
{
"valid": false,
"consumed": false,
"error": "Preimage does not match the challenge payment hash."
}
400 mpp_not_supported means modern credentials are switched off on the server. Like /verify, the first successful call marks the challenge paid and fires l402.challenge.paid.
Client Libraries
Use an L402 client rather than hand-rolling the 402, pay, retry loop. Each one pays the invoice from your wallet, caches the credential per path, and retries:
- Python:
l402-requests - JavaScript / TypeScript:
l402-requests - .NET:
L402Requests - AI agents: the Lightning Enable MCP server
By hand with curl:
# 1. Get the challenge
curl -s https://api.lightningenable.com/l402/proxy/my-api/data > challenge.json
MACAROON=$(jq -r '.l402.macaroon' challenge.json)
INVOICE=$(jq -r '.l402.invoice' challenge.json)
# 2. Pay $INVOICE with your wallet and copy the preimage
read -p "Preimage: " PREIMAGE
# 3. Retry with the credential
curl https://api.lightningenable.com/l402/proxy/my-api/data \
-H "Authorization: L402 $MACAROON:$PREIMAGE"
In a browser with a WebLN wallet, const { preimage } = await window.webln.sendPayment(invoice); returns the preimage.
Next Steps
- Proxy Configuration - Set up L402 proxies
- How It Works - Technical deep dive
- API Monetization - Protect your APIs