How to connect to Ethereum network with Web3.js | QuickNode (2024)

3 min read

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:

$ 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:

$ npm install web3

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

$ 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.

Set Up Your QuickNode Endpoint

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 create a free QuickNode account here and generate an Ethereum endpoint. After you've created your free Ethereum endpoint, copy your HTTP Provider endpoint:

How to connect to Ethereum network with Web3.js | QuickNode (1)

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:

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:

$ node index.js

You should see the following:

How to connect to Ethereum network with Web3.js | QuickNode (2)

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 :)

How to connect to Ethereum network with Web3.js | QuickNode (2024)
Top Articles
The "Grandparent Loophole": Grandparent-Owned 529 Accounts & the New FAFSA - Savingforcollege.com
How to file a retaliation/discrimination complaint
Use Copilot in Microsoft Teams meetings
NOAA: National Oceanic & Atmospheric Administration hiring NOAA Commissioned Officer: Inter-Service Transfer in Spokane Valley, WA | LinkedIn
Bild Poster Ikea
Fat Hog Prices Today
Celebrity Extra
50 Meowbahh Fun Facts: Net Worth, Age, Birthday, Face Reveal, YouTube Earnings, Girlfriend, Doxxed, Discord, Fanart, TikTok, Instagram, Etc
Volstate Portal
Steve Strange - From Punk To New Romantic
Does Pappadeaux Pay Weekly
Turbocharged Cars
Urban Dictionary Fov
The Rise of Breckie Hill: How She Became a Social Media Star | Entertainment
Nier Automata Chapter Select Unlock
Slushy Beer Strain
8 Ways to Make a Friend Feel Special on Valentine's Day
Craigslist Edmond Oklahoma
Joann Ally Employee Portal
Halo Worth Animal Jam
Rqi.1Stop
Best Transmission Service Margate
Slim Thug’s Wealth and Wellness: A Journey Beyond Music
3Movierulz
Dmv In Anoka
Bayard Martensen
Mini-Mental State Examination (MMSE) – Strokengine
Imagetrend Elite Delaware
Vip Lounge Odu
Dentist That Accept Horizon Nj Health
Citibank Branch Locations In Orlando Florida
Egg Crutch Glove Envelope
Utexas Baseball Schedule 2023
Jambus - Definition, Beispiele, Merkmale, Wirkung
De beste uitvaartdiensten die goede rituele diensten aanbieden voor de laatste rituelen
October 31St Weather
Mohave County Jobs Craigslist
Nancy Pazelt Obituary
Lcwc 911 Live Incident List Live Status
Janaki Kalaganaledu Serial Today Episode Written Update
Best Restaurants West Bend
Fool's Paradise Showtimes Near Roxy Stadium 14
2024-09-13 | Iveda Solutions, Inc. Announces Reverse Stock Split to be Effective September 17, 2024; Publicly Traded Warrant Adjustment | NDAQ:IVDA | Press Release
Fatal Accident In Nashville Tn Today
Avatar: The Way Of Water Showtimes Near Jasper 8 Theatres
Tlc Africa Deaths 2021
15 Best Places to Visit in the Northeast During Summer
Willkommen an der Uni Würzburg | WueStart
Gear Bicycle Sales Butler Pa
Kidcheck Login
Nkey rollover - Hitta bästa priset på Prisjakt
Factorio Green Circuit Setup
Latest Posts
Article information

Author: Madonna Wisozk

Last Updated:

Views: 6630

Rating: 4.8 / 5 (68 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Madonna Wisozk

Birthday: 2001-02-23

Address: 656 Gerhold Summit, Sidneyberg, FL 78179-2512

Phone: +6742282696652

Job: Customer Banking Liaison

Hobby: Flower arranging, Yo-yoing, Tai chi, Rowing, Macrame, Urban exploration, Knife making

Introduction: My name is Madonna Wisozk, I am a attractive, healthy, thoughtful, faithful, open, vivacious, zany person who loves writing and wants to share my knowledge and understanding with you.