Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions examples/mastra-prepay-check/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Example: pre-pay check of an x402 endpoint in a Mastra agent

A [Mastra](https://mastra.ai) agent with one tool that checks an x402 paid endpoint **before anything pays for it**. The tool calls `runCheck402({ url })` from [`@agent402-dev/ai-sdk-tools`](../../packages/ai-sdk-tools): one request without a payment header, reading the 402 answer the way a buyer's agent will. Nothing is signed or paid, and no wallet, key or RPC is used.

The agent's instructions say: do not pay when `fixes > 0`.

## Files

| File | What it does |
|---|---|
| `src/index.ts` | The `check402` tool (`createTool`), the agent and its instructions, and a CLI entry that takes the URL |
| `package.json` | Pinned versions; `npm start` runs the example with `tsx` |

## Install

```sh
npm install
```

Node 22.13 or later (`@mastra/core` engine requirement).

## Run

```sh
LLM_API_KEY=sk-... npm start -- https://agent402-check402.agent402-ebc.workers.dev/x402/check402
```

The agent calls the tool once and prints its answer:

```
fixes: 0. The endpoint returned a valid x402 payment header with no fixes required. price: 0.01 USDC on Solana, 0.01 USDC on Base.
```

That URL is a live paid endpoint (the toolkit's own hosted pre-check, 0.01 USD per request); this example reads its 402 and pays nothing. Any public https x402 endpoint works.

| Variable | Default | |
|---|---|---|
| `LLM_API_KEY` (or `OPENAI_API_KEY`) | required | Key for the model endpoint |
| `LLM_BASE_URL` | `https://api.openai.com/v1` | Any OpenAI-compatible endpoint |
| `LLM_MODEL` | `gpt-5-mini` | Model ID at that endpoint |

### A local broken 402

Start a server that answers 402 without a `PAYMENT-REQUIRED` header, then point the agent at it. `--allow-local` is needed because the tool refuses localhost by default:

```sh
node -e "require('node:http').createServer((q,s)=>{s.writeHead(402,{'content-type':'application/json'});s.end(JSON.stringify({x402Version:2,error:'Payment required'}))}).listen(4020,'127.0.0.1')"
npm start -- http://127.0.0.1:4020/paid --allow-local
```

```
fixes: 1. I will not pay this endpoint. The finding reported: HTTP 402 without a PAYMENT-REQUIRED header.
```

## Result fields

The tool returns `runCheck402`'s result unchanged:

- `fixes`: number of findings a buyer will fail on; `null` when the request itself failed (`error` says why). The agent does not pay when this is above 0.
- `findings`: `fix` first, then `note`, then `ok`, each with a `code` and a `message`.
- `usdcAmounts`: USDC base units the 402 asks for, one per USDC payment option (`10000` = 0.01 USDC).
- `signedOrPaid`: always `false`.

**Nothing is paid:** the example has no paying tool, no wallet and no key; it only reads one 402 answer.

## Versions used

Run on 2026-12-05 with Node 24.15.0, `@mastra/core` 1.87.0, `@agent402-dev/ai-sdk-tools` 0.1.0, `@ai-sdk/openai-compatible` 3.0.100 and `zod` 4.6.7, model `gemini-3.5-flash` via Google's OpenAI-compatible endpoint (`LLM_BASE_URL` / `LLM_MODEL`), against the live endpoint above and a local broken 402 as shown. The default `gpt-5-mini` on `api.openai.com` was not run here (no OpenAI key).

Internal review only, not independently audited. Apache-2.0.
Loading