Web3 Provider

If your dApp is written using web3 and already works with MetaMask, it's super easy to integrate with Fortmatic. All you have to do is to swap your existing web3 provider with the Fortmatic provider and continue developing in the same version of web3 you use.

Replace Web3 Provider

import Fortmatic from 'fortmatic';
// Works for web3 1.0 and pre-1.0 versions
import Web3 from 'web3';
const fm = new Fortmatic('YOUR_API_KEY');
window.web3 = new Web3(fm.getProvider());

Switch Network on Testnet

On Testnet, you can switch between Rinkeby, Ropsten, and Kovan. Default is Rinkeby.

import Fortmatic from 'fortmatic';
import Web3 from 'web3';
// Default is Rinkeby
const fm = new Fortmatic('YOUR_TEST_API_KEY');
window.web3 = new Web3(fm.getProvider());
// Setting testnet network to Ropsten
const fm = new Fortmatic('YOUR_TEST_API_KEY', 'ropsten');
window.web3 = new Web3(fm.getProvider());
// Setting testnet network to Kovan
const fm = new Fortmatic('YOUR_TEST_API_KEY', 'kovan');
window.web3 = new Web3(fm.getProvider());

MetaMask or dApp Browsers

You can also let existing users who have MetaMask installed to continue using MetaMask without disruption and direct new users without MetaMask to Fortmatic. This method also works with legacy dApp browsers.

import Fortmatic from 'fortmatic';
import Web3 from 'web3';
const fm = new Fortmatic('YOUR_API_KEY');
// Post EIP-1102 update which MetaMask no longer injects web3
if (window.ethereum) {
// Use MetaMask provider
window.web3 = new Web3(window.ethereum);
} else {
// Use Fortmatic provider
window.web3 = new Web3(fm.getProvider());
}
// Legacy dApp browsers which web3 is still being injected
if (typeof web3 !== 'undefined') {
// Use injected provider
window.web3 = new Web3(web3.currentProvider);
} else {
// Use Fortmatic provider
window.web3 = new Web3(fm.getProvider());
}

Detecting the Fortmatic Provider

As a convenience, you can detect the Fortmatic web3 provider by checking for an isFortmatic property on the provider object. The following lines of code should evaluate to true when using the Fortmatic provider:

import Fortmatic from 'fortmatic';
import Web3 from 'web3';
const fm = new Fortmatic('YOUR_API_KEY');
fm.getProvider().isFortmatic // => true
window.web3 = new Web3(fm.getProvider());
window.web3.currentProvider.isFortmatic // => true