# x402 pay-per-run

Updated 2026-11-13.

**Status: off.** x402 is built but disabled on asipipes.com: it stays off until an operator sets both `ASIPIPES_X402=on` and `X402_PAY_TO` (a 0x receiving address), and neither is set today. When it is turned on it will run on the Base Sepolia **testnet** with test USDC first; no real money moves. `GET /v1/billing` reports `"x402": {"enabled": true|false}`.

[x402](https://github.com/coinbase/x402) lets an agent pay for one request over plain HTTP, with no account or card. Asipipes uses it for one thing: a live pipe run that would otherwise be refused. A free tier stays available, so an agent can evaluate pipes before it ever pays.

## When a run asks for payment

Only `POST /v1/pipes/{name}/run` with `mode` live (the default). Dry runs (`mode: dry`), read-only peeks (`mode: all`), replays, and every MCP tool never ask for payment.

The operator chooses one policy:

- `over_cap` (default): only when the run would fail with `quota_exceeded` because the workspace used its daily runs or fetches.
- `no_plan`: every live run from a workspace on the Free plan. Paid plans and workspaces with custom limits are never asked.

## The flow (x402 v2, HTTP transport)

1. The agent calls the run endpoint. The server answers **HTTP 402** with a `PAYMENT-REQUIRED` header: base64 JSON of the `PaymentRequired` object. The same object is the JSON body.
2. The agent signs a payment for one of the `accepts` entries and retries with a `PAYMENT-SIGNATURE` header (base64 JSON `PaymentPayload`). `X-PAYMENT` is accepted as an alias.
3. Asipipes checks the payment matches exactly what it asked for (scheme, network, amount, asset, payTo), refuses a payload it has already seen, and asks the facilitator to `/verify` it.
4. The pipe runs. The daily run cap is skipped for this one run; the per-run fetch cap still applies.
5. Only if the run **succeeded** does Asipipes call `/settle`. The response carries `PAYMENT-RESPONSE` (base64 JSON with the transaction hash) and the run body includes `"x402": {"settled": true, "transaction": "0x…"}`.
6. If the run fails, the payment is not settled and nothing moves. If settlement fails, the answer is 402 with a `PAYMENT-RESPONSE` whose `success` is false.

## Example `PaymentRequired`

```json
{
  "x402Version": 2,
  "error": "PAYMENT-SIGNATURE header is required",
  "resource": {
    "url": "https://asipipes.com/v1/pipes/release-watch/run",
    "description": "One live run of pipe release-watch (testnet USDC on Base Sepolia).",
    "mimeType": "application/json",
    "serviceName": "Asipipes"
  },
  "accepts": [{
    "scheme": "exact",
    "network": "eip155:84532",
    "amount": "<price in USDC atomic units>",
    "asset": "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
    "payTo": "<operator wallet>",
    "maxTimeoutSeconds": 120,
    "extra": {"name": "USDC", "version": "2"}
  }],
  "extensions": {}
}
```

`eip155:84532` is Base Sepolia, and the asset is Circle's test USDC there. The price per run is not set yet; read `x402.amount_atomic` from `GET /v1/billing` once x402 is on (USDC has 6 decimals).

## Safety

- Testnet only: the server refuses to enable x402 on any network other than Base Sepolia.
- Off unless the operator sets a receiving address. Asipipes never pays agents.
- Each signed payload is accepted once.
