x402 payment protocol tools for Microsoft Agent Framework. Enable .NET AI agents to pay for APIs with USDC.
$ dotnet add package AgentRails.AgentFramework.X402x402 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.
dotnet add package AgentRails.AgentFramework.X402
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);
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>
{
"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"));
IList<AIFunction> via AIFunctionFactoryThe 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. |
| 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) |
| 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 |
| 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) |
MakePaidRequestAsync with a URLPAYMENT-REQUIRED header (V2) or X-PAYMENT-REQUIRED (V1)TransferWithAuthorization message locallyPAYMENT-SIGNATURE headerAutoPay = false to require explicit approval before any payment.MIT — see LICENSE