Skip to main content
Besides awaiting results inline with wait_connect() / wait_transaction() / wait_sign_data(), the library can dispatch results to registered handlers — the natural style for long-lived services such as Telegram bots, where many users hold sessions concurrently. Both styles work at the same time: every result resolves the pending wait_* call and dispatches the corresponding event.

Event Types

Events are members of a single Event enum:
There is no separate error-event enum. Errors that belong to a specific action are delivered as the error argument of that action’s handler (CONNECT, DISCONNECT, TRANSACTION, SIGN_DATA); Event.ERROR only receives errors that cannot be attributed to a pending request.

Registering Handlers

Register a handler with the @tc.on(...) decorator or the tc.register(...) method. One handler is stored per event — registering again replaces the previous handler.
Register all handlers before calling tc.create_connector(...). Handlers are copied into each connector at creation time, so handlers registered afterwards do not apply to connectors that already exist.
Each event has a fixed handler signature. The first argument is always the Connector that dispatched the event:
SendTransactionResult, SignDataResult, and WalletMessage are importable from ton_connect.models. For TRANSACTION and SIGN_DATA, exactly one of result and error is set.
An exception raised inside a handler is caught, wrapped in TonConnectError if needed, and dispatched to the Event.ERROR handler. Exceptions raised inside the ERROR handler itself are swallowed.

Passing Context

Handlers receive only the connector and event data, so application state (database sessions, user objects) is attached as context. Context is a plain key-value store, readable and writable through item access. Global context — shared by all connectors:
Per-connector context — merged on top of the global context at creation:
Inside a handler, read context from the connector:
Connectors also support item assignment (connector["key"] = value) to update their own context. Accessing a missing key raises KeyError.

Connection Lifecycle

resume_connect and resume_request make pending requests restart-safe: persist the request_id, event type, and deadline before your process exits, then re-create the connector, call await connector.restore(), and resume waiting.

Complete Example

A minimal event-driven flow: the CONNECT handler sends a transaction as soon as the wallet connects, the TRANSACTION handler reports the result, and the ERROR handler logs anything unattributed.

TON Connect Examples

Full runnable scripts for all TON Connect flows on GitHub.