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 singleEvent 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.
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: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: theCONNECT 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.