{"id":9195,"date":"2025-07-26T13:48:44","date_gmt":"2025-07-26T08:18:44","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=9195"},"modified":"2025-07-26T13:48:50","modified_gmt":"2025-07-26T08:18:50","slug":"connect-metamask-to-react-using-ethersjs","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/connect-metamask-to-react-using-ethersjs\/","title":{"rendered":"Using Ethers v6 library connect Metamask\u00a0 to React Application"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Metamask is a well-known browser extension that allows users to interact with the Ethereum blockchain effortlessly. In this step-by-step guide, we&#8217;ll explore how to connect Metamask to\u00a0 React website using the ethers.js library<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this tutorial, you&#8217;ll be able to<br>\u2705 verify whether Metamask is installed<br>\u2705 connect to users&#8217; wallets and fetch the user details\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;Step 1: Install the Ethers Library<br>&nbsp; The Ethers.js library must be installed in the React project. Either yarn or npm can be used:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>#Using npm<br>npm install ethers<br><br>#Using yarn<br>yarn add ethers<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><br><br>Step 2: Checking Metamask Installation&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The first step the metamask extension is installed or not in the chrome brower or any other brower In case it&#8217;s not installed, we&#8217;ll prompt the user to install Metamask for a smooth experience.<br><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>const<\/strong> checkMetamaskInstallation = () =&gt; {<br>&nbsp; &nbsp; <strong>if<\/strong> (window.ethereum == undefined) {<br>&nbsp; &nbsp; &nbsp; &nbsp; alert(&#8220;Metamask wallet is not installed&#8221;);<br>&nbsp; &nbsp; &nbsp; &nbsp; <strong>return<\/strong>;<br>&nbsp; &nbsp; }<br>};<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u00a0The window.ethereum object is available, which is injected by MetaMask.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Step 3 :\u00a0 Initiating Wallet Connection and fetch the user details\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once the Metamask is installed, the next step is to establish a connection between the user&#8217;s wallet and our metamask . With the connection established, we&#8217;ll have the user&#8217;s account address available for further interactions.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>const<\/strong> [account, setAccount] = useState&lt;string | null&gt;(null);<br><strong>const<\/strong> [balance, setBalance] = useState&lt;string | null&gt;(null);<br><strong>const<\/strong> connectToMetaMask = <strong>async<\/strong> () =&gt; {<br>&nbsp; setIsLoading(true);<br>&nbsp; <strong>try<\/strong> {<br>&nbsp; &nbsp; <strong>if<\/strong> (window.ethereum?.isMetaMask) {<br>&nbsp; &nbsp; &nbsp; <strong>const<\/strong> provider = <strong>new<\/strong> ethers.BrowserProvider(window.ethereum);<br>&nbsp; &nbsp; &nbsp; <strong>const<\/strong> accounts = <strong>await<\/strong> provider.send(&#8220;eth_requestAccounts&#8221;, []);<br>&nbsp; &nbsp; &nbsp; <strong>const<\/strong> account = accounts[0];<br>&nbsp; &nbsp; &nbsp; setAccount(account);<br><br>&nbsp; &nbsp; &nbsp; <strong>const<\/strong> balance = <strong>await<\/strong> provider.getBalance(account);<br>&nbsp; &nbsp; &nbsp; <strong>const<\/strong> ethBalance = ethers.formatEther(balance);<br>&nbsp; &nbsp; &nbsp; setBalance(ethBalance);<br>&nbsp; &nbsp; &nbsp; setErrorMessage(&#8220;&#8221;);<br>&nbsp; &nbsp; } <strong>else<\/strong> {<br>&nbsp; &nbsp; &nbsp; setErrorMessage(&#8220;Please install MetaMask extension!&#8221;);<br>&nbsp; &nbsp; &nbsp; setSnackbarOpen(true);<br>&nbsp; &nbsp; }<br>&nbsp; } <strong>catch<\/strong> (error: any) {<br>&nbsp; &nbsp; setErrorMessage(`Error connecting to MetaMask: ${error.message}`);<br>&nbsp; &nbsp; setSnackbarOpen(true);<br>&nbsp; } <strong>finally<\/strong> {<br>&nbsp; &nbsp; setIsLoading(false);<br>&nbsp; }<br>};<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">How it operates:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>We check window.ethereum?.isMetaMask to confirm MetaMask&#8217;s availability.<\/li>\n\n\n\n<li>ethers.BrowserProvider(window.ethereum) is used to create a provider.<\/li>\n\n\n\n<li>eth_requestAccounts is used to request account access.<\/li>\n\n\n\n<li>Ethers are used to retrieve the ETH balance and account address.<\/li>\n\n\n\n<li>ethers.formatEther() is used to convert the balance from Wei to ETH.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">UI Implementation Example<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To connect MetaMask, we can add a straightforward button to the React component:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>&lt;button onClick={connectToMetaMask}&gt;Connect Wallet&lt;\/button&gt;<br>{account &amp;&amp; &lt;p&gt;Account Connected:&nbsp; {account}&lt;\/p&gt;}<br>{balance &amp;&amp; &lt;p&gt;Balance: {balance} ETH&lt;\/p&gt;}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Metamask is a well-known browser extension that allows users to interact with the Ethereum blockchain effortlessly. In this step-by-step guide, [&hellip;]<\/p>\n","protected":false},"author":507,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[],"class_list":["post-9195","post","type-post","status-publish","format-standard","hentry","category-web-architecture","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-2oj","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/9195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/507"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=9195"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/9195\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=9195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=9195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=9195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}