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" />
<PackageReference Include="AgentRails.SemanticKernel.X402" />
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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
#tool nuget:?package=AgentRails.SemanticKernel.X402&version=0.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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
- Your AI agent decides to call
make_paid_requestwith a URL - The plugin makes the HTTP request
- If the server returns 200 OK, the response is returned directly
- If the server returns 402 Payment Required:
- Parses the
PAYMENT-REQUIREDheader (V2) orX-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
TransferWithAuthorizationmessage locally - Retries the request with the
PAYMENT-SIGNATUREheader - Returns the response prefixed with payment details
- Parses the
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 = falseto require explicit approval before any payment.
Requirements
- .NET 8.0+
- Microsoft Semantic Kernel 1.30.0+
License
MIT — see LICENSE
Links
| 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- Microsoft.Extensions.Configuration.Binder (>= 8.0.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Http (>= 8.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Options (>= 8.0.2)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- Microsoft.SemanticKernel.Abstractions (>= 1.30.0)
- Nethereum.Signer (>= 5.8.0)
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 |