TonPay 402 is a specialized infrastructure toolkit designed to enable secure, autonomous, and policy-driven payments for AI agents on the TON Blockchain. By leveraging the Wallet V5 (W5) standard and the x402 (HTTP 402) protocol
TonPay402 is an early-stage TypeScript project in the AI payments / x402 ecosystem. It currently has 2 GitHub stars and 0 forks.

🤖 TonPay 402: The Universal M2M Payment Rail for TON AI Agents TonPay 402 is a specialized infrastructure toolkit designed to enable secure, autonomous, and policy-driven payments for AI agents on the TON Blockchain. By leveraging the Wallet V5 (W5) standard and the x402 (HTTP 402) protocol, it provides the "financial guardrails" necessary for agents to participate in the machine economy without compromising user funds.
TonPay402 is not just a 402 paywall gateway — it is a treasury policy engine for AI agents.
ApprovalRequest -> Approve/Reject)requestId + approval refs + status transitions| Capability | Generic 402 Gateway | TonPay402 |
|---|---|---|
| Core flow | Paywall access (pay -> token) | Policy-checked treasury execution |
| Over-limit handling | Usually reject/fail | Emit ApprovalRequest + human approval path |
| Emergency response | Usually off-chain/manual | On-chain EmergencyStop / ResumeOperations controls |
| Trusted vendor fast lane | Rare | Whitelist-based limit bypass for approved recipients |
| Approval lifecycle | Minimal/manual | pending -> approved/rejected/failed persisted state |
| Auditability | Basic payment logs | Correlated requestId + approval records + status trail |
| Agent safety | Limited | On-chain spend limits + explicit HITL escalation |
TonPay402 is a policy engine for agent treasuries, not only a payment gateway.
The "Toddler with a Credit Card" Dilemma AI Agents are becoming autonomous economic actors—buying API credits, renting GPU power, or hiring other sub-agents. However, giving an LLM-based agent full access to a seed phrase is an extreme security risk . Current solutions either:
Risk total drainage via prompt injection or logic bugs.
Require constant human approval, defeating the purpose of autonomy.
TonPay402 provides a supervised spending stack for agents:
AI Reasoning Layer: The agent identifies a need (e.g., "I need a weather forecast API"). x402 Challenge: The service provider issues an HTTP 402 challenge (Price, Address, Network). SDK Policy Check: The TonPay 402 SDK checks the request against on-chain policies. W5 Execution: If within limits, the Wallet V5 Extension authorizes an internal_signed message to execute the payment.
Human Escalation: If the limit is exceeded, an event is emitted to the Telegram Bot, providing the user with "Approve/Reject" buttons.
TonPay402 combines:
contracts/ — Tact contract source (ton_pay402.tact)tests/ — sandbox tests for payment policy behaviorscripts/ — deploy script(s)build/ — generated wrappers/artifactsmcp-server/ — MCP runtime + Telegram approval botexecute_m2m_payment.ExecutePayment to contract using the agent wallet.ApprovalRequest and does not transfer.ApprovalRequest, and notifies owner.ExecutePayment) or Reject.TonPay402 tracks manual-approval requests through a durable lifecycle in the bot state file:
pending — request detected from chain and awaiting decisionapproved — owner approved and submission was sentrejected — owner explicitly rejectedfailed — approval action failed during submissionEach request is identified by a stable approval reference and can be correlated with client-side requestId in MCP payment submissions.
Current policy in contracts/ton_pay402.tact:
owner or agent can call ExecutePaymentEmergencyStop and ResumeOperationsmcp-server/index.ts (agent signer)mcp-server/bot.ts (owner signer)@BotFather)npm install
npx blueprint build
npx blueprint test
cd mcp-server
npm install
cp .env.example .env
Fill .env values:
TON_NETWORK (testnet or mainnet)ENABLE_MAINNET_MODE (set true to allow mainnet runtime)TON_API_KEY (optional)EXECUTION_BUFFER_TONCONTRACT_ADDRESSAGENT_MNEMONICAGENT_WALLET_WORKCHAINX402_FACILITATOR_URL (optional; when set, MCP calls facilitator before on-chain submit)X402_FACILITATOR_API_KEY (optional bearer token for facilitator)X402_FACILITATOR_TIMEOUT_MS (optional, default 15000)X402_FACILITATOR_RETRY_ATTEMPTS (optional, default 0)X402_FACILITATOR_RETRY_BACKOFF_MS (optional, default 300)X402_FACILITATOR_FAIL_OPEN (optional, default false; if true, payment can continue when facilitator call fails)BROKER_STATE_FILE (persistent broker envelope state, default broker-state.json)OWNER_MNEMONICOWNER_WALLET_WORKCHAINTELEGRAM_BOT_TOKENTELEGRAM_CHAT_IDAPPROVAL_POLL_INTERVAL_MSAPPROVAL_STATE_FILE (persistent bot state file, default approval-state.json)BOOTSTRAP_HISTORY_LIMIT (how many recent txs to mark as seen on first startup)REQUEST_AUDIT_FILE (shared MCP/bot request audit log, default request-audit.json)Never commit real mnemonics or bot secrets.
For deployment script (scripts/deployTonPay402.ts) also provide:
AGENT_ADDRESS (agent wallet address to be authorized by contract)DAILY_LIMIT_TON (optional, default 10)npm install
npx blueprint build
npx blueprint test
AGENT_ADDRESS="EQ..." DAILY_LIMIT_TON="10" npx blueprint run deployTonPay402 --testnet
Put deployed address into mcp-server/.env as CONTRACT_ADDRESS and set the rest of required values.
Start MCP server and Telegram bot (separate terminals):
cd mcp-server
npm run start:mcp
cd mcp-server
npm run start:bot
Practical checks:
execute_m2m_payment with a small amount and verify transfer succeeds.execute_m2m_payment above daily limit and verify Telegram approval prompt appears.X402_FACILITATOR_URL and verify returned target/amount overrides are used.Verify persisted audit/state artifacts:
request-audit.jsonapproval-state.jsonbroker-state.jsonFrom mcp-server/:
npm run start:mcp
In a second terminal:
npm run start:bot
get_allowance
contractAddressexecute_m2m_payment
contractAddress, targetAddress, amountInTon, optional requestId, optional facilitatorContextExecutePayment from agent walletX402_FACILITATOR_URL is configured, MCP first calls AEON/x402 facilitator and can apply returned target/amount/reference before submitting on-chaincreate_envelope
envelopeId, totalBudgetTon, periodSecondsassign_agent_to_envelope
envelopeId, agentIdget_envelope_allowance
envelopeIdexecute_envelope_payment
envelopeId, agentId, contractAddress, targetAddress, amountInTon, optional requestId, optional facilitatorContextAPPROVAL_STATE_FILE since it stores approval/audit state.REQUEST_AUDIT_FILE since it links requestId to approval outcomes.tests/TonPay402.spec.ts.mcp-server/index.ts.mcp-server/bot.ts.This project is licensed under the MIT License.
The agent-native LLM router for autonomous agents. 55+ models (8 free), <1ms local routing, USDC payments on Base & Solana via x402.
A payments protocol for the internet. Built on HTTP.
A local-first AI agent with persistent memory, emotional intelligence, and a peer-to-peer skills economy.
The trust layer for agent-to-agent commerce — natural-language mandates, ERC-7710 delegated permissions, x402 payments, escrow, and dispute resolution as one open, catch-all Agent Skill / Claude Code plugin.
Self-healing infrastructure for AI agent payments. 90.3% auto-recovery.
The AI agent with a wallet — spends USDC autonomously to get real work done. Apache-2.0, TypeScript.