AgentRails.AgentFramework.X402
0.1.0
dotnet add package AgentRails.AgentFramework.X402 --version 0.1.0
NuGet\Install-Package AgentRails.AgentFramework.X402 -Version 0.1.0
<PackageReference Include="AgentRails.AgentFramework.X402" Version="0.1.0" />
<PackageVersion Include="AgentRails.AgentFramework.X402" Version="0.1.0" />
<PackageReference Include="AgentRails.AgentFramework.X402" />
paket add AgentRails.AgentFramework.X402 --version 0.1.0
#r "nuget: AgentRails.AgentFramework.X402, 0.1.0"
#:package AgentRails.AgentFramework.X402@0.1.0
#addin nuget:?package=AgentRails.AgentFramework.X402&version=0.1.0
#tool nuget:?package=AgentRails.AgentFramework.X402&version=0.1.0
AgentRails.AgentFramework.X402
x402 payment tools for Microsoft Agent Framework (via Microsoft.Extensions.AI). Enables .NET AI agents to make HTTP requests with automatic x402 payment handling. When an API returns HTTP 402 Payment Required, the tools automatically sign a USDC payment authorization (EIP-3009) and retry the request.
Built by AgentRails — the first x402 integration for the Microsoft Agent Framework ecosystem.
Note: If you're using Semantic Kernel, see the companion package AgentRails.SemanticKernel.X402.
Installation
dotnet add package AgentRails.AgentFramework.X402
Quick Start
using AgentRails.AgentFramework.X402;
using AgentRails.AgentFramework.X402.Extensions;
using AgentRails.AgentFramework.X402.Models;
using Microsoft.Extensions.AI;
// Create x402 tools
var options = new X402PluginOptions
{
PrivateKey = Environment.GetEnvironmentVariable("WALLET_PRIVATE_KEY")!,
Network = "eip155:84532", // Base Sepolia testnet
BudgetUsd = 5.00m,
};
var wallet = new X402Wallet(options);
var httpFactory = /* your IHttpClientFactory */;
var x402 = new X402Tools(wallet, httpFactory, options);
// Register as tools on any IChatClient-based agent
IChatClient chatClient = /* your chat client (OpenAI, Azure, Ollama, etc.) */;
var response = await chatClient.GetResponseAsync(
"Get the analysis from https://sandbox.agentrails.io/api/x402/protected/analysis",
new ChatOptions { Tools = x402.GetTools() });
Console.WriteLine(response);
With Dependency Injection
using AgentRails.AgentFramework.X402.Extensions;
// In your service registration:
builder.Services.AddX402Tools(options =>
{
options.PrivateKey = Environment.GetEnvironmentVariable("WALLET_PRIVATE_KEY")!;
options.Network = "eip155:84532";
options.BudgetUsd = 5.00m;
});
// Later, resolve and use:
var x402 = serviceProvider.GetRequiredService<X402Tools>();
var tools = x402.GetTools(); // IList<AIFunction>
appsettings.json Configuration
{
"X402": {
"PrivateKey": "your-hex-private-key",
"Network": "eip155:84532",
"BudgetUsd": 10.00,
"DefaultMaxPriceUsd": 1.00,
"AutoPay": true,
"TimeoutSeconds": 30
}
}
builder.Services.AddX402Tools(configuration.GetSection("X402"));
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 agent 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
- Agent Framework native — Exposes tools as
IList<AIFunction>viaAIFunctionFactory
Tools
The package exposes three AI functions:
| Function | Description |
|---|---|
MakePaidRequestAsync |
Make an HTTP request to any URL. Automatically handles x402 payment if required. |
CheckBudget |
Check wallet address, network, budget, spending, and remaining balance. |
GetPaymentHistory |
Get the history of all x402 payments made during this session. |
MakePaidRequestAsync 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 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 calls
MakePaidRequestAsyncwith a URL - The tool 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.Extensions.AI 9.3.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. |
-
net8.0
- Microsoft.Extensions.AI (>= 9.5.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.3)
- Microsoft.Extensions.Options (>= 8.0.2)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.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 | 63 | 2/23/2026 |