Skip to main content

Use MetaMask SDK with JavaScript

You can import MetaMask SDK into your JavaScript dapp to enable your users to easily connect with a MetaMask wallet client. The following instructions work for web dapps based on standard JavaScript. You can also see instructions for the following JavaScript-based platforms:

How it works

When a user accesses your JavaScript web dapp on a desktop browser and doesn't have the MetaMask extension installed, a popup appears that prompts the user to either install the MetaMask extension or connect to MetaMask Mobile using a QR code.

SDK desktop browser example

You can try the hosted test dapp with the SDK installed. You can also download the React project example. Install the example using yarn and run it using yarn start.

Prerequisites

Steps

1. Install the SDK

In your project directory, install the SDK using Yarn or npm:

yarn add @metamask/sdk
or
npm i @metamask/sdk

2. Import the SDK

In your project script, add the following to import the SDK:

import MetaMaskSDK from '@metamask/sdk';

3. Instantiate the SDK

Instantiate the SDK using any options:

const MMSDK = new MetaMaskSDK(options);

const ethereum = MMSDK.getProvider(); // You can also access via window.ethereum

4. Use the SDK

Use the SDK by calling any provider API methods. Always call eth_requestAccounts using ethereum.request() first, since it prompts the installation or connection popup to appear.

ethereum.request({ method: 'eth_requestAccounts', params: [] });