# How to connect to the Ethereum network using Web3.py

By [WatchData](https://paragraph.com/@gr0wth) · 2022-03-10

---

![](https://storage.googleapis.com/papyrus_images/177a4af9d8db85c129853593ad2d58f0a29b0a38521c40eb3713d20e4ed4d7ba.png)

You can create Ethereum Applications in different programming languages. In this article, we will connect to the Ethereum network using Python.

**Preinstalled**

*   You have Python (version >=3.5.3) and Pip3 installed on your system.
    
*   [Install web3.py](https://github.com/ethereum/web3.py) (A Python wrapper for Ethereum node APIs).
    
*   A text editor
    

Adding web3.py
--------------

**Note:** We need Python version >=3.5.3 and install web3.py using pip3 install web3.

Python and other library versions cause common installation problems. So if you face any problems, try [setting up a virtual environment and troubleshooting](https://web3py.readthedocs.io/en/stable/troubleshooting.html) the installation of web3.py.

Get API Key WatchData
---------------------

To create an API key, you need to do the following steps: On the [Dashboard.watchdata.io](http://dashboard.watchdata.io/) tab, click Create API key.

![](https://storage.googleapis.com/papyrus_images/cdf367cdd3538e5c7bb3b21f353209aae9203efe89443e9c8941cd6f4eb3b5b3.png)

Your API key will be automatically created.

![](https://storage.googleapis.com/papyrus_images/45840094b2965e87982e96188199aada872bae814ab966f41e0822938243bab3.png)

Once you have created the API key, copy it and combine it with the HTTP provider's endpoint in this format:

    https://ethereum.api.watchdata.io/node/jsonrpc?api_key=YOUR_API_KEY_HERE
    

For example:

    https://ethereum.api.watchdata.io/node/jsonrpc?api_key=83ab245b-ae08-43a4-97db-85903d35652f
    

Using web3.py
-------------

We will use web3.py to get the last Ethereum block number. To do this we will use the code below.

    from web3 import Web3
    watchdata_url = 'https://ethereum.api.watchdata.io/node/jsonrpc?api_key=83ab245b-ae08-43a4-97db-85903d35652f'
    w3 = Web3(Web3.HTTPProvider(watchdata_url))
    last_block_number = w3.eth.block_number
    print(f'last block number in eth = {last_block_number}')
    

**Let's take a closer look at the code:**

![](https://storage.googleapis.com/papyrus_images/e060cb34d16b2deeb1ae712ccca89b78b6f28f336752bb9525a55beee8468bca.png)

In this code snippet, we import the web3.py library, declare a watchdata\_url variable to which we assign the URL of our watchdata node in Ethereum. And then we create an instance of the web3 class with our watchdata\_url.

![](https://storage.googleapis.com/papyrus_images/0a9b1763605d31d512c235fdfe608cb1335beb3ab6ac8ea246b59f1814346b4a.png)

In this code snippet, we retrieve the last Ethereum block number using the w3.eth.blockNumber API and display it.

* * *

That's it, you are connected via the Ethereum network using Python. You can also learn more about the [web3.py](https://web3py.readthedocs.io/en/stable/index.html) APIs for building complicated applications on Ethereum.

At the moment, only sets of methods described in [Ethereum API documentation](https://docs.watchdata.io/blockchain-apis/ethereum-api) are available from API interfaces, when working through WatchData node, and we are actively working on adding new methods.

* * *

1.  Join to [our Discord server](https://bit.ly/3w4VlDz)
    
2.  Please leave your feedback in the #feedback channel. If you have any problems or you think that some functionality is not working correctly, we are waiting for your feedback.
    
3.  Want a new feature or you miss some functionality, tell us about it in the #general channel. We are always open to your ideas!
    

#### HELPFUL LINKS

*   [WatchData main](https://bit.ly/3CGCSOT)
    
*   [WatchData Twitter](https://bit.ly/3t6jMyH)
    
*   [WatchData Discord](https://bit.ly/3w4VlDz)
    
*   [WatchData Docs](https://bit.ly/3q1Rpjl)

---

*Originally published on [WatchData](https://paragraph.com/@gr0wth/how-to-connect-to-the-ethereum-network-using-web3-py)*
