Configure Infura with Web3.js and Node.js
Introduction
At the foundation of any decentralized application on the Ethereum network, is an easy to use and reliable way to connect and interact with the Ethereum blockchain.
Infura provides this service. They give instant access over HTTPs and WebSockets to the Ethereum network. And they make interacting with the Ethereum blockchain super easy.
In this article, we'll walk through how to configure Infura with Node.js and Web3.js. Web3.js is a collection of libraries that will help us interact with the Infura Etheruem node.
Let's get started!
Table of Contents
Create Infura Account & Get Api Token
The first thing we need to do is create an Infura account and get an api key to access their network.
So, head over to the Infura website and sign up for a new account.
Infura Sign Up Page Screenshot
Then, go to the Dashboard page and click the Create New Project button. Enter the name of your project and click the Create button.
Infura Create New Project Screenshot
Once you've created your project, go to your project page. In the Keys section, your Project ID will be listed. That's what we'll use to send requests to the Infura Api.
Infura Api Keys Page Screenshot
Now that we have our Project ID, we are ready to configure Web3.js and connect with the Infura Ethereum nodes.
Install Web3.js & Make a Test Infura Api Request
Before we install Web3.js, make sure you have Node.js installed and an application directory setup to store our code.
You can install the Web3.js npm package with one of these commands:
# With Npm
$ npm install web3 --save
 
# With Yarn
yarn add web3
When that's done installing, Web3.js is ready to be used.
To illustrate how to use Web3.js alongside Infura, let's use Web3.js and Infura together to get the balance of an Ethereum address.
Open up your favorite text editor and add this code to your application's index.js or app.js file (make sure you replace YOUR_PROJECT_ID with your unique Project ID):
Node.js
 
const Web3 = require("web3")
 
const web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/v3/YOUR_PROJECT_ID"))
 
web3.eth.getBalance("0x5A0b54D5dc17e0AadC383d2db43B0a0D3E029c4c", function(err, result) {
  if (err) {
    console.log(err)
  } else {
    console.log(web3.utils.fromWei(result, "ether") + " ETH")
  }
})
A couple different things are happening in the code here, so let's go through each part:
  1. First, we require the web3 npm package and store it in the uppercase Web3 variable.
  2. Then, we create a web3 instance and set its provider (the method in which web3 gets access to an Ethereum node). We use the HttpProvider() function and pass our Infura api endpoint. This api endpoint is for the Mainnet network (you can replace this with any of the other networks, i.e. Ropsten) and has the ID for our project appended to the end of the url.
  3. Now that the web3 instance is ready to use, we use the web3.eth.getBalance() function to get the balance of an Ethereum address. We pass an Ethereum address as a parameter and the function returns a callback with an error and the balance for the address.
  4. Inside the callback, we do some error handling by logging any errors that occur.
  5. Then, we log the result from the callback function. We use the web3.utils.fromWei() function to log it in a human-readable format.
When you run the code, you should get an output similar to this:
Output
 
5110.694355 ETH
If you're looking for more things to do with Web3.js, check out the documentation.
Conclusion
Pretty pain-free right? Hopefully, this article helped you in your Ethereum coding journeys!
As always, thanks for reading and happy coding!