Embedded Wallets SDK for React
Overview
MetaMask Embedded Wallets SDK (formerly Web3Auth Plug and Play) provides a seamless authentication experience for React applications with social logins, external wallets, and more. React hooks simplify how you connect users to their preferred wallets and manage authentication state.
For a complete working project, see the React quick start example on GitHub.
Requirements
- This is a frontend SDK and must be used in a browser environment.
- Node.js 20+ and npm (or yarn).
- Basic knowledge of JavaScript and React.
Prerequisites
- Set up your project on the Embedded Wallets dashboard
See the dashboard setup guide to learn more.
Installation
Install the Web3Auth Modal SDK and Wagmi dependencies:
- npm
- Yarn
- pnpm
- Bun
npm install --save @web3auth/modal wagmi@3 @tanstack/react-query
yarn add @web3auth/modal wagmi@3 @tanstack/react-query
pnpm add @web3auth/modal wagmi@3 @tanstack/react-query
bun add @web3auth/modal wagmi@3 @tanstack/react-query
1. Configuration
Create a configuration file with your Client ID and Sapphire network from the Embedded Wallets dashboard.
Use Sapphire Devnet for local development (http://localhost). Sapphire Mainnet does not allow
localhost origins.
import { type Web3AuthContextConfig } from '@web3auth/modal/react'
import { WEB3AUTH_NETWORK } from '@web3auth/modal'
const clientId = import.meta.env.VITE_WEB3AUTH_CLIENT_ID // or your env prefix
const web3AuthContextConfig: Web3AuthContextConfig = {
web3AuthOptions: {
clientId,
web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_DEVNET, // use SAPPHIRE_MAINNET in production
authBuildEnv: 'production',
},
}
export default web3AuthContextConfig
2. Set up providers
In your main entry file (main.tsx or index.tsx), wrap your app with Web3AuthProvider,
QueryClientProvider, and the Embedded Wallets WagmiProvider:
import './index.css'
import ReactDOM from 'react-dom/client'
import { Web3AuthProvider } from '@web3auth/modal/react'
import { WagmiProvider } from '@web3auth/modal/react/wagmi'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import web3AuthContextConfig from './web3authContext'
import App from './App'
const queryClient = new QueryClient()
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<Web3AuthProvider config={web3AuthContextConfig}>
<QueryClientProvider client={queryClient}>
<WagmiProvider>
<App />
</WagmiProvider>
</QueryClientProvider>
</Web3AuthProvider>
)
3. Connect users
Use useWeb3AuthConnect to open the pre-built modal or connect to a
specific login method:
import { useWeb3AuthConnect, useWeb3AuthDisconnect } from '@web3auth/modal/react'
import { useAccount } from 'wagmi'
function App() {
const { connect, loading, isConnected, error } = useWeb3AuthConnect()
const { disconnect } = useWeb3AuthDisconnect()
const { address } = useAccount()
if (!isConnected) {
return (
<button onClick={() => connect()} disabled={loading}>
{loading ? 'Connecting...' : 'Login'}
</button>
)
}
return (
<div>
<p>{address}</p>
<button onClick={() => disconnect()}>Log out</button>
</div>
)
}
Advanced configuration
The Web3Auth Modal SDK offers a rich set of advanced configuration options:
- Smart accounts: Configure account abstraction parameters.
- Custom authentication: Define authentication methods.
- Whitelabeling and UI customization: Personalize the modal's appearance.
- Multi-Factor Authentication (MFA): Set up and manage MFA.
- Wallet Services: Integrate additional Wallet Services.
See the advanced configuration section to learn more about each configuration option.
- Basic Configuration
- Advanced Configuration
import { type Web3AuthContextConfig } from '@web3auth/modal/react'
import { WEB3AUTH_NETWORK } from '@web3auth/modal'
const web3AuthContextConfig: Web3AuthContextConfig = {
web3AuthOptions: {
clientId: import.meta.env.VITE_WEB3AUTH_CLIENT_ID,
web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_DEVNET,
authBuildEnv: 'production',
},
}
import { type Web3AuthContextConfig } from '@web3auth/modal/react'
import {
WALLET_CONNECTORS,
WEB3AUTH_NETWORK,
MFA_LEVELS,
type Web3AuthOptions,
} from '@web3auth/modal'
const web3AuthOptions: Web3AuthOptions = {
clientId: import.meta.env.VITE_WEB3AUTH_CLIENT_ID,
web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET,
authBuildEnv: 'production',
modalConfig: {
connectors: {
[WALLET_CONNECTORS.AUTH]: {
label: 'auth',
loginMethods: {
google: {
name: 'google login',
// logoDark: "url to your custom logo which will shown in dark mode",
},
facebook: {
name: 'facebook login',
showOnModal: false, // hides the facebook option
},
email_passwordless: {
name: 'email passwordless login',
showOnModal: true,
authConnectionId: 'w3a-email_passwordless-demo',
},
sms_passwordless: {
name: 'sms passwordless login',
showOnModal: true,
authConnectionId: 'w3a-sms_passwordless-demo',
},
},
showOnModal: true, // set to false to hide all social login methods
},
},
hideWalletDiscovery: true, // set to true to hide external wallets discovery
},
mfaLevel: MFA_LEVELS.MANDATORY,
}
const web3AuthContextConfig: Web3AuthContextConfig = {
web3AuthOptions,
}
Blockchain integration
Web3Auth is blockchain agnostic, enabling integration with any blockchain network. Out of the box, Web3Auth offers robust support for both Solana and Ethereum, each with dedicated React hooks.
Solana integration
Solana hooks are included natively within the @web3auth/modal package. Install
@solana/kit and use hooks from
@web3auth/modal/react/solana.
For detailed usage and examples, see the Solana hooks section.
Ethereum integration
Ethereum wallet operations use Wagmi hooks after the provider setup above. Configure EVM chains in the Embedded Wallets dashboard; no in-app chain list is required for standard integrations.
For implementation details and examples, refer to the Ethereum hooks section.
Troubleshooting
JWT errors
When using custom authentication, you may encounter JWT errors: