Skip to content
Protocol Explainer • Bitcoin Lightning

What is L402? The Lightning payment protocol built for machines

L402 revives the long-reserved HTTP 402 status code so software can pay for APIs, data and tools over the Bitcoin Lightning Network, using the payment itself as the credential instead of a per-service account or API key.

PayAgents Research • • 9 min read
payagents:insight > "HTTP reserved the 402 Payment Required code in 1997. Lightning finally gives the internet a cash small and fast enough to use it."

L402 is an open protocol for charging for access to HTTP resources (APIs, data feeds, MCP tools) using Bitcoin's Lightning Network. A server answers an unpaid request with 402 Payment Required and a Lightning invoice. The client pays the invoice, proves it cryptographically, and gets the resource. Between the client and the server there is no signup form, no API key to issue, no card on file.

The protocol was originally published by Lightning Labs under the name LSAT (Lightning Service Authentication Token) and later renamed L402. It is one of two major revivals of HTTP 402 in the agent economy; the other is x402, which settles in stablecoins such as USDC. PayAgents supports both, because agents and sellers should not have to bet on a single rail.

How the L402 handshake works

The whole exchange is four steps and can complete in about a second once a Lightning route exists (route availability and wallet behavior vary):

  1. The client requests a paid resource, like any ordinary HTTP call.
  2. The server responds 402 Payment Required with a WWW-Authenticate: L402 header carrying a macaroon and a Lightning invoice (a BOLT11 payment request).
  3. The client pays the invoice over Lightning. The payment preimage it receives back is cryptographic proof of payment.
  4. The client retries the request with an Authorization: L402 <macaroon>:<preimage> header. The server verifies the pair without touching a payment database and serves the resource.
# Illustrative example: the shape of a real exchange, values shortened

→ GET /api/v2/forecast
← 402 Payment Required
   WWW-Authenticate: L402 token="AgEEbGF...", invoice="lnbc300n1p..."

⚡ Client pays the invoice over Lightning (often ~1s once routed)

→ GET /api/v2/forecast
   Authorization: L402 AgEEbGF...:8f3c1a...preimage_hex
← 200 OK (forecast data)

Two details make this practical for machines. First, payment and authorization are the same object: the preimage both settles the invoice and unlocks the credential. Second, verification is stateless: the server checks the macaroon with its root key and basic cryptography, so it never needs to look up whether an invoice was paid.

The macaroon: not a cookie, a capability

The credential in an L402 challenge is a macaroon: a bearer token whose permissions are embedded in the token itself as "caveats". A service can encode what was bought, for how long, at what rate, and up to what spend, and the holder can add further restrictions before passing it on. An agent that bought cloud storage access could hand a sub-agent a read-only, one-hour version of the same credential.

Because a macaroon is a bearer instrument, it should be scoped tightly and treated as a secret, exactly like a wallet seed or an API key.

A short history of HTTP 402

  • 1997: RFC 2068 reserves status code 402 "Payment Required" for future use. No digital cash exists that could use it, so it sits dormant for decades.
  • 2009: Bitcoin launches, the first decentralized digital cash to reach sustained global use.
  • 2018: The Lightning Network reaches mainnet beta, making near-instant, low-cost micropayments practical.
  • 2020: Lightning Labs publishes the LSAT specification (later renamed L402) and Aperture, an LSAT/L402-aware reverse proxy it has used with its own services.
  • 2023: Lightning Labs releases open-source tools that let AI agents send and receive Lightning payments, and agent frameworks begin adopting L402.

L402 vs x402: an honest comparison

The two protocols share the same handshake shape and differ mainly in the settlement rail. Neither is strictly better; they suit different price bands and treasuries.

L402 (Lightning) x402 (Stablecoin)
Settles in Bitcoin (sats) over Lightning USDC on an EVM chain (e.g. Base)
Unit of account Sats: a fixed amount of BTC, a floating amount of fiat Dollar-denominated and stable
Cost floor No chain gas; routing fees on tiny payments are typically a fraction of a sat to a few sats, so sub-cent pricing stays workable Gas and settlement costs push pricing toward larger amounts or batching
Settlement speed Often about a second once a route exists; depends on route availability Bounded by the chain's block and confirmation times
Credential Macaroon + preimage; stateless verification; nothing to register Signed transfer authorization from an EVM address; durable and publicly linkable
Honest tradeoff BTC denomination moves against fiat, and someone must manage Lightning liquidity Per-call costs and confirmation latency are higher, and funds sit on a specific chain

Our working view: tiny, constant, machine-priced calls are Lightning's home turf, while dollar-denominated or higher-value settlement fits stablecoins. The two protocols will likely serve different price bands rather than one replacing the other, which is why PayAgents routes across both from a single balance.

How PayAgents speaks L402

A note on setup: the "no account" property describes the protocol exchange itself. Using L402 through PayAgents involves a PayAgents account and a funded balance, or your own Lightning address on the seller side; what disappears is the per-service signup between the agent and each endpoint it pays.

On the seller side, one line of middleware turns any route into an L402- or x402-gated endpoint. An unpaid request gets a 402 with your price; the handler runs only after payment is verified:

import express from 'express';
import { PayAgents } from 'payagents';

const pa = PayAgents.server({ apiKey: process.env.PAYAGENTS_API_KEY! });
const app = express();

app.post('/tool', pa.charge({ price: '0.002 USD' }).express(), (req, res) => {
  // Runs ONLY after payment is verified.
  res.json({ data: 'premium result' });
});

app.listen(3000);

On the buyer side, an agent pays a compatible L402- or x402-gated endpoint with one call, within the budget you set. A bare 402 status alone is not enough; the endpoint must speak one of the two challenge schemes:

import { PayAgents } from 'payagents';

const client = new PayAgents({ apiKey: process.env.PAYAGENTS_API_KEY });

// Detects whether the challenge is L402 or x402 and routes the payment.
const response = await client.pay('https://api.example.com/gated-route', {
  maxAmount: '0.01 USD'
});

Both examples above are copied from the PayAgents documentation and were not executed as part of this page's publication.

Sellers can receive sats directly to their own Lightning address, where PayAgents only verifies payments and never takes custody, or use a managed USD balance while in beta. The details are in the PayAgents documentation and the non-custodial receiving guide.

Common questions about L402

What does L402 stand for?

L for Lightning, 402 for the HTTP status code "Payment Required". The protocol was originally called LSAT (Lightning Service Authentication Token); L402 is the newer name for the same design.

Do I need to run a Lightning node to use it?

Not necessarily. The protocol only requires that the client can obtain the preimage as proof of payment, so any funded Lightning wallet can pay. Running a node (or using a toolkit like Lightning Labs' Aperture in front of your API) is the self-hosted route for sellers; a payment layer like PayAgents handles the invoice, verification and routing for you.

Is L402 only for AI agents?

No. It is a general machine-to-machine payment and authentication scheme; Lightning Labs has used the design, via its Aperture proxy, with its own services since 2020. AI agents simply made the "no human present" problem urgent: an autonomous program cannot fill in a card form or complete an OAuth consent screen.

Does PayAgents require L402?

No. PayAgents is multi-rail: it detects whether an endpoint challenges with L402 or x402 and routes the payment over Lightning or USDC accordingly, from one balance with one audit trail.

Further reading

A machine-readable Markdown version of this page lives at /l402.md.