Marketplace has launched, further enabling blockchain developers! Learn more

How to connect to Ethereum network with Web3.js

September 09, 2022

Overview

Libraries and frameworks make the development process a lot easier and faster. When it comes to Ethereum development, Web3.js is the go to library. That's because Web3.js is the official library, from the Ethereum foundation. It serves as the best way to develop a website/client application that interacts with the Ethereum blockchain's JSON RPC via HTTP, WebSocket or IPC connection.

In this guide, we will learn how to use Web3 to connect to the Ethereum blockchain using HTTP and grab the latest block number. The cool thing about this will be that our code will work for both a front-end (with a build system, like webpack) and back-end implementation (using NodeJS).

Prerequisites
  • NodeJS installed on your system
  • A text editor
  • Terminal aka Command Line

Installing the Web3.js package

Let's make sure we have NodeJS installed. Pretty simple, open a terminal and run:

installing the web3 js package

Copy
$ node -v 

Next, let's get the web3 package installed - this is the package that lets you interact with the ethereum blockchain. You can install it with npm from your command line:

installing the web3 js package

Copy
$ npm install web3

or you can use yarn, if that's your preferred package manager:

installing the web3 js package

Copy
$ yarn add web3

The most common issue at this step is an internal failure with `node-gyp`. You can follow node-gyp installation instructions here.

Note: You will need to have your python version match one of the compatible versions listed in the instructions above if you encounter the node-gyp issue. 

Booting our Ethereum node

For our purposes today, we could use pretty much any Ethereum client, such as Geth or OpenEthereum (fka Parity). Since that is a bit too involved for just querying for block height, we'll just grab a free endpoint from QuickNode to make this easy. After you've created your free Ethereum endpoint, copy your HTTP Provider endpoint:

A screenshot of the Quicknode Ethereum endpoint Getting Started page with HTTP link and WSS


You'll need this later, so copy it and save it.

Connecting via Web3

Now let's create a short script, let's call it `index.js`, to fetch the block height from our node. You can copy/paste this into your code editor:

connecting via web3

Copy
var Web3 = require('web3');
var provider = 'ADD_YOUR_ETHEREUM_NODE_URL';
var web3Provider = new Web3.providers.HttpProvider(provider);
var web3 = new Web3(web3Provider);
web3.eth.getBlockNumber().then((result) => {
  console.log("Latest Ethereum Block is ",result);
});

So go ahead and replace `ADD_YOUR_ETHEREUM_NODE_URL` with the http provider from the instructions above. 

A quick explanation of the code above - we are importing the web3 library we installed earlier (line 1), setting our Ethereum node URL (line 2), instantiating a Web3 HttpProvider instance (line 3) and creating a Web3 instance (line 4). Finally, we're calling getBlockNumber on the web3 object and waiting for a successful result from the server (QuikNode) then logging the result to the console (lines 5-7).

Save this code snippet in a file index.js we're going to run this very shortly.

Confirm it's working

Run the file using the node command and you will see the latest Ethereum blockchain:

confirm it s working

Copy
$ node index.js

You should see the following:



That’s it! You have connected to the Ethereum network using Web3.js. You can check out more APIs provided by web3 to build complex applications on Ethereum. If you want to use this in a front-end application, you can configure webpack to compile it into something suitable for a browser.

Subscribe to our newsletter for more articles and guide on Ethereum. If you have any feedback, please feel free to reach out to us via Twitter and you can always chat with us if you have a question via our community server on Discord, thanks :)

Related articles 35

How to Access Bitcoin Mempool
Published: Aug 16, 2021
Updated: Sep 21, 2022

Bitcoin is the father of blockchain technology. With Bitcoin started a new era of blockchain and decentralization. Bitcoin enabled everyone to make the peer-to-peer transactions they enjoy...

Continue reading
How to access Ethereum Mempool
Published: Oct 15, 2020
Updated: Sep 15, 2022

On Ethereum, when a transaction is sent, before being added to a block, it resides in what is called a Mempool. To receive information about this transaction, the Mempool must be queried. This...

Continue reading
The Web3 Developer Stack
Published: Jul 3, 2021
Updated: Sep 15, 2022

A developer stack is a bag of technologies a developer possesses. For example, MEAN (MongoDB, Express.js, AngularJS/Angular, and Node.js) and MERN (MongoDB, Express.js, React, and Node.js) are...

Continue reading
Introduction to Scaffold-ETH 🏗
Published: Oct 7, 2021
Updated: Sep 9, 2022

Developing applications involves juggling several moving pieces like front-ends, back-ends, and databases. But developing a decentralized application on a blockchain adds a few more elements...

Continue reading
How to create your own DAO with Aragon
Published: Aug 8, 2021
Updated: Sep 9, 2022

Blockchain provides us with the power of decentralization. Decentralization means the transfer of power to users/members rather than having a single centralized authority governing everything;...

Continue reading
How to connect to Ethereum using PHP
Published: Jun 11, 2021
Updated: Sep 9, 2022

PHP is a very popular choice among developers and has a vast community due to its long presence in web development. In this guide, we’ll cover how to connect to Ethereum with PHP using the

Continue reading
How to use Subspace with QuickNode
Published: Sep 8, 2020
Updated: Sep 9, 2022

In this guide, we'll understand a bit about reactive development and how to use Subspace with QuickNode.JavaScript is the programming language behind most of the internet apps and...

Continue reading