You can also use Privy to connect a user’s external wallet if they have one, or to create an embedded wallet for them if they do not. This ensures users always have a connected wallet they can use with your application, and allows them to choose to use their external wallet if preferred.

To do so, use the connectOrCreateWallet method of the usePrivy hook:

const {connectOrCreateWallet} = usePrivy();

This method will prompt the user to connect an external wallet, or log in with email, SMS, or socials, depending on your configured loginMethods, to create an embedded wallet.

Privy’s connectOrCreate interface currently only supports external and embedded wallets on EVM networks.

For example, you might have a “Connect” button in your app that prompts users to connect their wallet, like so:

import {useConnectOrCreateWallet} from '@privy-io/react-auth';

export default function ConnectWalletButton() {
  const {connectOrCreateWallet} = useConnectOrCreateWallet();
  // Prompt user to connect a wallet with Privy modal
  return <button onClick={connectOrCreateWallet}>Connect wallet</button>;
}

This method functions exactly the same as Privy’s login method, except when users connect their external wallet, they will not automatically be prompted to authenticate that wallet by signing a message

Callbacks

You can optionally pass callbacks to the useConnectOrCreateWallet hook to run custom logic after connecting a wallet or to handle errors.

onSuccess

onSuccess: (args: {wallet: ConnectedWallet}) => Promise<void>;
Parameters
wallet
ConnectedWallet

The most recently connected wallet.

onError

onError: (error: Error) => Promise<void>;
Parameters
error
Error

The error that occurred during the this flow.