LnBot.L402
0.2.0
dotnet add package LnBot.L402 --version 0.2.0
NuGet\Install-Package LnBot.L402 -Version 0.2.0
<PackageReference Include="LnBot.L402" Version="0.2.0" />
<PackageVersion Include="LnBot.L402" Version="0.2.0" />
<PackageReference Include="LnBot.L402" />
paket add LnBot.L402 --version 0.2.0
#r "nuget: LnBot.L402, 0.2.0"
#:package LnBot.L402@0.2.0
#addin nuget:?package=LnBot.L402&version=0.2.0
#tool nuget:?package=LnBot.L402&version=0.2.0
LnBot.L402
L402 Lightning payment client for .NET — auto-pay any L402-protected API with HttpClient. Works in console apps, background services, MAUI — anything with HttpClient. Built on ln.bot.
Looking for server-side middleware? See
LnBot.L402.AspNetCore.
What is L402?
L402 is a protocol built on HTTP 402 Payment Required. It enables machine-to-machine micropayments over the Lightning Network — ideal for API monetization, AI agent tool access, and pay-per-request data feeds.
Install
dotnet add package LnBot.L402
Quick Start
With DI (ASP.NET Core, Worker Services)
using LnBot;
using LnBot.L402;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton(new LnBotClient("key_..."));
builder.Services.AddHttpClient("paid-apis")
.AddL402Handler(new L402ClientOptions
{
MaxPrice = 100,
BudgetSats = 50_000,
BudgetPeriod = BudgetPeriod.Day,
});
var app = builder.Build();
app.MapGet("/proxy", async (IHttpClientFactory factory) =>
{
var http = factory.CreateClient("paid-apis");
var data = await http.GetStringAsync("https://api.example.com/premium/data");
return Results.Ok(data);
});
Console App (no ASP.NET Core)
using LnBot;
using LnBot.L402;
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection();
services.AddSingleton(new LnBotClient("key_..."));
services.AddSingleton<ITokenStore, MemoryTokenStore>();
services.AddHttpClient("paid-apis").AddL402Handler();
var provider = services.BuildServiceProvider();
var http = provider.GetRequiredService<IHttpClientFactory>().CreateClient("paid-apis");
// Auto-pays any 402 responses transparently
var response = await http.GetStringAsync("https://api.example.com/premium/data");
Console.WriteLine(response);
Options
| Property | Type | Default | Description |
|---|---|---|---|
MaxPrice |
int |
int.MaxValue |
Max sats per request. Throws L402BudgetExceededException if exceeded. |
BudgetSats |
long |
0 (unlimited) |
Rolling spend limit in sats. |
BudgetPeriod |
BudgetPeriod |
Day |
Reset interval: Minute, Hour, Day. |
Custom Token Store
Implement ITokenStore for Redis, file system, or any persistence layer:
public class RedisTokenStore : ITokenStore
{
public Task<L402Token?> GetAsync(string url) { /* ... */ }
public Task SetAsync(string url, L402Token token) { /* ... */ }
public Task DeleteAsync(string url) { /* ... */ }
}
services.AddSingleton<ITokenStore, RedisTokenStore>();
Header Utilities
using LnBot.L402;
L402Headers.ParseAuthorization("L402 mac_base64:preimage_hex");
L402Headers.ParseChallenge("L402 macaroon=\"abc\", invoice=\"lnbc1...\"");
L402Headers.FormatAuthorization("mac", "pre");
L402Headers.FormatChallenge("mac", "lnbc1...");
How It Works
The L402DelegatingHandler intercepts HttpClient responses:
- If the response is
402, it reads theWWW-Authenticateheader - Calls
client.L402.PayAsync()to pay the Lightning invoice via the ln.bot API - Retries the request with the
Authorization: L402 <macaroon>:<preimage>header - Caches the token for future requests to the same URL
Zero crypto dependencies — all L402 logic lives in the ln.bot API.
Requirements
- .NET 8+
- An ln.bot API key — create a wallet to get one
Related
LnBot.L402.AspNetCore— Server-side middlewareLnBot— The .NET SDK this package is built on@lnbot/l402— TypeScript equivalent
Links
- ln.bot — website
- Documentation
- L402 specification
- GitHub
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- LnBot (>= 0.5.0)
- Microsoft.Extensions.Http (>= 8.0.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on LnBot.L402:
| Package | Downloads |
|---|---|
|
LnBot.L402.AspNetCore
L402 Lightning payment middleware for ASP.NET Core — paywall any API in one line |
GitHub repositories
This package is not used by any popular GitHub repositories.