# x402-avm

> Let AI agents and apps pay for your tools and APIs per call, in ALGO or USDC, with no accounts and no API keys.

An AI agent that finds a useful tool can't use it if the tool wants a sign-up form, a credit card and an API key. And a tool worth a tenth of a cent per call can't be sold on card rails at all.

x402-avm fixes both. Your server answers a call with **HTTP 402 Payment Required** and a price. The caller's wallet pays it on Algorand and retries, and the call goes through. There are no accounts, no keys to issue and no invoices. Payment and result come back in the same request, in about three seconds.

<video controls preload="none" poster="/videos/x402-avm-demo-poster.png" className="w-full aspect-video rounded-xl" src="/videos/x402-avm-demo.mp4"></video>

## What you can build

- [Sell an MCP tool](https://x402-avm.github.io/build/sell-mcp-tools.md)
Put a price on each tool of a FastMCP server. Any MCP client with a wallet can call it.
- [Paywall an HTTP API](https://x402-avm.github.io/build/paywall-http-api.md)
Charge per request on a FastAPI or Flask route, using the official x402 SDK.
- [Let your agent pay](https://x402-avm.github.io/build/ai-agents.md)
Give an agent a wallet and a budget. It pays for tools on its own and refuses anything over its limits.

## Why it works for both sides

**For the seller,** a price is one line of code: `prices={"summarize": 5000}`. Payments land directly in your Algorand account, in ALGO or USDC, with nothing in between taking a cut. The payment is checked before your tool runs.

**For the buyer,** you pay only when the call succeeds. The server checks the payment, runs the tool, and submits the payment to the chain only if the tool returned a result. If the tool fails, nothing is charged. The seller pays the network fee, so the buyer's balance goes down by exactly the price shown.

**For whoever owns the agent,** spend limits are checked before anything is signed: a maximum per call, and which assets, networks and recipients may be paid. Both clients apply a limit even if you set none, refusing anything over 1 ALGO or 1 USDC per call until you raise it.

## How it looks in code

```python
from fastmcp import FastMCP
from ai_economy_sdk import PaymentGate

mcp = FastMCP("Summarizer")

@mcp.tool()
def summarize(text: str) -> str:
    return text[:200]

gate = PaymentGate(
    signer=signer,                  # your server's key; it pays the network fee
    network="testnet",
    pay_to=my_address,              # where the money goes
    prices={"summarize": 5000},     # 0.005 ALGO per call
)
app = gate.wrap(mcp, transport="streamable-http")
```

```python
from ai_economy_sdk import paid_mcp_session

async with paid_mcp_session(url, wallet_key, "testnet") as session:
    result = await session.call_tool("summarize", {"text": article})
```

```ts
import { algodClient, decodePaymentRequired, payForRequirement } from 'x402-avm-js';

const res = await fetch(url);
if (res.status === 402) {
  const required = decodePaymentRequired(res.headers.get('PAYMENT-REQUIRED')!);
  const payment = await payForRequirement(required, address, algodClient('testnet'),
    (txns, idx) => wallet.signTransactions(txns, idx));
  await fetch(url, { headers: { 'PAYMENT-SIGNATURE': payment } });
}
```

The [Quickstart](https://x402-avm.github.io/quickstart) runs a complete version on your machine in about five minutes, with test money.

## What's in the box

x402-avm is three open-source packages. Most projects need only one.

| Package | Install | Use it to |
|---|---|---|
| [`ai-economy-sdk`](https://x402-avm.github.io/build/sell-mcp-tools) | `pip install ai-economy-sdk` | Sell MCP tools, and pay for them from Python and agents |
| [`x402-algorand`](https://x402-avm.github.io/build/paywall-http-api) | `pip install x402-algorand` | Add Algorand to the official [x402](https://www.x402.org) SDK, for HTTP APIs and facilitators |
| [`x402-avm-js`](https://x402-avm.github.io/build/pay-from-browser) | `npm install x402-avm-js algosdk` | Pay from a browser wallet or Node.js |

All three follow the open x402 standard and the official [Algorand `exact` scheme](https://x402-avm.github.io/reference/protocol/avm-exact-scheme), so they work with any other x402 software on Algorand.

> **Note:**
x402-avm is an independent open-source project. It is not affiliated with GoPlausible's x402-avm packages, from which x402-algorand is derived (see [NOTICE](https://github.com/x402-avm/x402-algorand/blob/main/NOTICE)).

Source: https://x402-avm.github.io/
