facebook

How to Fetch Transaction History on Ethereum using Web3.py

Posted By : Rahul

Jun 12, 2025

One of the most popular blockchain systems, Ethereum, enables token trading, smart contract development, and the development of decentralised apps (DApps).  Retrieving transaction history for a specific address is a crucial component of any blockchain.  Web3.py is a well-liked Python toolkit for Ethereum developers to communicate with the Ethereum blockchain.  We'll demonstrate how to use Web3.py to retrieve Ethereum transaction history in this blog post.

 

What is Web3.py?

 

Web3.py is a Python library that enables interaction with the Ethereum blockchain.  It enables developers to send transactions, communicate with smart contracts, query the blockchain, and much more. Web3.py is frequently used in Ethereum-based DApps and blockchain development to facilitate blockchain interactions using Python.

 

Why Fetch Transaction History?

 

One essential function of any blockchain explorer, wallet app, or DApp is the ability to retrieve transaction history.  Obtaining transaction information allows you to:

 

Steps to Fetch Ethereum Transaction History

 

1. Set Up Web3.py:

 

Installing Web3.py and configuring the connection to an Ethereum node
pip install web3

 

2. Connecting to the Ethereum Network:

 

Web3.py must be connected to an Ethereum node in order to communicate with the Ethereum blockchain. Infura is a popular solution for this, offering an Ethereum access API. For this, you will want an Infura project ID.
 

Also, Discover | Developing Cross-Platform Crypto Wallet with Web3.js & React


 


from web3 import Web3
infura_url = 'https://mainnet.infura.io/v3/your infura api key'
web3 = Web3(Web3.HTTPProvider(infura_url))

# Check if connected
if web3.is_connected():
    print('Connected to Ethereum Network')
else:
    print('Failed to connect to Ethereum Network')

 

3. Fetch Transaction History:
 

def get_transaction_history(address, start_block=0, end_block='latest'):
    address = web3.to_checksum_address(address)  # Ensure address is in checksum format
    transactions = []

    # Define block range
    end_block = web3.eth.block_number if end_block == 'latest' else int(end_block)

    # Loop through blocks and fetch transactions
    for block_number in range(start_block, end_block + 1):
        block = web3.eth.get_block(block_number, full_transactions=True)
        print(f'Processing block {block_number}')
        if block and block.transactions:
            for txn in block.transactions:
                # Check if the transaction involves the target address
                if txn['to'] == address or txn['from'] == address:
                    transactions.append(txn)

    return transactions

# Replace with the address you're querying
transactions = get_transaction_history('0xYourEthereumAddressHere',
                                       start_block=10000000,
                                       end_block=str(10001000))

# Display transaction details
for txn in transactions:
    print(f'Transaction Hash: {txn['hash'].hex()}')
    print(f'From: {txn['from']}')
    print(f'To: {txn['to']}')
    print(f'Amount: {web3.from_wei(txn['value'], 'ether')} ETH')
    print(f'Block Number: {txn['blockNumber']}')
    print(f'Gas Used: {txn['gas']}')
    print('---')

 

Output: 

 


 Environment updated. Reloading shell...
Connected to Ethereum Network
Processing block 21873279
Processing block 21873280
Processing block 21873281
Processing block 21873282
Processing block 21873283
Processing block 21873284

Transaction Hash: 650b635c2687df8103a3df0ede497e0ff12fb3041494adf031debc7b61e8223c
From: 0x8D9903956d0C9bE104E4Ed32852080F86cfCadcF
To: 0x66a9893cC07D91D95644AEDD05D03f95e1dBA8Af
Amount: 0 ETH
Block Number: 21873279
Gas Used: 351949
---

 

Conclusion

 

Fetching Ethereum transaction history using Web3.py empowers developers to build robust blockchain applications with transparency and accountability. Whether you're developing a wallet, an audit tool, or an analytics dashboard, accessing historical transaction data is key to understanding user behavior, ensuring compliance, and enabling seamless experiences. With just a few lines of Python and a connection to an Ethereum node like Infura, you can easily retrieve and analyze on-chain activity, making Web3.py an essential tool in every Ethereum developer's toolkit.

 

If you're looking to build or scale blockchain solutions, feel free to connect with our experienced blockchain developers for tailored development support.

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

June 26, 2025 at 12:53 pm

Your comment is awaiting moderation.

bg bg

What's Trending in Tech

bg

Our Offices

India

INDIA

DG-18-009, Tower B,
Emaar Digital Greens, Sector 61,
Gurugram, Haryana
122011.
Unit- 117-120, First Floor,
Welldone Tech Park,
Sector 48, Sohna road,
Gurugram, Haryana
122018.
USA

USA

30N, Gloud St STR E, Sheridan, Wyoming (USA) - 82801
Singapore

SINGAPORE

10 Anson Road, #13-09, International Plaza Singapore 079903.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.