How to get the Ethereum gas price using web3 JS

In this tutorial, we are going to learn how to get the current Ethereum gas price using Web3 JavaScript.

In this tutorial, we are going to learn how to get the current Ethereum gas price using Web3 JavaScript.

First of all, you need to install the web3 JavaScript library using the following command:

npm install web3

Then, to interact with a blockchain you need to create an instance of Web3 and give it a provider (like window.ethereum for browser wallets, for example) or HTTP providers like Infura.

You can check out our guides if you want to learn how to connect to a wallet or how to use the Web3 JS library.

Now that everything is set up, here is how to get the gas price:

const gasPrice = await web3.eth.getGasPrice();

That will return a the gas price which you will have to multiply by the gas amount to know the fee (in Ethereum) that you will pay for a specific transaction.

If you want a more in-depth guide on how to estimate the cost of a transaction, check out this tutorial:

How to estimate the gas fee for a transaction using web3 JS
In this tutorial, we are going to learn how to get the gas price and the gas amount to estimate the gas fee that a transaction will cost.

Thank you for reading this article 🙂