Client
| Parameter | Default | Description |
|---|---|---|
api_key | Required | API key from tonconsole.com. |
network | Required | Mainnet or Testnet. |
base_url | Optional | Override the default URL. |
timeout | 10 | Request timeout in seconds. |
session | Optional | External HTTP session to share across clients. |
headers | Optional | Extra HTTP headers. |
cookies | Optional | Extra cookies. |
retry_policy | Enabled | Retry policy, or None to disable. |
Session
- Context manager
- Manual
Managing Webhooks
Subscriptions
A boundTonapiWebhook manages subscriptions without requiring an explicit webhook ID.
| Property | Type | Description |
|---|---|---|
id | int | Webhook identifier. |
endpoint | str | Callback URL. |
token | str | Secret token sent as Authorization: Bearer {token} — use for verifying incoming requests. |
Account transactions
Other subscriptions
Deleting a webhook
Event Handling
The dispatcher manages handler registration, webhook lifecycle, and event routing.| Parameter | Default | Description |
|---|---|---|
url | "" | Public webhook URL prefix. |
client | Optional | TonapiWebhookClient (required for setup/teardown). |
accounts | Optional | Account IDs to subscribe. |
opcodes | Optional | Opcodes for opcode_msg subscriptions. |
**kwargs | — | Default dependencies injected into handler calls. |
Registering handlers
account_tx, opcode_msg, new_contracts accept *accounts for local filtering. All accept path for custom URL paths. Default paths: /account-tx, /mempool-msg, /opcode-msg, /new-contracts.
Without decorators:
Lifecycle
setup() opens a session, creates webhooks per event type, subscribes, and stores tokens.
teardown(cleanup) closes the session. When cleanup=True, unsubscribes from all events first. Subscriptions persist across restarts — on next setup(), existing webhooks are reused via ensure().
Processing events
Dependencies
Pass default dependencies via**kwargs in the constructor. Handlers receive only parameters they declare in their signature:
process() take priority over constructor defaults. Handlers can be async or sync — the dispatcher awaits async results automatically.
Models
WebhookInfo
WebhookInfo
| Field | Type | Description |
|---|---|---|
id | int | Webhook ID. |
endpoint | str | Callback URL. |
token | str | Secret token for Authorization header. |
subscribed_accounts | int | Number of subscribed accounts. |
subscribed_msg_opcodes | int | Number of subscribed opcodes. |
subscribed_to_mempool | bool | Mempool subscription active. |
subscribed_to_new_contracts | bool | New contracts subscription active. |
status | str | Current webhook status. |
status_updated_at | str | Last status update timestamp. |
last_online_at | str | Last online timestamp. |
status_failed_attempts | int | Consecutive failed delivery attempts. |
AccountSubscription
AccountSubscription
| Field | Type | Description |
|---|---|---|
account_id | str | Subscribed account address. |
last_delivered_lt | int | Last delivered logical time. |
failed_at | str | None | Timestamp of last failure. |
failed_lt | int | None | Logical time of failed delivery. |
failed_attempts | int | None | Number of failed delivery attempts. |