TON Connect for Python
The recommended SDK for Python applications is tonutils. It is a Python library that provides a high-level interface for interacting with the TON blockchain, including TON Connect.
Implementation
Installation
pip install tonutils
To store user wallet connection data, you need to implement a storage interface. An additional dependency is required for this. In this example, a file-based implementation using aiofiles is used:
pip install aiofiles
Creating the manifest
Create the tonconnect-manifest.json file for your application according to the guidelines, and host it at a publicly accessible URL.
Implementing storage
To store connection data with user wallets, a storage interface must be implemented.
Save the following example as storage.py so that from storage import FileStorage works as shown below.
Example implementation
import json
import os
from asyncio import Lock
from typing import Dict, Optional
import aiofiles
from tonutils.tonconnect import IStorage
class FileStorage(IStorage):
def __init__(self, file_path: str):
self.file_path = file_path
self.lock = Lock()
if not os.path.exists(self.file_path):
with open(self.file_path, "w") as f:
json.dump({}, f) # type: ignore
async def _read_data(self) -> Dict[str, str]:
async with self.lock:
async with aiofiles.open(self.file_path, "r") as f:
content = await f.read()
if content:
return json.loads(content)
return {}
async def _write_data(self, data: Dict[str, str]) -> None:
async with self.lock:
async with aiofiles.open(self.file_path, "w") as f:
await f.write(json.dumps(data, indent=4))
async def set_item(self, key: str, value: str) -> None:
data = await self._read_data()
data[key] = value
await self._write_data(data)
async def get_item(self, key: str, default_value: Optional[str] = None) -> Optional[str]:
data = await self._read_data()
return data.get(key, default_value)
async def remove_item(self, key: str) -> None:
data = await self._read_data()
if key in data:
del data[key]
await self._write_data(data)
Initializing TON Connect
Create a TonConnect instance by specifying the manifest URL and the storage object:
from storage import FileStorage
from tonutils.tonconnect import TonConnect
TC_MANIFEST_URL = "https://raw.githubusercontent.com/nessshon/tonutils/main/examples/tonconnect/tonconnect-manifest.json"
TC_STORAGE = FileStorage("connection.json")
tc = TonConnect(
storage=TC_STORAGE,
manifest_url=TC_MANIFEST_URL,
wallets_fallback_file_path="./wallets.json"
)
The wallets_fallback_file_path parameter is used as a fallback source of wallet data (e.g., Tonkeeper, Wallet) in case the external API is unavailable.
You can find all input parameters at the following link.
Initializing the connector
Create a Connector instance to work with a specific user:
connector = await tc.init_connector(user_id)
-
user_idcan be an integer (int) or a string (str). -
If not provided, an incremental ID will be assigned automatically.
-
You can store the user ID for future reference:
user_id = connector.user_id
Connect to a wallet
Retrieve the list of wallets
To display available wallets in the user interface, retrieve the list of supported wallets:
wallets = await tc.get_wallets()
You can then display the list using each wallet’s name via wallet.name.