DEV Community

OnFinality
OnFinality

Posted on Originally published at onfinality.io

BNB Smart Chain RPC URL: Endpoint, Setup & Debugging

Quick answer: the BNB Smart Chain RPC URL

The BNB Smart Chain (BSC) RPC URL is the endpoint your application uses to communicate with the BSC network via JSON-RPC. The official OnFinality public endpoint is:

https://bnb.api.onfinality.io/public
Enter fullscreen mode Exit fullscreen mode

This URL works for both HTTP and WebSocket connections. You can use it in MetaMask, Hardhat, ethers.js, viem, or any other Web3 tool that supports custom RPC endpoints.

Chain settings at a glance

Before you connect, make sure your configuration matches the BSC mainnet parameters:

Setting Value
Network name BNB Smart Chain Mainnet
Chain ID 56
Native currency BNB (18 decimals)
Block explorer https://bscscan.com
RPC URL (OnFinality) https://bnb.api.onfinality.io/public
WebSocket URL wss://bnb.api.onfinality.io/public/ws

For testnet development, use the BNB Chain Testnet with chain ID 97 and the endpoint https://bnb-testnet.api.onfinality.io/public.

How to configure the RPC URL in a wallet

If you want to add BSC to MetaMask or another wallet, use the chain settings above. Here is an example for MetaMask:

  1. Open MetaMask and click the network dropdown.
  2. Click Add Network.
  3. Fill in the details:
  4. Click Save.

Now your wallet can interact with BSC.

Using the RPC URL in code

Here is how to use the endpoint with ethers.js and viem.

ethers.js

const { ethers } = require("ethers");

const provider = new ethers.JsonRpcProvider("https://bnb.api.onfinality.io/public");

async function getBlockNumber() {
  const blockNumber = await provider.getBlockNumber();
  console.log("Current block:", blockNumber);
}

getBlockNumber();
Enter fullscreen mode Exit fullscreen mode

viem

import { createPublicClient, http } from 'viem';
import { bsc } from 'viem/chains';

const client = createPublicClient({
  chain: bsc,
  transport: http('https://bnb.api.onfinality.io/public'),
});

const blockNumber = await client.getBlockNumber();
console.log('Current block:', blockNumber);
Enter fullscreen mode Exit fullscreen mode

Public vs. managed RPC: which should you use?

Public RPC endpoints like the one above are great for development, testing, and low-traffic applications. However, they are shared across many users, which can lead to rate limiting and slower responses during peak times.

For production applications, you should consider a managed RPC provider. OnFinality offers RPC pricing and supported RPC networks that include BSC. Managed endpoints provide higher reliability, better performance, and dedicated options.

How to choose an RPC provider for BSC

When evaluating RPC providers for BSC, consider these factors:

Criterion What to check Why it matters
Uptime and reliability Historical uptime, SLAs Downtime means your dApp is unavailable
Throughput and rate limits Requests per second, burst capacity High-traffic apps need headroom
Archive data Does the provider offer archive nodes? Needed for historical queries and analytics
WebSocket support Does it support wss://? Required for real-time subscriptions
Dedicated options Can you get a dedicated node? Isolates your traffic from noisy neighbors
Pricing model Pay-as-you-go vs. subscription Predictable costs for your budget

OnFinality offers both shared and dedicated BSC nodes. For high-throughput workloads, a dedicated node gives you more consistent performance. See our BNB Chain RPC provider comparison for more details.

Common RPC errors and how to debug them

Here are some common issues you might encounter when using a BSC RPC URL:

Error Likely cause Fix
connection refused Wrong URL or network issue Check the URL and your internet connection
rate limit exceeded Too many requests on a shared endpoint Use a managed provider or add retry logic
invalid chain id Wrong chain ID in your config Set chain ID to 56 for mainnet
method not found Using an unsupported JSON-RPC method Check the provider's method support
timeout Slow network or overloaded provider Increase timeout or use a dedicated node

Debugging with curl

You can test your RPC endpoint with a simple curl command:

curl -X POST https://bnb.api.onfinality.io/public \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'
Enter fullscreen mode Exit fullscreen mode

A successful response looks like:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": "0x1a2b3c"
}
Enter fullscreen mode Exit fullscreen mode

WebSocket subscriptions

For real-time data, use the WebSocket endpoint. Here is an example with ethers.js:

const { ethers } = require("ethers");

const provider = new ethers.WebSocketProvider("wss://bnb.api.onfinality.io/public/ws");

provider.on("block", (blockNumber) => {
  console.log("New block:", blockNumber);
});
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

  • The BNB Smart Chain RPC URL is the gateway to BSC for your dApp or wallet.
  • The OnFinality public endpoint is https://bnb.api.onfinality.io/public.
  • Always verify chain ID (56) and other settings when configuring your environment.
  • For production, consider a managed RPC provider for better reliability and performance.
  • Use the debugging tips above to resolve common RPC issues quickly.

Frequently Asked Questions

What is the BNB Smart Chain RPC URL?

The official OnFinality public RPC URL is https://bnb.api.onfinality.io/public. It supports HTTP and WebSocket.

Can I use the BSC RPC URL for free?

Yes, the public endpoint is free for development and light usage. For production, check RPC pricing for managed options.

What is the BSC testnet RPC URL?

The BNB Chain Testnet RPC URL is https://bnb-testnet.api.onfinality.io/public with chain ID 97.

How do I add BSC to MetaMask?

Use the chain settings provided in this article to add BSC as a custom network.

What should I do if I get rate limited?

Switch to a managed RPC provider or implement retry logic with exponential backoff. OnFinality offers scalable options for production workloads.

Related resources

Originally published at OnFinality.

Top comments (0)