Gate any API route behind a Lightning payment using the L402 protocol (bLIP-26)
Gate any API route behind a Lightning payment. No accounts, no subscriptions — clients pay a Lightning invoice and get immediate access.This is an L402-compatible implementation (bLIP-26). Any client that speaks the L402 protocol (HTTP 402 + L402 auth scheme) can interact with your API out of the box. The legacy LSAT scheme is also accepted for backwards compatibility.
Option A: Use an AI coding assistant (recommended)Install the MCP server and let your AI agent handle setup. When the agent asks for your email, use a real email address so you can log in to your dashboard later.
Cursor
Click to install MCP in Cursor
VS Code
Click to install MCP in VS Code
Claude Code:
claude mcp add moneydevkit --transport http https://mcp.moneydevkit.com/mcp/
After signup, it’s highly recommended to log in at moneydevkit.com and switch to the authenticated MCP server (see the “Existing Account” tab). This connects your agent to your account so it can manage apps, view payments, and access your dashboard.
Option B: Manual setup
Create a moneydevkit account at moneydevkit.com or run npx @moneydevkit/create to generate credentials locally, then grab your api_key and mnemonic.
// app/api/mdk/route.jsexport { POST } from '@moneydevkit/nextjs/server/route'
Configure Next.js:
// next.config.js / next.config.mjsimport withMdkCheckout from '@moneydevkit/nextjs/next-plugin'export default withMdkCheckout({})
Option A: Use Replit Agent (recommended)Click the button below to install moneydevkit directly in Replit Agent. When the agent asks for your email, use a real email address so you can log in to your dashboard later.
After signup, it’s highly recommended to log in at moneydevkit.com and switch to the authenticated MCP server (see the “Existing Account” tab). This connects your agent to your account so it can manage apps, view payments, and access your dashboard.
Option B: Manual setup
Create a moneydevkit account at moneydevkit.com or run npx @moneydevkit/create to generate credentials locally, then grab your api_key and mnemonic.
Install the SDK (Express is a peer dependency):
npm install @moneydevkit/replit express
Add environment variables to .env (or Replit Secrets):
Every request without a valid token returns a 402 with a Lightning invoice per the L402 protocol. After payment, the same request with the authorization header returns the premium data.
The pricing function is evaluated both when creating the invoice and when verifying the token. If the price changes between issuance and verification (e.g., the client replays a cheap token on an expensive tier), the request is rejected with amount_mismatch.
By default, withPayment marks the credential as used immediately before your handler runs. If your handler fails after the credential is consumed, the payer can’t retry.Use withDeferredSettlement when the service delivery might fail and you want the payer to be able to retry. Your handler receives a settle() callback - call it only after you’ve successfully delivered the service:
Next.js
Replit / Express
// app/api/ai/route.tsimport { withDeferredSettlement, type SettleResult } from '@moneydevkit/nextjs/server'const handler = async (req: Request, settle: () => Promise<SettleResult>) => { const { prompt } = await req.json() // Do the expensive work first const result = await runAiInference(prompt) // Work succeeded - now mark the credential as used const { settled } = await settle() if (!settled) { return Response.json({ error: 'settlement_failed' }, { status: 500 }) } return Response.json({ result })}export const POST = withDeferredSettlement( { amount: 100, currency: 'SAT' }, handler,)
If your handler returns without calling settle() (e.g. it throws or the service fails), the credential stays valid and the payer can retry with the same macaroon and preimage.settle() is callable only once per request. A second call returns { settled: false, error: 'already_settled' } without hitting the backend.
A 402 is only returned when no L402/LSAT authorization header is present. If the header is present but malformed or invalid, you get a 401 - not a new invoice. This prevents wasting invoices on bad auth attempts.