AgentRails.SemanticKernel.X402 0.1.0

dotnet add package AgentRails.SemanticKernel.X402 --version 0.1.0
                    
NuGet\Install-Package AgentRails.SemanticKernel.X402 -Version 0.1.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="AgentRails.SemanticKernel.X402" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AgentRails.SemanticKernel.X402" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="AgentRails.SemanticKernel.X402" />
                    
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 AgentRails.SemanticKernel.X402 --version 0.1.0
                    
#r "nuget: AgentRails.SemanticKernel.X402, 0.1.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 AgentRails.SemanticKernel.X402@0.1.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=AgentRails.SemanticKernel.X402&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=AgentRails.SemanticKernel.X402&version=0.1.0
                    
Install as a Cake Tool

AgentRails.SemanticKernel.X402

A Semantic Kernel plugin that enables AI agents to make HTTP requests with automatic x402 payment handling. When an API returns HTTP 402 Payment Required, the plugin automatically signs a USDC payment authorization (EIP-3009) and retries the request.

Built by AgentRails — the first x402 integration for the Microsoft/.NET AI ecosystem.

Installation

dotnet add package AgentRails.SemanticKernel.X402

Quick Start

using AgentRails.SemanticKernel.X402;
using AgentRails.SemanticKernel.X402.Extensions;
using Microsoft.SemanticKernel;

var builder = Kernel.CreateBuilder();
builder.AddOpenAIChatCompletion("gpt-4o", Environment.GetEnvironmentVariable("OPENAI_API_KEY")!);

// Register x402 plugin
builder.Services.AddX402Plugin(options =>
{
    options.PrivateKey = Environment.GetEnvironmentVariable("WALLET_PRIVATE_KEY")!;
    options.Network = "eip155:84532";  // Base Sepolia testnet
    options.BudgetUsd = 5.00m;         // Max spend per session
});
builder.Plugins.AddFromType<X402Plugin>();

var kernel = builder.Build();

// The agent will automatically pay for x402-protected APIs
var result = await kernel.InvokePromptAsync(
    "Get the analysis from https://sandbox.agentrails.io/api/x402/protected/analysis");
Console.WriteLine(result);

Features

  • Automatic 402 handling — Detects HTTP 402 responses, signs EIP-3009 payment authorizations, retries transparently
  • Budget management — Per-session USD budget with real-time tracking
  • Thread-safe — Safe for concurrent use across multiple kernel invocations
  • x402 V2 compliant — Supports V2 protocol with V1 backwards compatibility
  • Multi-network — Base Sepolia, Ethereum Sepolia, Arc Testnet, Base Mainnet, Ethereum Mainnet
  • Local signing — Uses Nethereum for local EIP-712 signing, no external services required

Plugin Functions

The plugin exposes three functions to the AI agent:

Function Description
make_paid_request Make an HTTP request to any URL. Automatically handles x402 payment if required.
check_budget Check wallet address, network, budget, spending, and remaining balance.
get_payment_history Get the history of all x402 payments made during this session.

make_paid_request Parameters

Parameter Type Default Description
url string required The full URL to request
method string "GET" HTTP method: GET, POST, PUT, DELETE, PATCH
body string? null Request body as JSON string
maxPriceUsd double? null Maximum USD willing to pay (overrides default)

Configuration

Lambda Configuration

builder.Services.AddX402Plugin(options =>
{
    options.PrivateKey = "your-hex-private-key";
    options.Network = "eip155:84532";
    options.BudgetUsd = 10.00m;
    options.DefaultMaxPriceUsd = 1.00m;
    options.AutoPay = true;
    options.TimeoutSeconds = 30;
});

appsettings.json Configuration

{
  "X402": {
    "PrivateKey": "your-hex-private-key",
    "Network": "eip155:84532",
    "BudgetUsd": 10.00,
    "DefaultMaxPriceUsd": 1.00,
    "AutoPay": true,
    "TimeoutSeconds": 30
  }
}
builder.Services.AddX402Plugin(configuration.GetSection("X402"));

Options

Option Default Description
PrivateKey required Hex-encoded Ethereum private key (with or without 0x prefix)
Network "eip155:8453" CAIP-2 network identifier or legacy name
BudgetUsd 10.00 Total USD budget for the session
DefaultMaxPriceUsd 1.00 Default per-request price cap
AutoPay true Automatically sign and pay, or return requirements for approval
TimeoutSeconds 30 HTTP request timeout

Supported Networks

CAIP-2 ID Legacy Name Network
eip155:84532 base-sepolia Base Sepolia (testnet)
eip155:11155111 ethereum-sepolia Ethereum Sepolia (testnet)
eip155:5042002 arc-testnet Arc Testnet
eip155:8453 base-mainnet Base (mainnet)
eip155:1 ethereum-mainnet Ethereum (mainnet)

How It Works

  1. Your AI agent decides to call make_paid_request with a URL
  2. The plugin makes the HTTP request
  3. If the server returns 200 OK, the response is returned directly
  4. If the server returns 402 Payment Required:
    • Parses the PAYMENT-REQUIRED header (V2) or X-PAYMENT-REQUIRED (V1)
    • Finds a compatible payment option matching the wallet's network
    • Checks the price against the per-request limit and session budget
    • Signs an EIP-3009 TransferWithAuthorization message locally
    • Retries the request with the PAYMENT-SIGNATURE header
    • Returns the response prefixed with payment details

Security

  • Private keys stay local — All signing happens in-process using Nethereum. No keys are ever sent over the network.
  • Budget enforcement — Hard USD cap prevents runaway spending. Thread-safe budget tracking.
  • Per-request limits — Each request has a configurable max price. The agent can also set limits per-call.
  • AutoPay toggle — Set AutoPay = false to require explicit approval before any payment.

Requirements

  • .NET 8.0+
  • Microsoft Semantic Kernel 1.30.0+

License

MIT — see LICENSE

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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0 88 2/19/2026