Configure Infura with Web3 and Node.js

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.

Infura service

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.

Infura dashboard

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.

mkdir infura-connect && cd infura-connect
npm init --y

let’s install the dependencies.

npm install --S [email protected]

Here is the code to make a connection.

const Web3 = require("web3");
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.

Infura with Node

Awesome. Infura is now configured with Web3 and Node.