- An installed MetaMask browser extension, on desktop or inside the MetaMask in-app browser.
- A MetaMask SDK pairing URI rendered as a QR code on desktop.
- An SDK-managed handoff to MetaMask on mobile web or React Native.
MetaMask SDK pairing is not a generic WalletConnect connection. The MetaMask
functions open MetaMask on mobile. Use their returned URI only when rendering
a desktop QR code.
Prerequisites
- Create and initialize a Dynamic client.
- Enable EVM and/or Solana in the Dynamic Console.
- Install
@dynamic-labs-sdk/evmand/or@dynamic-labs-sdk/solana. - On React Native, complete React Native setup.
Add the extensions
The MetaMask extensions in@dynamic-labs-sdk/evm/metamask and @dynamic-labs-sdk/solana/metamask cover the desktop QR code, the mobile handoff, and connecting EVM and Solana through the MetaMask browser extension in one prompt. The standard chain extensions discover the installed MetaMask browser extension through EIP-6963 (EVM) and Solana Wallet Standard, so add both:
- EVM
- Solana
Connect an installed extension
This route applies when MetaMask runs as a browser extension: a desktop browser with MetaMask installed, or your dapp opened inside the MetaMask in-app browser. On mobile web and React Native without the extension, use the URI routes below instead. Use the chain-specific key helper to detect a MetaMask provider announced through EIP-6963 or Solana Wallet Standard. If the helper returns a key, pass it to the normal wallet provider connection flow.getMetaMaskSolanaExtensionWalletProviderKey from @dynamic-labs-sdk/solana/metamask.
Connect EVM and Solana in one prompt
connectMetaMaskBrowserExtension connects through the MetaMask browser extension using MetaMask’s own SDK. It requests every chain whose MetaMask extension you added, shows one approval prompt, and resolves with a wallet account for each chain MetaMask granted. Add both MetaMask extensions from the previous section first.
verifyWalletAccount for each returned account. See Connecting and Verifying a Wallet for the verification flow.
chains to request only some chains. A later call with other chains adds them to the same MetaMask session, and chains that were already granted stay connected.
addMetaMaskEvmExtension or addMetaMaskSolanaExtension call first, and returns the same { walletAccounts } result.
accountsChanged. When the user removes one chain, that chain emits disconnected and the other stays connected.
Call isMetaMaskBrowserExtensionInstalled before connecting. Without the extension, connectMetaMaskBrowserExtension throws MetaMaskBrowserExtensionNotInstalledError, and you can fall back to the URI routes below.
Connect with a MetaMask SDK URI
The URI functions return{ uri, approval }:
- On desktop, render
urias a QR code for the user to scan with MetaMask. - On mobile web and React Native, the SDK opens MetaMask. You do not need to use
uri. - Call
approval()after starting the connection. It resolves with{ walletAccounts }after the user finishes the wallet interactions.
The connect-only functions add an unverified wallet account by default. Pass
{ addToDynamicWalletAccounts: false } if you only need the returned account. The connect-and-verify functions verify the account before approval() resolves. Users may see separate pairing and verification requests in MetaMask. Each function returns one wallet account for its selected chain.
Connect EVM and Solana with one URI
connectWithMetaMaskUri from @dynamic-labs-sdk/metamask requests every chain whose MetaMask extension you added in a single pairing, so the user scans one QR code to connect EVM and Solana accounts. Add both MetaMask extensions first.
verifyWalletAccount for each returned account. See Connecting and Verifying a Wallet for the verification flow.
chains to request only some chains. Chains that were already granted stay in the pairing, since a new pairing would otherwise drop them.
Desktop QR code
Render the URI with your QR code library. The user must scan the QR code from MetaMask.- EVM
- Solana
Mobile web
Call the MetaMask function from the user’s click or tap handler. The SDK opens MetaMask:React Native
React Native uses the same URI pairing functions. Call the function from the user’s press handler so the SDK can open MetaMask.- Expo
- Bare React Native
metadata.nativeLink and metadata.universalLink on your Dynamic client so MetaMask can return the user to your app. See React Native setup.
Route MetaMask from a wallet picker
CallgetWalletOptionsCatalogue({ includeMobileOptions: true }) to include MetaMask’s mobile and QR routes. A MetaMask entry can contain:
withWalletProviderfor the installed MetaMask browser extension.metamaskSdkUrifor MetaMask SDK desktop QR and mobile pairing.inAppBrowserfor opening your dapp inside MetaMask.
withWalletProvider option is the installed MetaMask browser extension, so connect it through the normal wallet provider flow.
See Build a Wallet Picker for inAppBrowser, installation, and fallback handling.
Common pitfalls
A fresh pairing replaces stale connection state
Each MetaMask URI function clears stale MetaMask pairing state before requesting a new URI. If pairing fails or expires, call the URI function again. Render its new URI only for a desktop QR code. Do not cache and reuse a previous URI.User rejection and timeouts
connectMetaMaskBrowserExtension also throws MetaMaskUnexpectedTransportError if MetaMask connects through something other than the browser extension. Treat it as a failed connection and let the user retry.
The initial URI function or approval() can reject if the user cancels a wallet interaction. The URI function also rejects with MetaMaskDisplayUriMissingError if MetaMask does not provide a pairing URI, or NoAddressFoundError if the approved session has no account.
Keep the connection UI available so the user can retry, but allow only one attempt at a time. Disable retry while the URI request or approval() is pending.
MetaMask transport timeouts are not automatically retried. A timed-out signing or transaction request may still be pending in MetaMask. Avoid immediately sending the same fund-moving request again.
Related
- Build a Wallet Picker shows how to handle every connection option.
- Get Wallet Options Catalogue documents the wallet and connection option types.
- Authenticate with External Wallets covers installed wallet providers.
- Authenticate with WalletConnect covers non-MetaMask WalletConnect flows.