Skip to main content

Native L402 Integration

Lightning Enable supports two integration shapes. Native mode is the one where your API stays exactly where it is — same domain, same auth, same observability — and Lightning Enable handles the payment protocol via a small middleware you drop into your existing app.

When to choose Native mode over Proxy mode​

Proxy modeNative mode
Setup time~5 minutes in the dashboardOne install + one line of middleware
Code changes to your APINoneAdd one middleware registration
Traffic flows through Lightning EnableYesNo — your domain, your servers
You can keep your existing authDifficult (Authorization header is consumed by L402)Yes
Custom observability / rate limitingLimitedFull control
Best forPublic APIs, experiments, no-existing-authCommercial APIs, anything with sensitive infrastructure

If your API has its own authentication, custom rate limiting, observability you don't want to lose, or is hosted somewhere you can't change DNS for — Native mode is the right answer.

The two halves of the protocol​

The L402 protocol has a consumer side and a producer side. Lightning Enable publishes packages for both:

SideAudiencePackages
ConsumerAgents / clients that pay for paid APIsl402-requests (Node), L402Requests (.NET), l402-requests (Python)
ProducerAPI providers that charge for their endpointsThe packages described on this page

If you're calling paid APIs from an agent, you want the consumer packages. If you're building a paid API that you want agents to pay for, you want the producer packages — that's what the rest of this page is about.

Two layers: SDK and middleware​

Each language ships two packages: a server SDK (raw HTTP client for our producer API) and a framework middleware (drop-in for your web framework, built on top of the SDK).

StackSDKFramework middleware
Node + TypeScriptl402-serverl402-express
.NETL402ServerL402Server.AspNetCore

What the SDK does​

The SDK wraps two Lightning Enable hosted endpoints:

  • POST /api/l402/challenges → createChallenge() → mints a Lightning invoice + macaroon
  • POST /api/l402/challenges/verify → verifyToken() → validates an incoming L402 credential

That's it. ~200 lines of typed HTTP-client glue. The SDK knows nothing about HTTP frameworks, routes, or middleware pipelines. You give it inputs, it calls Lightning Enable, you get outputs. Use the SDK directly if you're calling these from a background job, queue worker, serverless function, or anywhere that isn't a typical HTTP middleware.

What the middleware does​

The framework middleware sits on top of the SDK and handles all the wiring for the 90% case (an HTTP API that wants to charge per request):

  1. Read the Authorization: L402 <macaroon>:<preimage> header
  2. If absent → call createChallenge(), respond 402 Payment Required with the invoice
  3. If present → call verifyToken(). Valid → call next handler; invalid → respond 401

That's all it does — and it does it so you don't have to write the parse-call-respond dance in every paid route. Most merchants will only ever touch the middleware. The SDK is what the middleware imports under the hood.

Resource and amount enforcement by version​

0.2.0 and later (l402-server / l402-express 0.2.0, L402Server / L402Server.AspNetCore 0.2.0):

  • The SDKs accept optional resource and amountSats on verifyToken() (Resource and AmountSats on VerifyTokenRequest in .NET). When you set them, the hosted API enforces the macaroon's path and amount caveats and reports a token minted for a different resource or price as invalid.
  • The middleware sends the request's resource with every verification by default, so a token bought for one of your paths returns 401 on a different path. Opt out with verifyResource: false (Express) or EnforceResourceOnVerify = false (ASP.NET Core).
  • The middleware doesn't send the price. If you charge different prices on the same resource, put the price-deciding input into the resource, or compare the verified amount in your handler. Each stack page shows how.

0.1.x releases call the verify endpoint with only the macaroon and preimage. They don't pass resource or amountSats, and they don't compare the verified token's resource against the incoming request path. Within the token validity window (60 minutes by default), a token bought for one of your paths can pass verification on a different path of yours. Upgrade to 0.2.0 or later; if you can't, compare the verified resource in your handler as shown on the Express page and the ASP.NET Core page.

On every version, the hosted API enforces merchant binding and expiry.

Quick start by stack​

  • Express (Node.js) — npm install l402-express, then one line of app.use(l402({ apiKey, priceSats }))
  • ASP.NET Core (.NET) — dotnet add package L402Server.AspNetCore, then app.UseL402() + [L402(PriceSats = 100)] attribute
  • Any other language — call the producer API directly; see the Producer API Reference.

Architectural decisions baked into the SDKs​

These are intentional. Worth knowing what you're getting:

  • No protocol code on the client side. Macaroon signing, preimage hashing, payment-hash linking — all server-side at Lightning Enable. The SDK is HTTP-client glue. If the protocol evolves, the hosted endpoint changes; you don't update your code.
  • Verification via the hosted endpoint, not local key material. Every verifyToken call goes to /api/l402/challenges/verify. One round-trip per paid request. We don't distribute the L402 root key to merchants — that's centralized for security and for replay protection.
  • Token reuse within the validity window is a feature. A paid token verifies again and again until its expires caveat (60 minutes by default), so an agent can make many quick calls on one payment. Merchant binding and expiry are always enforced; path and amount are enforced when the verify call includes resource and amountSats. For strict single-use semantics, track paymentHash in your handler. See Token reuse within the validity window.
  • No credentials stored anywhere. Lightning Enable never asks for your upstream API credentials. The middleware never touches your authentication. Your secrets stay on your servers.

Prerequisites​

  • A Lightning Enable account. The free Free Producer Sandbox (3 endpoints, 200 challenges a month, 1,000 sats max per challenge) is enough to build and test; Agentic Commerce ($49/month, 30-day free trial) removes the caps. Agentic Commerce — Business is contact us.
  • A payment provider — Strike (recommended), OpenNode, or your own wallet over Nostr Wallet Connect — saved under Dashboard → Settings → Payment Provider.
  • A Lightning Enable merchant API key, from Dashboard → Settings → API Credentials.
  • An existing HTTP API (or a fresh one) on Node + Express or .NET + ASP.NET Core. For another language, use the Producer API Reference.

What the merchant pays for​

The middleware is open-source MIT. The SDK is open-source MIT. Lightning Enable's value is the hosted protocol broker — the API endpoint that mints macaroons, signs them with our root key, verifies preimages, enforces caveats, and talks to your payment provider (Strike, OpenNode, or your NWC wallet). Your plan pays for that broker, the dashboard, and the registry that lets agents discover your API.

Testing your integration​

Three practical ways to exercise a Native integration before real traffic hits it:

  1. Pay real 1-sat invoices. Set a 1-sat price on a staging route and pay the invoices from your own wallet; you're out ~1 sat per test plus routing fees. (Hosted Lightning Enable runs OpenNode in production mode, so an OpenNode dev/testnet key won't mint here.)
  2. Unit-test without any network calls. Both SDKs let you inject the HTTP layer, so you can stub the producer API's responses:
    • Node: the L402Server constructor accepts a custom fetch implementation (new L402Server({ apiKey, fetch: mockFetch })) — return canned challenge/verification JSON from your mock.
    • .NET: L402ServerClient has a constructor overload that accepts an external HttpClient (designed for IHttpClientFactory) — hand it an HttpClient built on a fake HttpMessageHandler that returns canned responses.
  3. Smoke-test against the live example apps. The runnable reference apps at l402-example-node and l402-example-aspnet are complete, working integrations — curl them to see exactly what a healthy 402 → pay → 200 flow looks like, and diff their setup against yours when something misbehaves.

Next steps​