Get gas prices from the ETH Gas Station using json in Python

Gas is the fee that is required to process a transaction on the Ethereum blockchain. The price of gas fluctuates based on the amount of computation your transaction requires and transaction demand at the time of submission.

If the gas that you pay in your transaction is too low you will longer for the transaction to to confirm. If the gas that you pay in your transaction is too high you over paid. It is important not to overlook gas as the network might be congested which drives up prices.

Gas prices can be obtained from the ETH Gas Station website,, and other sources. The Python code below prints out ETH gas prices from:

  • The ETH Gas Station website using json. It print prices from slow to fast.
  • using eth.gasPrice

One can use this to compare calculated gas prices between the two sources. In addition you can use this in your code to calculate the price of gas before submitting a transaction to the Ethereum blockchain.

from web3 import Web3
import json
import requests


web3 = Web3(Web3.HTTPProvider(infura_url))

req = requests.get('')
t = json.loads(req.content)
print('safeLow', t['safeLow'])
print('average', t['average'])
print('fast', t['fast'])
print('fastest', t['fastest'])

gas_price1 = web3.eth.gasPrice

Next – Call a uniswap function using in Python


Leave a Reply