The Web3 API

Learn how to use the web3.js library to interact with Ethereum smart contracts.

Now that we know the role Ethereum clients play and what API they provide, we'll learn how to use this API. This API is used by all Ethereum software, including MetaMask, so we need to have at least a cursory understanding of how to use it in order to become proficient Ethereum developers.

In this lesson, we'll learn more about using the API with the popular web3.js library and doing common operations with it.

Web3 libraries

We’ve already seen what JSON-RPC requests and responses look like, but in practice, we can use one of the many libraries that are designed to interact with the Ethereum API. In this course, we'll use web3.js, which is one of the most popular. There are also other popular JavaScript libraries available, such as Ethers.js; and there are libraries for other programming languages, such as web3j for Java, Nethereum for .NET, and so on.

Regardless of what library we choose, they all provide the same basic functionality. They implement a convenient interface for using the JSON-RPC API and allow deploying smart contracts, sending transactions, calling methods, transferring Ether to other accounts, querying data about the network state, and other related tasks.

The web3.js library

Now, let’s see how we can use the web3.js library to interact with an Ethereum network. We first need to add web3.js to our JavaScript project, and we can do this using the following command:

Get hands-on with 1200+ tech skills courses.