Connect to MetaMask using Dynamic SDK
Get started with MetaMask SDK and Dynamic SDK. You can use MetaMask SDK features directly within Dynamic SDK. You can download the quickstart template or manually set up the SDKs in an existing dapp.
Prerequisites
- Node.js version 19 or later installed.
- A package manager installed, such as npm, Yarn, pnpm, or bun.
- MetaMask installed in your browser or on mobile.
- A Dynamic Environment ID.
Set up using a template
-
Download the MetaMask SDK + Dynamic SDK template:
npx degit MetaMask/metamask-sdk-examples/partners/dynamic metamask-dynamic
-
Navigate into the repository:
cd metamask-dynamic
Degit vs. Git clone
degit
is a tool that enables cloning only the directory structure from a GitHub repository, without retrieving the entire repository.Alternatively, you can use
git clone
, which will download the entire repository. To do so, clone the MetaMask SDK examples repository and navigate into thepartners/dynamic
directory:git clone https://github.com/MetaMask/metamask-sdk-examples
cd metamask-sdk-examples/partners/dynamic -
Install dependencies:
pnpm install
-
Create a
.env.local
file:touch .env.local
-
In
.env.local
, add aNEXT_PUBLIC_DYNAMIC_ENVIRONMENT_ID
environment variable, replacing<YOUR-ENVIRONMENT-ID>
with your Dynamic Environment ID:.env.localNEXT_PUBLIC_DYNAMIC_ENVIRONMENT_ID=<YOUR-ENVIRONMENT-ID>
-
Run the project:
pnpm dev
You've successfully set up MetaMask SDK and Dynamic SDK. See how to use the combined SDKs.
Set up manually
1. Install dependencies
Install the SDK and the required dependencies to an existing project:
- npm
- Yarn
- pnpm
- Bun
npm install @dynamic-labs/sdk-react-core @dynamic-labs/ethereum @dynamic-labs/wagmi-connector wagmi viem @tanstack/react-query
yarn add @dynamic-labs/sdk-react-core @dynamic-labs/ethereum @dynamic-labs/wagmi-connector wagmi viem @tanstack/react-query
pnpm add @dynamic-labs/sdk-react-core @dynamic-labs/ethereum @dynamic-labs/wagmi-connector wagmi viem @tanstack/react-query
bun add @dynamic-labs/sdk-react-core @dynamic-labs/ethereum @dynamic-labs/wagmi-connector wagmi viem @tanstack/react-query
2. Configure providers
Set up your providers in app/providers.tsx
:
"use client";
import { DynamicContextProvider } from "@dynamic-labs/sdk-react-core";
import { EthereumWalletConnectors } from "@dynamic-labs/ethereum";
import { DynamicWagmiConnector } from "@dynamic-labs/wagmi-connector";
import { WagmiProvider } from "wagmi";
import { config } from "@/wagmi.config";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
export function Providers({ children }: { children: React.ReactNode }) {
const queryClient = new QueryClient();
return (
<DynamicContextProvider
settings={{
mobileExperience: "redirect",
environmentId: process.env.NEXT_PUBLIC_DYNAMIC_ENVIRONMENT_ID!,
walletConnectors: [EthereumWalletConnectors],
appName: "MetaMask Dynamic Integration",
}}
>
<WagmiProvider config={config}>
<QueryClientProvider client={queryClient}>
<DynamicWagmiConnector>{children}</DynamicWagmiConnector>
</QueryClientProvider>
</WagmiProvider>
</DynamicContextProvider>
);
}
3. Set up environment variables
Create a .env.local
file.
In .env.local
, add a NEXT_PUBLIC_DYNAMIC_ENVIRONMENT_ID
environment variable, replacing <YOUR-ENVIRONMENT-ID>
with your Dynamic Environment ID:
NEXT_PUBLIC_DYNAMIC_ENVIRONMENT_ID=<YOUR-ENVIRONMENT-ID>
You can now test your dapp by running pnpm run dev
.
Usage
Connect wallet
Use the Dynamic Widget in your components:
"use client";
import { DynamicWidget } from "@dynamic-labs/sdk-react-core";
export const Navbar = () => {
return (
<nav>
<DynamicWidget />
</nav>
);
};
Check wallet status
Use the useAccount
hook from Wagmi:
"use client";
import { useAccount } from "wagmi";
export const Hero = () => {
const { address, isConnected } = useAccount();
return (
<div>
{isConnected ? (
<p>Connected: {address}</p>
) : (
<p>Not connected</p>
)}
</div>
);
};
Production readiness
Before deploying your project to production:
- Update your
next.config.ts
with production domains. - Set up proper environment variables.
- Configure your Dynamic SDK environment ID.
- Ensure MetaMask SDK is properly initialized.
Troubleshoot
Common issues and solutions include:
- SDK initialization error:
- Ensure MetaMask is installed.
- Check environment variables.
- Verify network connectivity.
- TypeScript errors:
- Update type definitions.
- Check SDK versions compatibility.
- Mobile experience issues:
- Test on actual mobile devices.
- Verify redirect URLs.
- Check MetaMask mobile app installation.