OpenParable
Skip to content
← Back to skills

tonapi

Apps & Automation

Work with TON blockchain via the tonapi Python SDK — query accounts, transactions, jettons, NFTs, staking, DNS, emulate messages, subscribe to real-time events, and manage webhooks. Use this skill whenever the user mentions TONAPI, tonapi.io, TON blockchain data, wallet balance, jetton transfers, NFT collections, or wants to interact with TON via TONAPI. Also use when the user works with code that imports from pytonapi.rest, pytonapi.streaming, pytonapi.webhook, or pytonapi.types, or asks about TONAPI endpoints, even if they don't explicitly mention "pytonapi".

View on GitHub ↗
QUICK START

How to use this skill

Bring this guide into your coding agent with a prompt tailored to the tool you use.

  1. Open your project in Codex.
  2. Copy and paste the prompt below.
  3. Review files and risks before approving any action.
Prompt to paste
Inspect the Agent Skill at https://github.com/nessshon/tonapi/blob/HEAD/skills/tonapi/SKILL.md and all supporting files. Report provenance, permissions, scripts, dependencies, and security risks. Propose an exact installation into .agents/skills/tonapi, but wait for my approval before writing files or running scripts.

Copying this prompt does not install or run the skill. Review third-party files before use.

Python SDK for querying TON blockchain via TONAPI. Covers REST API, streaming (SSE/WebSocket), and webhooks with event dispatcher. Read the relevant reference file, then either execute via runner or generate SDK code.

API Key

Optional — without a key, REST requests are throttled to ~1 per 4 seconds. A key unlocks higher rate limits for production use. Get one at tonconsole.com.

Networks

NetworkEnumBase URL
MainnetNetwork.MAINNEThttps://tonapi.io
TestnetNetwork.TESTNEThttps://testnet.tonapi.io
TetraNetwork.TETRAhttps://tetra.tonapi.io

Routing Table

User intentReferenceRunner example
Installation, setup, first requestreferences/quickstart.md—
SDK version—pytonapi -v
Account info, balance, jetton balances, eventsreferences/accounts.mdaccounts get_account --account-id EQ...
Blocks, transactions, validators, configreferences/blockchain.mdblockchain get_masterchain_head
TonConnect payload, state initreferences/connect.mdconnect get_ton_connect_payload
DNS domains, auctions, bidsreferences/dns.mddns get_info --domain-name ton
Message decoding, emulationreferences/emulation.mdemulation decode_message --body {...}
Events by IDreferences/events.mdevents get_event --event-id ...
Extra currency inforeferences/extra_currency.mdextra_currency get_info --id 1
Gasless transfersreferences/gasless.mdgasless config
Jetton info, holders, transfersreferences/jettons.mdjettons get_jetton_info --account-id EQ...
Raw liteserver operationsreferences/lite_server.mdlite_server get_raw_masterchain_info
Multisig wallets, ordersreferences/multisig.mdmultisig get_account --account-id EQ...
NFT collections, items, historyreferences/nft.mdnft get_collection --account-id EQ...
Purchase historyreferences/purchases.mdpurchases get_purchase_history --account-id EQ...
Token prices, charts, marketsreferences/rates.mdrates get_rates --tokens ton --currencies usd
Staking pools, nominatorsreferences/staking.mdstaking get_pools
TON storage providersreferences/storage.mdstorage get_providers
Execution tracesreferences/traces.mdtraces get_trace --trace-id ...
API status, address parsingreferences/utilities.mdutilities status
Wallet info, seqno, authreferences/wallet.mdwallet get_info --account-id EQ...
Real-time events (SSE/WebSocket)references/streaming.mdstreaming sse --types transactions
Webhooks (push notifications)references/webhooks.md— (generate code)

Runner

Always run the runner from the user's working directory, not from the skill directory. Use ${CLAUDE_SKILL_DIR} to reference the script path:

python3 ${CLAUDE_SKILL_DIR}/scripts/run.py <resource> <method> [--param value ...]
python3 ${CLAUDE_SKILL_DIR}/scripts/run.py streaming <transport> [--types type ...]

Requires pytonapi package installed (pip install pytonapi).

Configuration priority: CLI flags > environment variables > defaults.

SourceAPI KeyNetworkBase URLRPS LimitRPS Period
CLI--api-key KEY--network mainnet|testnet|tetra--base-url URL--rps-limit N--rps-period N
EnvTONAPI_API_KEYTONAPI_NETWORKTONAPI_BASE_URLTONAPI_RPS_LIMITTONAPI_RPS_PERIOD
Default— (optional)mainnetauto by network0 (disabled)1.0

Key Rotation

REST — pass ApiKey objects with per-key rate limits:

from pytonapi.types import ApiKey

TonapiRestClient(api_key=[ApiKey("key-1", rps_limit=5), ApiKey("key-2", rps_limit=10)])

On 429 (after all retries for current key), rotates to the next key with its own rate limiter.

Same-plan keys share combined server-side RPS; different-plan keys have independent limits.

When to Execute vs Generate Code

Execute via runner — user wants concrete data ("balance of X", "show NFT Y"), single API call, no complex logic.

Generate SDK code — user is building an app, streaming/webhook scenarios, multiple calls with conditions, or explicitly asks for code.

Retries

SDK retries 429 (5 attempts, 0.3s base) and 500, 502, 503, 504 (3 attempts, 0.5s base) with exponential backoff. Retries configurable via RetryPolicy or disabled with retry_policy=None.

Error Handling

  • No API key → SDK auto-limits to ~1 request per 4 seconds; inform user a key from tonconsole.com unlocks higher limits
  • 401 → invalid API key, direct user to tonconsole.com
  • 403 → check API key permissions
  • 404 → explain meaning (address not found, transaction not found, etc.)
  • 429 → SDK retries automatically with key rotation if multiple keys provided; if persistent, suggest upgrading plan
  • TONAPIValidationError → response body didn't match Pydantic model; likely API change, report to developer
  • Never guess addresses or parameters — ask the user

Signing and Sending Transactions

The SDK can send a signed BOC (blockchain.send_message), but cannot sign transactions — it has no access to private keys. For building and signing transactions, recommend tonutils (pip install tonutils).

Addresses

  • Accept any format (raw, bounceable, non-bounceable) — TONAPI handles all
  • Don't convert addresses without explicit request

Import Paths

from pytonapi.rest import TonapiRestClient
from pytonapi.types import Network, Workchain, Opcode, ApiKey, RetryPolicy, RetryRule, ReconnectPolicy, DEFAULT_RETRY_POLICY, DEFAULT_RECONNECT_POLICY
from pytonapi.streaming import TonapiSSE, TonapiWebSocket
from pytonapi.streaming import Finality, ConnectionState, EventType, ActionType
from pytonapi.streaming import TransactionsNotification, ActionsNotification, TraceNotification
from pytonapi.streaming import AccountStateNotification, JettonsNotification, TraceInvalidatedNotification
from pytonapi.streaming import AccountState, JettonWallet, StreamNotification
from pytonapi.webhook import TonapiWebhookClient, TonapiWebhookDispatcher, TonapiWebhook
from pytonapi.webhook import WebhookEventType, AccountTxEvent, MempoolMsgEvent, OpcodeMsgEvent, NewContractsEvent
from pytonapi.utils import raw_to_userfriendly, userfriendly_to_raw, to_nano, to_amount
from pytonapi.exceptions import TONAPIError, TONAPIClientError, TONAPINotFoundError, TONAPIValidationError

Never use from pytonapi import ... — the root package has no re-exports.