v4 is here! Read all about it
Maintained by ChainSafe
Trusted by
Web3.js was established in 2014, making it the oldest web3 library. With extensive documentation, an active community and modular design, Web3.js is powerful and easy-to-use.
2M
Monthly npm usage
2M
Monthly CDN requests
500k
Weekly downloads
19K
GitHub stars
3K+
Dependent projects
Deploy and interact with smart contracts as TS/JS objects
Subscribe to specific on-chain or smart contract events
Interact with the Ethereum Name Service (ENS)
Create and import accounts, sign data and transactions
Get block and state information from Ethereum nodes
Have access to utilities with built-in helpers for Ethereum dApps and web3 packages
import {Web3} from "web3";
// set a provider in the sepolia testnet using node rpc url
const web3 = new Web3("https://rpc.sepolia.org");
// interacting with the smart contract
const abi = [
{
outputs: [
{
internalType: "uint256",
name: "randomNo",
type: "uint256",
},
],
name: "generateRandomNumber",
stateMutability: "nonpayable",
type: "function",
},
] as const;
const address = "0xA36432F7B12f160F685717c4Ab12EB883a682810";
// create a new contract object, providing the ABI and address
const contract = new web3.eth.Contract(abi, address);
// using contract.methods to get value
contract.methods
.generateRandomNumber()
.call()
.then(console.log);
import {Web3} from "web3";
// set a provider to the sepolia testner using node rpc url
const web3 = new Web3("wss://sepolia.infura.io/ws/v3/API_KEY");
// interacting with the smart contract
const abi = [
{
inputs: [
{
internalType: "uint256",
name: "randomNo",
type: "uint256",
},
],
name: "RandomNo",
type: "event",
},
] as const;
const address = "0xA36432F7B12f160F685717c4Ab12EB883a682810";
// create a new contract object, providing the ABI and address
const contract = new web3.eth.Contract(abi, address);
// using contract.methods to get value
const subscription = contract.events.RandomNo()
subscription.on("data", console.log); // new value every time the event is emitted
import {Web3} from "web3"
// set a provider such as using infura
const web3 = new Web3("https://rpc.sepolia.org")
// Get the balance of an Ethereum address
web3.eth.getBalance("0xF8561Dc64E577B0CF53dd3FC11329e80B1A8343e").then(console.log) // result: 1.000.000 eth
// Get the latest block number
web3.eth.getBlockNumber().then(console.log) // result: BigInt(xxx)