BNB Smart Chain

BNB Smart Chain

JavaScript / TypeScript

JavaScript developers targeting BNB Smart Chain reach for one of two libraries: web3.js, the long-standing workhorse, or ethers.js, the leaner modern alternative. Because BSC speaks the same geth-flavoured JSON-RPC dialect as any EVM network, both libraries run unchanged — you simply hand them the https://bsc.therpc.io/YOUR_API_KEY endpoint and start reading balances or broadcasting transactions on chain 56. For greenfield work we lean toward ethers.js v6. Its tighter API and first-class typings pay off over a project's lifetime, helped by a smaller browser footprint.

Both libraries are under active maintenance and ship regular releases, so either is a safe bet for production BSC code. That said, ethers.js v6 is our recommendation for new projects: its TypeScript support is sharper and its bundle size is meaningfully smaller, which matters when your dApp ships to wallets and mobile browsers.

Web3.js

web3.js is the original and most widely deployed JavaScript API for talking to BNB Smart Chain, and a huge body of tutorials and Stack Overflow answers assumes it. Pull it in with npm install web3, then construct a client by passing your TheRPC BSC URL straight to the constructor: new Web3('https://bsc.therpc.io/YOUR_API_KEY'). From that one instance you get the full web3.eth surface: balance reads in BNB, transaction sends, plus BEP-20 contract calls.

import Web3 from 'web3';
const web3 = new Web3('https://bsc.therpc.io/YOUR_API_KEY');
// Get balance
const getBalance = async (address) => {
const balance = await web3.eth.getBalance(address);
const balanceInEth = web3.utils.fromWei(balance, 'ether');
return balanceInEth;
};
// Send transaction
const sendTransaction = async (from, to, value) => {
const tx = {
from,
to,
value: web3.utils.toWei(value, 'ether'),
};
return await web3.eth.sendTransaction(tx);
};
  • GitHub: https://github.com/web3/web3.js
  • Docs: https://web3js.readthedocs.io/
  • Complete BNB Smart Chain JSON-RPC coverage, including WebSocket transports for live BSC subscriptions
  • Rich contract abstractions for deploying and calling BEP-20 and other Solidity contracts
  • ENS resolution plus the largest community and tutorial corpus of any EVM JS library

Ethers.js

ethers.js is the modern, compact counterpart, written in TypeScript from the ground up so its typings are exact rather than bolted on. Install it with npm install ethers, then open a connection to BSC by handing the endpoint to a provider: new ethers.JsonRpcProvider('https://bsc.therpc.io/YOUR_API_KEY'). That provider becomes your gateway for querying chain 56: it fetches BNB balances, estimates gas, then submits signed transactions.

import { ethers } from 'ethers';
const provider = new ethers.JsonRpcProvider('https://bsc.therpc.io/YOUR_API_KEY');
// Get balance
const getBalance = async (address) => {
const balance = await provider.getBalance(address);
return ethers.formatEther(balance);
};
// Send transaction
const sendTransaction = async (wallet, to, value) => {
const tx = await wallet.sendTransaction({
to,
value: ethers.parseEther(value),
});
return await tx.wait();
};
  • GitHub: https://github.com/ethers-io/ethers.js/
  • Docs: https://docs.ethers.org/
  • Native TypeScript types and a noticeably smaller bundle than web3.js
  • Hardened security defaults and immutable objects that reduce footguns in signing flows
  • Built-in ENS resolution and an extensive, well-audited test suite trusted by major BSC dApps

TypeScript Support

Neither library needs a separate @types/* package — web3.js and ethers.js both bundle their own declaration files, so the moment you import them your editor knows the shape of every BSC call. The practical wins are immediate. Autocompletion suggests valid method names and parameters. The compiler catches a malformed transaction object before it ever reaches chain 56. And that inline type information doubles as living documentation while you build.

import { BigNumber } from 'ethers';
interface TransactionData {
to: string;
value: BigNumber;
gasLimit?: BigNumber;
}
const createTransaction = async (data: TransactionData) => {
// Your implementation
};

Node.js Usage

On the server, both libraries run happily under Node.js regardless of your module system. Classic CommonJS projects can pull them in with require(), while ESM projects use import — the same BSC provider setup applies either way, so dropping a chain-56 client into a backend service, a cron job, or an indexer takes no extra ceremony.

// Web3.js in Node.js
const Web3 = require('web3');
const web3 = new Web3('https://bsc.therpc.io/YOUR_API_KEY');
// Ethers.js in Node.js
const { ethers } = require('ethers');
const provider = new ethers.JsonRpcProvider('https://bsc.therpc.io/YOUR_API_KEY');

Browser Usage

In the browser you bring the library in through ES module imports and let a bundler such as webpack or Vite handle the packaging. When a user has an injected wallet like MetaMask configured for BNB Smart Chain, skip the read-only RPC provider and wrap the injected object instead: new ethers.BrowserProvider(window.ethereum). That hands signing control to the wallet while still routing reads across the BSC network.

// Using ES modules
import { Web3 } from 'web3';
// or
import { ethers } from 'ethers';
// Using window.ethereum (MetaMask)
const provider = new ethers.BrowserProvider(window.ethereum);

Ready to call this in production?

Free tier covers personal projects. Pay-as-you-go scales without a card.