Logo
RPC Assistant

How do I use the Binance Smart Chain API?

Summary

The Binance Smart Chain (BSC) API provides JSON-RPC endpoints to interact with the BNB Smart Chain network. Developers use these APIs to query blockchain data, send transactions, and deploy smart contracts. This guide covers the available methods, endpoint URLs, and how to get started with code examples.

Binance Smart Chain API decision checklist

Before integrating with the Binance Smart Chain API, evaluate these factors:

CriterionWhat to checkWhy it matters
Rate limitsAre there per-second or daily limits on requests?Exceeding limits can cause dropped requests; choose a provider with sufficient capacity for your usage.
Archive data supportDoes the endpoint support archive state (e.g., eth_call at past blocks)?Required for dApps that need historical state or deep analytics.
WebSocket supportIs a WebSocket endpoint available for real-time subscriptions?Needed for live updates like pending transactions or new blocks.
Performance & reliabilityWhat is the average latency and uptime history?Production apps require consistent performance; check SLAs if critical.
SecurityIs the connection encrypted (HTTPS/WSS)? Are API keys required?Protects data in transit; API key authentication prevents unauthorized access.

Understanding Binance Smart Chain API

Binance Smart Chain (BSC) is an Ethereum Virtual Machine (EVM)-compatible blockchain, meaning it supports the same JSON-RPC methods as Ethereum. The BSC API allows you to read blockchain data, submit transactions, and interact with smart contracts. Most tools built for Ethereum (like web3.js, ethers.js, and viem) work with BSC with minimal configuration changes.

JSON-RPC Methods

BSC supports standard Ethereum JSON-RPC methods plus a few custom ones for its finality mechanism. Common methods include:

  • eth_blockNumber – Get the latest block number.
  • eth_getBalance – Get the balance of an address.
  • eth_call – Execute a smart contract call without creating a transaction.
  • eth_sendRawTransaction – Broadcast a signed transaction.
  • eth_getTransactionReceipt – Get the receipt of a transaction.
  • eth_getLogs – Retrieve event logs.
  • eth_subscribe / eth_unsubscribe – Real-time subscriptions (WebSocket only).
  • Custom BSC methods: eth_getFinalizedHeader, eth_getFinalizedBlock, eth_newFinalizedHeaderFilter for finality queries.

RPC Endpoints

Public Endpoints

Official public RPC endpoints are available but have strict rate limits and are not recommended for production. Example:

https://bsc-dataseed.binance.org/
https://bsc-dataseed1.defibit.io/

These are free but subject to throttling. Use them only for testing or low-traffic applications.

Private/Provider Endpoints

For production workloads, use a reliable RPC provider like OnFinality. Providers offer dedicated or shared endpoints with higher rate limits, better performance, and support options. OnFinality provides BSC RPC endpoints with:

  • High throughput and low latency.
  • WebSocket support.
  • Archive node access for historical data.
  • API key management.

To create a BSC endpoint on OnFinality, visit our network page or sign up at api-service.

Code Examples

Using cURL

Get the latest block number:

curl -X POST https://rpc.onfinality.io/rpc?apikey=YOUR_API_KEY \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'

Response:

{"jsonrpc":"2.0","id":1,"result":"0x10d4f3b"}

Using ethers.js (JavaScript)

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

const provider = new ethers.JsonRpcProvider("https://rpc.onfinality.io/rpc?apikey=YOUR_API_KEY");

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

  const balance = await provider.getBalance("0x742d35Cc6634C0532925a3b844Bc4a23d0b5f8e2");
  console.log("Balance (BNB):", ethers.formatEther(balance));
}

main();

Using viem (JavaScript)

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

const client = createPublicClient({
  chain: bsc,
  transport: http("https://rpc.onfinality.io/rpc?apikey=YOUR_API_KEY"),
});

const blockNumber = await client.getBlockNumber();
console.log("Block number:", blockNumber);

Common Pitfalls

  • Using outdated nonce: When sending transactions, ensure the nonce matches the next expected nonce for your address. Use eth_getTransactionCount with "pending" parameter.
  • Exceeding rate limits: Public endpoints often impose strict limits. Use a provider with higher limits or dedicated nodes.
  • Missing archive data: Some endpoints don't support archive state. If your application requires historical eth_call, ensure the endpoint provides archive access.
  • WebSocket disconnections: WebSocket connections can drop; implement reconnection logic.

How to Choose the Right API Access

The choice between public, shared private, and dedicated node endpoints depends on your workload:

  • Public endpoints: Suitable for prototyping and low-volume read-only queries. Avoid for production.
  • Shared private endpoints (e.g., via OnFinality): Offer balanced performance and cost for most dApps. Good for read-heavy applications with moderate transaction submission.
  • Dedicated nodes: Full control over resources, clear rate limits, and low latency. Ideal for high-throughput dApps, real-time trading, or data indexing.

For more details on provider selection, see our guide on choosing an RPC provider.

Key Takeaways

  • Binance Smart Chain uses the same JSON-RPC as Ethereum, making it easy to migrate existing code.
  • Public endpoints are available but unsuitable for production due to rate limits.
  • Use a reliable RPC provider like OnFinality for faster, more reliable access.
  • Always test your endpoint's performance and archive support before deploying.
  • Implement proper error handling and reconnection for WebSocket subscriptions.

Frequently Asked Questions

Q: What is the difference between BSC API and Binance exchange API?

A: The BSC API is for interacting with the BNB Smart Chain blockchain (read/write data, deploy contracts), while the Binance exchange API is for trading on the Binance centralized exchange. They serve different purposes.

Q: Can I use Ethereum tools with BSC?

A: Yes, because BSC is EVM-compatible. Most Ethereum libraries work without modification. Just change the RPC endpoint to a BSC one.

Q: How do I get a BSC API key?

A: Sign up with an RPC provider like OnFinality to get an API key. Alternatively, you can use public endpoints without a key, but with limitations.

Q: Does BSC support WebSockets?

A: Yes, most providers offer WebSocket endpoints for real-time subscriptions. Check the provider's documentation for the WSS URL.

Q: What is archive node access?

A: An archive node stores the full state history, allowing queries like eth_call at any past block. Useful for analytics and historical data retrieval.

For more information, check our RPC pricing and supported networks.

RPC Knowledge Base

Related RPC details

Never Worry about Infrastructure Again

OnFinality takes away the heavy lifting of DevOps so you can build smarter and faster.

Get Started