Reference code for paying x402-priced URLs with Mailgent — an AI agent paying for APIs in USDC on Base.
| Folder | Language | What it shows |
|---|---|---|
payment-agent/ |
Node | Pays any x402 URL via mailgent.payments.pay({ url }) from @mailgent-dev/sdk. ~50 lines. |
payment-agent-python/ |
Python | Same as the Node buyer, using mailgent-sdk. |
Each folder is self-contained: copy .env.example to .env, fill in your Mailgent API key, install, run.
Your agent calls an x402-priced URL → gets 402 Payment Required → Mailgent pays the seller in USDC under your mandate caps → you get the content plus an on-chain txHash.
payment-agent ──1. GET /resource──▶ seller
◀─2. 402 ($0.05)──────
mailgent.payments.pay({ url }) ──▶ Mailgent ──pays──▶ seller
◀─3. content + txHash─
The buyer's wallet is set up by Mailgent automatically the first time you configure spending limits.
- Sign in at https://console.mailgent.dev, create a project, copy the
loid-…API key, and top up the wallet shown on the Identity page with a few cents of USDC on Base. - Pick a language:
- Node:
cd payment-agent && cp .env.example .env, paste the key + a target URL, thennpm install && npm start. - Python:
cd payment-agent-python && cp .env.example .env, thenpip install -r requirements.txt && python main.py.
- Node:
mailgent.payments.mandates.list()/create({ maxPerCallUsdc, dailyCapUsdc })— set the wallet's spend caps once.mailgent.payments.pay({ url })— pay any x402 URL within those caps; returns{ ok, content, txHash, cost, ... }.
Full reference: https://docs.mailgent.dev/docs/sdk-reference/payments/pay.
Selling / accepting x402 payments is a separate product — see loomal.ai.
Mailgent Pay runs on Base mainnet (real USDC) only. No testnet.
Open an issue, or reach us at https://mailgent.dev.