LnBot.L402 0.2.0

dotnet add package LnBot.L402 --version 0.2.0
                    
NuGet\Install-Package LnBot.L402 -Version 0.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LnBot.L402" Version="0.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LnBot.L402" Version="0.2.0" />
                    
Directory.Packages.props
<PackageReference Include="LnBot.L402" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add LnBot.L402 --version 0.2.0
                    
#r "nuget: LnBot.L402, 0.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package LnBot.L402@0.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=LnBot.L402&version=0.2.0
                    
Install as a Cake Addin
#tool nuget:?package=LnBot.L402&version=0.2.0
                    
Install as a Cake Tool

LnBot.L402

NuGet License: MIT

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:

  1. If the response is 402, it reads the WWW-Authenticate header
  2. Calls client.L402.PayAsync() to pay the Lightning invoice via the ln.bot API
  3. Retries the request with the Authorization: L402 <macaroon>:<preimage> header
  4. Caches the token for future requests to the same URL

Zero crypto dependencies — all L402 logic lives in the ln.bot API.


Requirements

License

MIT

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last Updated
0.2.0 42 2/27/2026
0.1.0 47 2/27/2026