Add the install Metamask button on your application(OPTIONAL)
It's optional but recommended to automate the MetaMask installation for users, as this enhances their experience. The choice is up to developers on how they want to handle this aspect of the integration.
asyncfunctionisMetamaskInstalled() {// Check if the `window.ethereum` property exists on the window objectreturntypeofwindow.ethereum !=='undefined'&&window.ethereum.isMetaMask;}
Check out Snap RPC APIs to learn about how to interact with the Identify Snap from your website.
Add the Connect to Identify Snap button on your application
Note that before users can interact with the Identify Snap, it needs to first be installed on the Metamask itself so you can have a button on your website that lets users do exactly this.
// Get permissions to interact with and install the Identify Snapasyncfunctionconnect() {console.log("snap id", snapId);constisMetamaskDetected=awaitisMetamaskInstalled();if (!isMetamaskDetected ) {console.error("You need to install Metamask for this demo to work. You can install it at https://metamask.io" );alert("You need to install Metamask for this demo to work. You can install it at https://metamask.io" ); }let snaps =awaitwindow.ethereum.request({ method:"wallet_getSnaps" });console.log("Installed snaps...", snaps);try {if (!(snapId in snaps)) {console.log("Identify Snap is not yet installed. Installing now...");constresult=awaitethereum.request({ method:"wallet_requestSnaps", params: { [snapId]: { version:"latest" } } });console.log("result: ", result); snaps =awaitwindow.ethereum.request({ method:"wallet_getSnaps" }); } } catch (e) {console.log(`Failed to obtain installed snap: ${JSON.stringify(e,null,4)}` );alert(`Failed to obtain installed snap: ${JSON.stringify(e,null,4)}`); }if (snapId in snaps) {console.log("Connected successfully!");alert("Connected successfully!"); } else {console.log("Could not connect successfully. Please try again!");alert("Could not connect successfully. Please try again!"); }}
How the Identify Snap APIs work
Live Demo on CodePen
Some things to keep in mind while interacting with the above demo
If you're getting any errors with the live demo, make sure you go through the FAQs section to learn about what you may be missing. You need to install Metamask in your browser for the live demo to work
Whenever there is a new version of the IdentifySnap, always make sure to first uninstall the old version of the snap from Metamask and only then try the above demo so it can install the latest version