Logo
RPC Assistant

What is the BSC API and how do you use it?

Summary

The BSC API is the JSON-RPC interface for BNB Smart Chain (BSC), letting you read chain data, submit transactions, and interact with smart contracts using the same methods as Ethereum. This guide explains the core methods, how to connect with common libraries, and how to choose between public endpoints and managed RPC providers for production workloads.

Quick decision guide: which BSC API endpoint should you use?

Before you write any code, decide which type of BSC API endpoint fits your workload. The right choice depends on how much traffic you expect, whether you need historical data, and how sensitive your app is to rate limits.

WorkloadRecommended endpoint typeWhy
Prototyping, hackathon, low trafficPublic RPC endpointFree, no signup, but rate limits and occasional instability
Production dApp, moderate trafficManaged RPC provider (shared endpoint)Higher reliability, better rate limits, easy scaling
High-throughput, analytics, or custom needsDedicated nodeFull control, no noisy neighbors, custom configuration
Historical data, deep indexingArchive nodeAccess to historical state, trace data

For most production apps, a managed RPC provider like OnFinality offers a good balance of reliability and cost. If you need predictable performance or custom configuration, consider a dedicated node. Check the RPC pricing page for details.

What is the BSC API?

The BSC API is the JSON-RPC interface for BNB Smart Chain (BSC). It lets you interact with the blockchain: read balances, send transactions, deploy and call smart contracts, and subscribe to events. Because BSC is EVM-compatible, the API follows the same JSON-RPC standard as Ethereum, so most Ethereum tools and libraries work with BSC out of the box.

BSC nodes expose a set of standard methods, plus a few BSC-specific extensions. The official documentation lists the full API, but you'll mostly use the core Ethereum methods like eth_blockNumber, eth_getBalance, eth_call, and eth_sendRawTransaction.

BSC API methods you'll use most

Here are the most common JSON-RPC methods for BSC development:

  • eth_blockNumber – get the latest block number
  • eth_getBalance – get the BNB balance of an address
  • eth_call – execute a read-only smart contract call
  • eth_sendRawTransaction – submit a signed transaction
  • eth_getTransactionReceipt – get the receipt of a transaction
  • eth_getLogs – fetch event logs
  • eth_subscribe – subscribe to new blocks, pending transactions, or logs (WebSocket)

BSC also has some chain-specific methods, like eth_getFinalizedBlock for fast finality, and eth_getBlobSidecarByTxHash for blob data. These are useful if you're building advanced infrastructure.

Connecting to the BSC API with curl

You can test the BSC API with a simple curl request. Replace YOUR_RPC_URL with your endpoint URL.

curl -X POST https://YOUR_RPC_URL \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'

This returns the latest block number in hexadecimal, like 0x10d4f.

Using the BSC API with ethers.js

For JavaScript developers, ethers.js is the most popular library. Here's how to connect to BSC and read data:

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

const provider = new ethers.JsonRpcProvider("https://YOUR_RPC_URL");

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

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

main();

If you're using viem, the setup is similar:

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

const client = createPublicClient({
  chain: bsc,
  transport: http("https://YOUR_RPC_URL"),
});

const blockNumber = await client.getBlockNumber();
console.log("Latest block:", blockNumber);

BSC API vs. BSC data APIs: what's the difference?

When searching for "api bsc", you'll also find products like Bitquery or Moralis that offer GraphQL or REST APIs for BSC. These are not the same as the BSC JSON-RPC API.

  • BSC JSON-RPC API: The raw interface to the blockchain. You can read state, send transactions, and query logs, but you have to do your own indexing and aggregation.
  • BSC data APIs: Pre-indexed, parsed data (trades, balances, token transfers) delivered via GraphQL or REST. Great for analytics and dashboards, but not for submitting transactions.

If you're building a dApp that needs to send transactions or read live state, you need the JSON-RPC API. If you're building an analytics dashboard, a data API might save you time.

Choosing a BSC API provider

When you move beyond public endpoints, you'll evaluate RPC providers. Here's what to compare:

CriterionWhat to checkWhy it matters
ReliabilityUptime history, error ratesDowntime breaks your app
Rate limitsRequests per second, daily limitsToo low and you'll hit errors under load
Data availabilityArchive data, trace supportNeeded for historical queries and debugging
WebSocket supportReal-time subscriptionsEssential for live updates
PricingFree tier, pay-as-you-go, dedicated optionsMust fit your budget and scale
SupportDocumentation, community, SLAsHelps you resolve issues quickly

OnFinality provides both shared and dedicated BSC endpoints. You can see the full list of supported networks on the networks page.

Common pitfalls when using the BSC API

Even with a good provider, you'll run into issues. Here are the most common ones:

  • Rate limiting: Public endpoints often return 429 Too Many Requests. Use a managed provider or add retry logic.
  • Incorrect chain ID: BSC mainnet uses chain ID 56, testnet uses 97. Using the wrong one causes transaction failures.
  • Gas price too low: BSC gas prices can spike. Use eth_gasPrice or a gas oracle to set appropriate fees.
  • Pending transaction not found: If you query a transaction before it's mined, you'll get null. Poll or use WebSocket subscriptions.
  • Block finality: BSC has fast finality, but you should still check for reorgs if you're building financial apps.

Debugging BSC API calls

When something goes wrong, start with these steps:

  1. Check the endpoint: Is it reachable? Try a curl request.
  2. Check the method: Is the method name correct? Are the parameters in the right format?
  3. Check the error message: JSON-RPC errors include a code and message. Common codes: -32601 (method not found), -32000 (server error).
  4. Test with a public endpoint: If your provider fails, try a public endpoint to isolate the issue.
  5. Use a block explorer: Verify the transaction or block exists.

For more advanced debugging, you might need trace methods, which are only available on archive nodes or dedicated nodes.

BSC API and WebSocket subscriptions

For real-time updates, use WebSocket. Here's an example with ethers.js:

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

const provider = new ethers.WebSocketProvider("wss://YOUR_WS_URL");

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

WebSocket connections are more resource-intensive, so make sure your provider supports them and your plan includes them.

Key Takeaways

  • The BSC API is a JSON-RPC interface, EVM-compatible, and works with Ethereum tools.
  • Choose your endpoint type based on workload: public for testing, managed for production, dedicated for high throughput.
  • Compare providers on reliability, rate limits, data availability, and WebSocket support.
  • Watch out for rate limits, chain ID mismatches, and gas price issues.
  • Use WebSocket for real-time updates.

Frequently Asked Questions

What is the BSC API?

The BSC API is the JSON-RPC interface for BNB Smart Chain, allowing you to interact with the blockchain programmatically.

Is BSC API the same as Ethereum API?

Yes, BSC is EVM-compatible, so it uses the same JSON-RPC standard and methods as Ethereum.

How do I get a BSC API key?

You don't need a key for public endpoints, but for managed providers like OnFinality, you'll create an account and get an API key.

What is the BSC testnet API?

BSC testnet (chain ID 97) has its own RPC endpoints for testing. You can find them on the BNB testnet page.

Can I use ethers.js with BSC?

Yes, ethers.js works with BSC out of the box. Just set the provider to a BSC RPC URL.

What is the difference between BSC RPC and BSC API?

They are the same thing. RPC is the protocol, API is the interface. Both refer to the JSON-RPC endpoint.

How do I choose a BSC API provider?

Evaluate reliability, rate limits, data availability, WebSocket support, pricing, and support. See the RPC provider selection guide for more details.

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