ethers.js

Connect to NodeFlare using ethers.js v6 — the most popular Ethereum JavaScript library.

ethers.js is a complete, compact library for interacting with Ethereum. Version 6 is the current stable release.

Installation

Terminal
npm install ethers

Connect a Provider

Replace YOUR_KEY with your API key from the dashboard.

JavaScript
import { ethers } from "ethers";

const provider = new ethers.JsonRpcProvider(
  "https://rpc.nodeflare.app/eth/v1/YOUR_KEY"
);

Switch chains by changing the chain slug in the path (e.g. /base/ → /arb/):

JavaScript
const baseProvider  = new ethers.JsonRpcProvider("https://rpc.nodeflare.app/base/v1/YOUR_KEY");
const arbProvider   = new ethers.JsonRpcProvider("https://rpc.nodeflare.app/arb/v1/YOUR_KEY");
const bnbProvider   = new ethers.JsonRpcProvider("https://rpc.nodeflare.app/bnb/v1/YOUR_KEY");

Read the Latest Block

JavaScript
const blockNumber = await provider.getBlockNumber();
console.log("Block:", blockNumber);

Get an Account Balance

JavaScript
const balance = await provider.getBalance("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
console.log(ethers.formatEther(balance), "ETH");

Call a Contract

JavaScript
const abi = ["function balanceOf(address) view returns (uint256)"];
const usdc = new ethers.Contract(
  "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
  abi,
  provider
);

const balance = await usdc.balanceOf("0xYourAddress");
console.log(ethers.formatUnits(balance, 6), "USDC");

Send a Transaction

JavaScript
const wallet = new ethers.Wallet("0xYOUR_PRIVATE_KEY", provider);

const tx = await wallet.sendTransaction({
  to: "0xRecipientAddress",
  value: ethers.parseEther("0.01"),
});

const receipt = await tx.wait();
console.log("Confirmed in block:", receipt.blockNumber);

WebSocket Provider

Use WebSocketProvider for real-time subscriptions without polling.

JavaScript
import { ethers } from "ethers";

const provider = new ethers.WebSocketProvider(
  "wss://rpc.nodeflare.app/eth/ws/v1/YOUR_KEY"
);

For Base:

JavaScript
const provider = new ethers.WebSocketProvider(
  "wss://rpc.nodeflare.app/base/ws/v1/YOUR_KEY"
);

Subscribe to New Blocks

JavaScript
provider.on("block", (blockNumber) => {
  console.log("New block:", blockNumber);
});

// Unsubscribe
provider.off("block");

Listen to Contract Events

JavaScript
const abi = ["event Transfer(address indexed from, address indexed to, uint256 value)"];
const usdc = new ethers.Contract("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", abi, provider);

usdc.on("Transfer", (from, to, value) => {
  console.log(`${from} → ${to}: ${ethers.formatUnits(value, 6)} USDC`);
});