SDK Integration for TON: Connect Apps or Mini Wallets to Web3

·

Integrating decentralized applications (DApps) with blockchain wallets is a critical step in delivering seamless Web3 experiences. This guide provides a comprehensive walkthrough of how to use the OKX Ton Connect SDK to integrate your application or mini wallet with the TON (The Open Network) ecosystem. Whether you're building on mobile, desktop, or within Telegram, this documentation ensures smooth connectivity, transaction handling, and real-time state management.

Designed for developers familiar with Ton Connect, this SDK reduces development overhead while supporting multi-network requests—making it ideal for scalable DApp architectures.

Core Keywords


Installing the SDK

You can install the OKX Ton Connect SDK via CDN or npm, depending on your project setup.

Using CDN

To integrate via CDN, add the following script tag to your HTML file:

<script src="https://cdn.okx.com/sdk/okx-ton-connect-sdk/latest.js"></script>

You may replace latest with a specific version number (e.g., 1.6.1) for better control over updates.

Once loaded, the SDK becomes available as a global object:

const connector = new OKXTonConnectSDK.OKXTonConnect();

This method is ideal for quick prototyping or frontend-only projects where package managers aren't used.

Using npm

For modern JavaScript environments, install the SDK using npm:

npm install @okxweb3/ton-connect-sdk

Then import it into your project:

import { OKXTonConnect } from '@okxweb3/ton-connect-sdk';

👉 Get started with secure, high-performance Web3 integration today.


Initializing the SDK

Before connecting to a wallet, initialize the SDK with metadata about your application:

const okxTonConnect = new OKXTonConnect({
  metaData: {
    name: 'My DApp',
    icon: 'https://mydapp.com/icon.png' // 180x180px PNG recommended
  }
});

Parameters

This initialization sets up the foundation for all subsequent interactions, including connection, transaction signing, and event listening.


Connecting a Wallet

To authenticate users and access their wallet address, use the connect() method:

const universalLink = await okxTonConnect.connect({
  tonProof: 'Sign to verify ownership',
  redirect: 'tg://resolve?domain=mybot',
  openUniversalLink: true
});

Request Parameters

Return Value

Returns a Promise resolving to a universal link. On desktop, generate a QR code from this link for mobile scanning.

Best Practices

👉 Unlock full Web3 functionality with seamless wallet onboarding.


Restoring a Previous Connection

If a user has connected before, restore their session without re-prompting:

okxTonConnect.restoreConnection();

This method checks for active sessions and reconnects silently, improving user experience during page refreshes or app relaunches.

No parameters or return values—just call it during initialization.


Disconnecting the Wallet

To terminate the current session:

okxTonConnect.disconnect();

Use this when users log out or switch accounts. It clears local session data and notifies the connected wallet.

Always disconnect before attempting a new connection to avoid conflicts.


Checking Connection Status

Verify whether a wallet is currently connected:

if (okxTonConnect.connected) {
  console.log('Wallet is connected');
}

Alternatively, use getAccount() or getWallet() methods to retrieve active data and infer connection status.


Sending Transactions

Send transactions securely through the user’s wallet:

const result = await okxTonConnect.sendTransaction({
  validUntil: Math.floor(Date.now() / 1000) + 600,
  messages: [
    {
      address: 'UQD...',
      amount: '100000000',
      payload: 'base64-encoded-boc'
    }
  ]
});

Transaction Object

Returns

A Promise resolving to { boc: "signed-transaction-in-base64" }.


Monitoring Wallet State Changes

Listen for real-time updates in connection status:

const unsubscribe = okxTonConnect.onStatusChange(
  (walletInfo) => {
    console.log('Connected:', walletInfo);
  },
  (error) => {
    console.error('Connection error:', error);
  }
);

Callback Data (walletInfo)

Includes:

Call unsubscribe() when no longer needed to prevent memory leaks.


Listening to Events

Subscribe to specific lifecycle events:

window.addEventListener('OKX_TON_CONNECTION_COMPLETED', () => {
  console.log('User successfully connected wallet');
});

Available Events

These help track user behavior and trigger UI updates accordingly.


Retrieving Account and Wallet Information

Fetch current account:

const account = okxTonConnect.getAccount();

Get wallet details:

const wallet = okxTonConnect.getWallet();

Both return null if not connected. Use them post-connection to personalize user interfaces or validate permissions.


Error Handling

Common errors include:

CodeDescription
UNKNOWN_ERRORUnexpected internal failure
ALREADY_CONNECTED_ERRORAttempted duplicate connection
NOT_CONNECTED_ERROROperation requires active connection
USER_REJECTS_ERRORUser denied request
METHOD_NOT_SUPPORTEDFeature unsupported by wallet
CONNECTION_ERRORNetwork or protocol issue

Handle these gracefully in your UI to maintain trust and usability.


Frequently Asked Questions

Q: Can I use this SDK outside of Telegram?
A: Yes. While optimized for Telegram mini-apps, it works across web, mobile browsers, and native apps using deep links or QR codes.

Q: Is user privacy protected during connection?
A: Absolutely. The SDK uses non-custodial authentication—your app never accesses private keys. All signatures occur within the user’s secure wallet environment.

Q: What networks does this support?
A: Primarily The Open Network (TON), with multi-chain support available via OKX Connect’s extended ProviderSDK.

Q: How do I debug connection issues?
A: Enable logging in development mode and listen to error events like OKX_TON_CONNECTION_ERROR. Check metadata formatting and network reachability.

Q: Can I customize the connection flow?
A: Limited customization is allowed via metadata (name/icon). The actual UI is managed by the OKX wallet for security consistency.

Q: Are there rate limits or usage fees?
A: No. The SDK is free to use with no imposed rate limits, making it suitable for high-traffic DApps.

👉 Enhance your DApp with powerful tools and real-time blockchain access.