Перейти к основному содержимому

TON Connect for JS

This guide will help you integrate TON Connect into your Javascript application for user authentication and transactions.

If you use React for your DApp, take a look at TON Connect UI React SDK.

Implementation

1) Installation

Add script in the HEAD element of your website:

<script src="https://unpkg.com/@tonconnect/ui@latest/dist/tonconnect-ui.min.js"></script>

2) TON Connect Initiation

After installing the package, you should create a manifest.json file for your application. More information on how to create a manifest.json file can be found here.

Add a button with ton-connect id to connect to the wallet:

<div id="ton-connect"></div>

After this tag add a script for tonConnectUI in <body> part of application page:

<script>
const tonConnectUI = new TON_CONNECT_UI.TonConnectUI({
manifestUrl: 'https://<YOUR_APP_URL>/tonconnect-manifest.json',
buttonRootId: 'ton-connect'
});
</script>

3) Connect to the Wallet

"Connect" button (which is added at buttonRootId) automatically handles clicks.

But you can open "connect modal" programmatically, e.g. after click on custom button:

<script>
async function connectToWallet() {
const connectedWallet = await tonConnectUI.connectWallet();
// Do something with connectedWallet if needed
console.log(connectedWallet);
}

// Call the function
connectToWallet().catch(error => {
console.error("Error connecting to wallet:", error);
});
</script>

4) Redirects

Customizing return strategy

To redirect user to a specific URL after connection, you can customize your return strategy.

Telegram Mini Apps

To redirect user to a Telegram Mini App after wallet connection use twaReturnUrl option:

tonConnectUI.uiOptions = {
twaReturnUrl: 'https://t.me/YOUR_APP_NAME'
};

Read more in SDK documentation

5) UI customization

TonConnect UI provides an interface that should be familiar and recognizable to the user when using various apps. However, the app developer can make changes to this interface to keep it consistent with the app interface.

SDK Documentation

Usage

Let's take a look at the example of using the TON Connect UI in the application.

Sending messages

Here is an example of sending a transaction using the TON Connect UI:

import TonConnectUI from '@tonconnect/ui';

const tonConnectUI = new TonConnectUI({ //connect application
manifestUrl: 'https://<YOUR_APP_URL>/tonconnect-manifest.json',
buttonRootId: '<YOUR_CONNECT_BUTTON_ANCHOR_ID>'
});

const transaction = {
messages: [
{
address: "0:412410771DA82CBA306A55FA9E0D43C9D245E38133CB58F1457DFB8D5CD8892F", // destination address
amount: "20000000" //Toncoin in nanotons
}
]
}

const result = await tonConnectUI.sendTransaction(transaction)

Understanding Transaction Status by Hash

The principle located in Payment Processing (using tonweb). See more

Signing and Verification

Understand how to sign and verify messages using the TON Connect:

Wallet Disconnection

Call to disconnect the wallet:

await tonConnectUI.disconnect();

See Also