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
                    
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.AgentFramework.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.AgentFramework.X402" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="AgentRails.AgentFramework.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.AgentFramework.X402 --version 0.1.0
                    
#r "nuget: AgentRails.AgentFramework.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.AgentFramework.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.AgentFramework.X402&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=AgentRails.AgentFramework.X402&version=0.1.0
                    
Install as a Cake Tool

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> via AIFunctionFactory

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

  1. Your AI agent calls MakePaidRequestAsync with a URL
  2. The tool 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.Extensions.AI 9.3.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 63 2/23/2026