Learn how Ethers.js simplifies Ethereum development with abstractions over raw JSON-RPC.
Ethers.js is a JavaScript library that makes it easier to interact with Ethereum. Instead of dealing with raw JSON-RPC methods, you get convenient abstractions for common tasks like sending transactions, reading contract data, and managing wallets.
Why use Ethers.js instead of raw JSON-RPC?
Install Ethers.js V6 using npm or yarn:
npm install ethers@6
# or
yarn add ethers@6
# or
pnpm add ethers@6Reads data from blockchain (view-only)
Sends transactions and signs messages
Interacts with smart contracts
Manages private keys and accounts
Compare raw JSON-RPC with Ethers.js. Notice how Ethers.js handles conversions automatically.
Raw JSON-RPC:
// Lots of manual work
const balance = await window.ethereum.request({
method: 'eth_getBalance',
params: ['0x...', 'latest']
});
// Manual conversion from Wei to ETH
const ethBalance =
(Number(BigInt(balance)) / 1e18)
.toFixed(2);Ethers.js:
// Much cleaner!
import { ethers } from 'ethers';
const provider = new ethers.JsonRpcProvider(
'https://eth.llamarpc.com'
);
const balance = await provider
.getBalance('0x...');
// Already in BigNumber format
const ethBalance =
ethers.formatEther(balance);Ethers.js provides a simple way to connect to MetaMask and get a signer for transactions.
import { ethers } from 'ethers';
async function connectWallet() {
// Request account access
await window.ethereum.request({
method: 'eth_requestAccounts'
});
// Create provider from MetaMask
const provider = new ethers.BrowserProvider(
window.ethereum
);
// Get signer (user's wallet)
const signer = await provider.getSigner();
// Get user's address
const address = await signer.getAddress();
console.log('Connected:', address);
return { provider, signer };
}