@mysten/sui v2.0 and a new dApp Kit are here! Check out the migration guide
Mysten Labs SDKs
Actions

Connect Wallet

The dApp Kit SDK provides an action for wallet connection, allowing users to connect, disconnect, and switch between wallets and accounts.

The Connect Button Web Component provides a complete wallet connection UI.

Usage

Auto-connect is enabled by default and will attempt to restore the previous wallet connection on page reload. This provides a seamless user experience but can be disabled if needed.

The connectWallet action prompts a wallet to connect and authorize accounts for your application:

import { createDAppKit } from '@mysten/dapp-kit-core';

const dAppKit = createDAppKit({
	/* config */
});

// Connect to a specific wallet
const result = await dAppKit.connectWallet({
	wallet: myWallet, // UiWallet instance
	account: myAccount, // Optional: specific account to select
});

console.log('Connected accounts:', result.accounts);

Parameters

  • wallet - The UiWallet instance to connect to
  • account (optional) - A specific UiWalletAccount to set as the selected account. Defaults to the first authorized account

Return Value

Returns a Promise that resolves to an object containing:

  • accounts - Array of authorized UiWalletAccount instances

Example

const wallets = dAppKit.stores.$wallets.get();

const unsubscribe = dAppKit.stores.$connection.subscribe((connection) => {
	if (connection.isConnected) {
		console.log(`Connected account address: ${connection.account.address}`);
	} else {
		console.log({ connection });
	}
});

if (wallets.length > 0) {
	try {
		const { accounts } = await dAppKit.connectWallet({
			wallet: wallets[0],
		});
		console.log('Available accounts:', accounts);
	} catch (error) {
		console.error('Connection failed:', error);
	}
}

On this page