Infura is a SaaS-based application offers development suits for Ethereum and IPFS platform. By using Infura, you don’t need to worry about setting up your own nodes in the machine to participate in the network. You can use Infura service to spin up the Ethereum test or main network and access it using the API.
You can use it for free and upgrade to paid options for production needs. No credit card required for testing and playing around.
Let’s create one Ethereum test environment and connect to it using our code.
First, create a new account on Infura.
Login to your account and create a new project. Give it a name and Infura will provide you the credentials.
Click on the project and go to Keys section. Choose the endpoint as rinkeby for testing.
Let’s connect to it using our code.
Create a new Node project.
let’s install the dependencies.
Here is the code to make a connection.
const ethNetwork = 'https://rinkeby.infura.io/v3/your project id';
const web3 = new Web3(new Web3.providers.HttpProvider(ethNetwork));
// let's fetch a balance
web3.eth.getBalance('0x6635F83421Bf059cd8111f180f0727128685BaE4', async (err, result) => {
if (err) {
console.log(err);
return;
}
let balance = web3.utils.fromWei(result, "ether");
console.log(balance + " ETH");
});
Replace your project id with the one provided by Infura and run the code.
You should receive the balance of the account in the terminal.
Awesome. Infura is now configured with Web3 and Node.