|
| 1 | +# LnBot.L402 |
| 2 | + |
| 3 | +[](https://www.nuget.org/packages/LnBot.L402) |
| 4 | +[](https://www.nuget.org/packages/LnBot.L402.AspNetCore) |
| 5 | +[](./LICENSE) |
| 6 | + |
| 7 | +**L402 Lightning payment middleware for .NET** — paywall any API in one line. Built on [ln.bot](https://ln.bot). |
| 8 | + |
| 9 | +Two NuGet packages: |
| 10 | + |
| 11 | +- **`LnBot.L402`** — Client-side. Auto-pay L402-protected APIs with any `HttpClient`. Works in console apps, background services, MAUI — anything with `HttpClient`. |
| 12 | +- **`LnBot.L402.AspNetCore`** — Server-side. Protect ASP.NET Core routes behind L402 paywalls with middleware, `[L402]` attributes, or endpoint filters. |
| 13 | + |
| 14 | +Both packages are thin glue layers. All L402 logic — macaroon creation, signature verification, preimage checking — lives in the [ln.bot API](https://ln.bot/docs) via the [`LnBot` SDK](https://www.nuget.org/packages/LnBot). Zero crypto dependencies. |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## What is L402? |
| 19 | + |
| 20 | +[L402](https://github.com/lightninglabs/L402) is a protocol built on HTTP `402 Payment Required`. It enables machine-to-machine micropayments over the Lightning Network: |
| 21 | + |
| 22 | +1. **Client** requests a protected resource |
| 23 | +2. **Server** returns `402` with a Lightning invoice and a macaroon token |
| 24 | +3. **Client** pays the invoice, obtains the preimage as proof of payment |
| 25 | +4. **Client** retries the request with `Authorization: L402 <macaroon>:<preimage>` |
| 26 | +5. **Server** verifies the token and grants access |
| 27 | + |
| 28 | +L402 is ideal for API monetization, AI agent tool access, pay-per-request data feeds, and any scenario where you want instant, permissionless, per-request payments without subscriptions or API key provisioning. |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +## Install |
| 33 | + |
| 34 | +```bash |
| 35 | +dotnet add package LnBot.L402.AspNetCore # Server (includes client package) |
| 36 | +dotnet add package LnBot.L402 # Client only (no ASP.NET Core dependency) |
| 37 | +``` |
| 38 | + |
| 39 | +--- |
| 40 | + |
| 41 | +## Server — Protect Routes with L402 |
| 42 | + |
| 43 | +### Middleware pipeline |
| 44 | + |
| 45 | +```csharp |
| 46 | +using LnBot; |
| 47 | +using LnBot.L402.AspNetCore; |
| 48 | + |
| 49 | +var builder = WebApplication.CreateBuilder(args); |
| 50 | +builder.Services.AddSingleton(new LnBotClient("key_...")); |
| 51 | + |
| 52 | +var app = builder.Build(); |
| 53 | + |
| 54 | +app.UseL402Paywall("/api/premium", new L402Options |
| 55 | +{ |
| 56 | + Price = 10, |
| 57 | + Description = "API access", |
| 58 | +}); |
| 59 | + |
| 60 | +app.MapGet("/api/premium/data", () => Results.Ok(new { data = "premium content" })); |
| 61 | +app.MapGet("/api/free/health", () => Results.Ok(new { status = "ok" })); |
| 62 | + |
| 63 | +app.Run(); |
| 64 | +``` |
| 65 | + |
| 66 | +### Controller attribute |
| 67 | + |
| 68 | +```csharp |
| 69 | +[ApiController] |
| 70 | +[Route("api/[controller]")] |
| 71 | +public class WeatherController : ControllerBase |
| 72 | +{ |
| 73 | + [L402(Price = 50, Description = "Weather forecast")] |
| 74 | + [HttpGet("forecast")] |
| 75 | + public IActionResult GetForecast() |
| 76 | + => Ok(new { forecast = "sunny" }); |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +### Minimal API endpoint filter |
| 81 | + |
| 82 | +```csharp |
| 83 | +app.MapGet("/api/premium/data", () => Results.Ok(new { data = "premium" })) |
| 84 | + .AddEndpointFilter(new L402EndpointFilter(price: 10, description: "API access")); |
| 85 | +``` |
| 86 | + |
| 87 | +### Dynamic pricing |
| 88 | + |
| 89 | +```csharp |
| 90 | +app.UseL402Paywall("/api/dynamic", new L402Options |
| 91 | +{ |
| 92 | + PriceFactory = context => |
| 93 | + { |
| 94 | + if (context.Request.Path.StartsWithSegments("/api/dynamic/bulk")) |
| 95 | + return Task.FromResult(50); |
| 96 | + return Task.FromResult(5); |
| 97 | + } |
| 98 | +}); |
| 99 | +``` |
| 100 | + |
| 101 | +--- |
| 102 | + |
| 103 | +## Client — Auto-Pay L402 APIs |
| 104 | + |
| 105 | +### With ASP.NET Core DI |
| 106 | + |
| 107 | +```csharp |
| 108 | +var builder = WebApplication.CreateBuilder(args); |
| 109 | +builder.Services.AddSingleton(new LnBotClient("key_...")); |
| 110 | + |
| 111 | +builder.Services.AddHttpClient("paid-apis") |
| 112 | + .AddL402Handler(new L402ClientOptions |
| 113 | + { |
| 114 | + MaxPrice = 100, |
| 115 | + BudgetSats = 50_000, |
| 116 | + BudgetPeriod = BudgetPeriod.Day, |
| 117 | + }); |
| 118 | + |
| 119 | +var app = builder.Build(); |
| 120 | + |
| 121 | +app.MapGet("/proxy", async (IHttpClientFactory factory) => |
| 122 | +{ |
| 123 | + var http = factory.CreateClient("paid-apis"); |
| 124 | + var data = await http.GetStringAsync("https://api.example.com/premium/data"); |
| 125 | + return Results.Ok(data); |
| 126 | +}); |
| 127 | +``` |
| 128 | + |
| 129 | +### Console app (no ASP.NET Core) |
| 130 | + |
| 131 | +```csharp |
| 132 | +using LnBot; |
| 133 | +using LnBot.L402; |
| 134 | +using Microsoft.Extensions.DependencyInjection; |
| 135 | + |
| 136 | +var services = new ServiceCollection(); |
| 137 | +services.AddSingleton(new LnBotClient("key_...")); |
| 138 | +services.AddSingleton<ITokenStore, MemoryTokenStore>(); |
| 139 | +services.AddHttpClient("paid-apis").AddL402Handler(); |
| 140 | + |
| 141 | +var provider = services.BuildServiceProvider(); |
| 142 | +var http = provider.GetRequiredService<IHttpClientFactory>().CreateClient("paid-apis"); |
| 143 | + |
| 144 | +// Auto-pays any 402 responses transparently |
| 145 | +var response = await http.GetStringAsync("https://api.example.com/premium/data"); |
| 146 | +Console.WriteLine(response); |
| 147 | +``` |
| 148 | + |
| 149 | +--- |
| 150 | + |
| 151 | +## Header Utilities |
| 152 | + |
| 153 | +```csharp |
| 154 | +using LnBot.L402; |
| 155 | + |
| 156 | +// Parse Authorization: L402 <macaroon>:<preimage> |
| 157 | +var auth = L402Headers.ParseAuthorization("L402 mac_base64:preimage_hex"); |
| 158 | +// → (Macaroon: "mac_base64", Preimage: "preimage_hex") |
| 159 | +
|
| 160 | +// Parse WWW-Authenticate: L402 macaroon="...", invoice="..." |
| 161 | +var challenge = L402Headers.ParseChallenge("L402 macaroon=\"abc\", invoice=\"lnbc1...\""); |
| 162 | +// → (Macaroon: "abc", Invoice: "lnbc1...") |
| 163 | +
|
| 164 | +// Format headers |
| 165 | +L402Headers.FormatAuthorization("mac", "pre"); // → "L402 mac:pre" |
| 166 | +L402Headers.FormatChallenge("mac", "lnbc1..."); // → "L402 macaroon=\"mac\", invoice=\"lnbc1...\"" |
| 167 | +``` |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +## Custom Token Store |
| 172 | + |
| 173 | +Implement `ITokenStore` for Redis, file system, or any persistence layer: |
| 174 | + |
| 175 | +```csharp |
| 176 | +public class RedisTokenStore : ITokenStore |
| 177 | +{ |
| 178 | + public Task<L402Token?> GetAsync(string url) { /* ... */ } |
| 179 | + public Task SetAsync(string url, L402Token token) { /* ... */ } |
| 180 | + public Task DeleteAsync(string url) { /* ... */ } |
| 181 | +} |
| 182 | + |
| 183 | +// Register in DI |
| 184 | +services.AddSingleton<ITokenStore, RedisTokenStore>(); |
| 185 | +``` |
| 186 | + |
| 187 | +--- |
| 188 | + |
| 189 | +## How It Works |
| 190 | + |
| 191 | +**Server middleware** makes two SDK calls: |
| 192 | +- `client.L402.CreateChallengeAsync()` — creates an invoice + macaroon when a client needs to pay |
| 193 | +- `client.L402.VerifyAsync()` — verifies an L402 authorization token when a client presents one |
| 194 | + |
| 195 | +**Client handler** makes one SDK call: |
| 196 | +- `client.L402.PayAsync()` — pays a Lightning invoice and returns a ready-to-use Authorization header |
| 197 | + |
| 198 | +--- |
| 199 | + |
| 200 | +## Requirements |
| 201 | + |
| 202 | +- **.NET 8+** |
| 203 | +- An [ln.bot](https://ln.bot) API key — [create a wallet](https://ln.bot/docs) to get one |
| 204 | + |
| 205 | +## Related packages |
| 206 | + |
| 207 | +- [`LnBot`](https://www.nuget.org/packages/LnBot) — The .NET SDK this package is built on |
| 208 | +- [`@lnbot/l402`](https://www.npmjs.com/package/@lnbot/l402) — TypeScript/Express.js equivalent |
| 209 | +- [`@lnbot/sdk`](https://www.npmjs.com/package/@lnbot/sdk) — TypeScript SDK |
| 210 | + |
| 211 | +## Links |
| 212 | + |
| 213 | +- [ln.bot](https://ln.bot) — website |
| 214 | +- [Documentation](https://ln.bot/docs) |
| 215 | +- [L402 specification](https://github.com/lightninglabs/L402) |
| 216 | +- [GitHub](https://github.com/lnbotdev) |
| 217 | + |
| 218 | +## License |
| 219 | + |
| 220 | +MIT |
0 commit comments