Connect to MetaMask

Learn how to implement the connect to MetaMask feature in a web application.

Before we can read blockchain data or send transactions to our smart contract, we need to connect our application to MetaMask. If this process succeeds, our application will be able to get the user’s account address and send transactions signed with the user’s private key to the Ethereum network.

This is exactly what we'll implement in this lesson, and this will be the last hurdle before we can access the Auction smart contract from the web application.

Connecting to MetaMask

The process of connecting to MetaMask is initiated by a web application. To initiate a connection flow, we need to call requestAccounts from the web3.js library:

Press + to interact
const accounts = await web3.eth.requestAccounts()

If our application is not yet connected to MetaMask and we call this method, MetaMask will display a window that asks a user if they want to connect our application to MetaMask. If they approve, our application will be able to get the user’s current account and then use our application.

Press + to interact
Selecting an account to connect with MetaMask
Selecting an account to connect with MetaMask

If the user approves ...