JavaScript API libraries
In order for a web app to interact with the Ethereum blockchain (i.e. read blockchain data and/or send transactions to the network), it must connect to an Ethereum node.
For this purpose, every Ethereum client implements the JSON-RPC specification, so there are a uniform set of methods that applications can rely on.
If you want to use JavaScript to connect with an Ethereum node, it's possible to use vanilla JavaScript but several convenience libraries exist within the ecosystem that make this much easier. With these libraries, developers can write intuitive, one-line methods to initialize JSON RPC requests (under the hood) that interact with Ethereum.
Please note that since The Merge, two connected pieces of Ethereum software - an execution client and a consensus client - are required to run a node. Please ensure your node includes both an execution and consensus client. If your node is not on your local machine (e.g. your node is running on an AWS instance) update the IP addresses in the tutorial accordingly. For more information please see our page on running a node.
Prerequisites
As well as understanding JavaScript, it might be helpful to understand the Ethereum stack and Ethereum clients.
Why use a library?
These libraries abstract away much of the complexity of interacting directly with an Ethereum node. They also provide utility functions (e.g. converting ETH to Gwei) so as a developer you can spend less time dealing with the intricacies of Ethereum clients and more time focused on the unique functionality of your application.
Library features
Connect to Ethereum nodes
Using providers, these libraries allow you to connect to Ethereum and read its data, whether that's over JSON-RPC, INFURA, Etherscan, Alchemy or MetaMask.
Ethers example
1// A Web3Provider wraps a standard Web3 provider, which is2// what MetaMask injects as window.ethereum into each page3const provider = new ethers.providers.Web3Provider(window.ethereum)45// The MetaMask plugin also allows signing transactions to6// send ether and pay to change state within the blockchain.7// For this, we need the account signer...8const signer = provider.getSigner()9நகலெடு
Web3js example
1var web3 = new Web3("http://localhost:8545")2// or3var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))45// change provider6web3.setProvider("ws://localhost:8546")7// or8web3.setProvider(new Web3.providers.WebsocketProvider("ws://localhost:8546"))910// Using the IPC provider in node.js11var net = require("net")12var web3 = new Web3("/Users/myuser/Library/Ethereum/geth.ipc", net) // mac os path13// or14var web3 = new Web3(15 new Web3.providers.IpcProvider("/Users/myuser/Library/Ethereum/geth.ipc", net)16) // mac os path17// on windows the path is: "\\\\.\\pipe\\geth.ipc"18// on linux the path is: "/users/myuser/.ethereum/geth.ipc"19அனைத்தையும் காட்டு