Binance (BNB) - N/A Real-Time Price API integration guide for Python applications
You need a reliable real-time Binance Coin (BNB) price feed inside a Python app to quote, convert, or risk-check transactions. By the end of this guide you will poll BNB/USD using the Metals-API latest endpoint, parse USDBNB to get the USD price per 1 BNB, handle units and timestamps correctly, and add simple caching so you don’t overcall the API.
What you’ll implement
We’ll focus on a minimal, production-aware integration that:
- Fetches the current BNB price using the latest endpoint.
- Extracts rates.BNB (BNB per USD) and rates.USDBNB (USD per BNB) correctly.
- Converts amounts between USD and BNB in Python without floating-point drift.
- Handles timestamps, base currency, rounding, and caching practices you can ship today.
If you need broader coverage or different assets, consult the full Metals-API Documentation and the current catalog on the Symbols page.
Before you start: symbols, units, and base
- Symbol: BNB
- Base currency: USD by default (rates are quoted relative to USD)
- Rates meaning for crypto symbols:
- rates.BNB = BNB per 1 USD
- rates.USDBNB = USD per 1 BNB (this is the spot price you’ll show users)
- Timestamps: Unix epoch seconds, UTC
Find supported symbols and their definitions on Metals-API Supported Symbols. If BNB is visible in your plan, you can query it directly as shown below.
Get your API key
You need an API key to call the endpoint from your app or server. Create an account here: Register. For coverage planning and product fit, review MCP.
Real-time BNB price: latest endpoint
The latest endpoint returns real-time exchange rates. Update frequency depends on your subscription plan. For live quoting, poll at a cadence that matches your plan’s update interval and your UX needs.
curl request (copy-paste)
curl -s "https://metals-api.com/api/latest?access_key=YOUR_API_KEY&symbols=BNB"
Official JSON response example (BNB)
{"success":true,"timestamp":1790813280,"date":"2026-10-01","base":"USD","rates":{"BNB":0.0013024564328323,"USD":1,"USDBNB":767.7800000000128}}
What these fields mean in practice
- success: boolean indicating the request status. Check it before using the data.
- timestamp: 1790813280 (Unix seconds, UTC). Useful for cache freshness and audit logs.
- date: "2026-10-01" (UTC calendar date at the time of the quote).
- base: "USD" (all rates are relative to USD).
- rates.BNB: 0.0013024564328323 (BNB per 1 USD). Multiply by a USD amount to get BNB.
- rates.USDBNB: 767.7800000000128 (USD per 1 BNB). This is the price you typically display.
- rates.USD: 1 (USD per USD).
Python: fetch and use USDBNB safely
This Python example calls the same latest endpoint, extracts the BNB price in USD (rates.USDBNB), converts between USD and BNB, and applies sensible rounding. It also shows a simple cache TTL you can adapt to your plan’s update interval.
import os
import time
import requests
from decimal import Decimal, ROUND_HALF_UP
API_KEY = os.getenv("METALS_API_KEY", "YOUR_API_KEY")
URL = "https://metals-api.com/api/latest"
SYMBOLS = "BNB"
# Simple in-memory cache
_cache_data = None
_cache_expiry = 0
def get_bnb_quote(ttl_seconds=30):
global _cache_data, _cache_expiry
now = time.time()
if _cache_data and now < _cache_expiry:
return _cache_data
resp = requests.get(URL, params={"access_key": API_KEY, "symbols": SYMBOLS}, timeout=10)
resp.raise_for_status()
data = resp.json()
if not data.get("success"):
raise RuntimeError(f"Metals-API error: {data}")
# Required fields
rates = data["rates"]
usdbnb = Decimal(str(rates["USDBNB"])) # USD per 1 BNB
bnb_per_usd = Decimal(str(rates["BNB"])) # BNB per 1 USD
ts = int(data["timestamp"])
base = data["base"]
# Minimal object you can pass around your app
quote = {
"price_usd_per_bnb": usdbnb,
"bnb_per_usd": bnb_per_usd,
"timestamp": ts,
"base": base
}
# Cache according to your plan's update interval
_cache_data = quote
_cache_expiry = now + ttl_seconds
return quote
def usd_to_bnb(usd_amount, price_usd_per_bnb):
# BNB = USD / (USD per BNB)
return (Decimal(str(usd_amount)) / price_usd_per_bnb).quantize(Decimal("0.00000001"), rounding=ROUND_HALF_UP)
def bnb_to_usd(bnb_amount, price_usd_per_bnb):
# USD = BNB * (USD per BNB)
return (Decimal(str(bnb_amount)) * price_usd_per_bnb).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
if __name__ == "__main__":
quote = get_bnb_quote(ttl_seconds=30)
price = quote["price_usd_per_bnb"]
print(f"USDBNB (USD per 1 BNB): {price}")
# Example conversions
print("0.25 BNB in USD:", bnb_to_usd(0.25, price))
print("100 USD in BNB:", usd_to_bnb(100, price))
Interpreting and converting values correctly
- To display the live price: use rates.USDBNB directly.
- To convert USD → BNB: BNB_amount = USD_amount × rates.BNB, or USD_amount / rates.USDBNB.
- To convert BNB → USD: USD_amount = BNB_amount × rates.USDBNB.
- Rounding: financial UIs typically show 2 decimals in USD and up to 6–8 decimals in crypto. Internally, use Decimal to avoid floating-point drift.
Choosing endpoints for a Python pricing workflow
For a real-time BNB price, you mainly need the latest endpoint. If you also need historical performance or change over a range, consider these two (link to docs, no redundant examples here):
- Historical rates: backfill a chart or validate a transaction price post-trade. See the Historical endpoint in the Documentation.
- Time-series or Fluctuation: compute day-over-day changes and analytics for a period. Details in Documentation.
Keep all examples in your code focused on BNB to avoid mixing units or symbols accidentally. You can always audit the exact available symbols in your plan via the Symbols page.
Field reference you’ll actually use
| Field | Meaning | Usage in Python |
|---|---|---|
| success | Request status | Guardrail; fail fast if false |
| timestamp | Unix epoch seconds (UTC) | Staleness checks, cache keys, analytics |
| date | UTC calendar date | Auditing and display |
| base | Quote base (USD) | Validation; interpret all rates relative to USD |
| rates.BNB | BNB per USD | USD → BNB conversions |
| rates.USDBNB | USD per BNB | Display price and BNB → USD conversions |
Production tips that save time
- Update cadence: The latest endpoint’s refresh frequency depends on your subscription plan. Poll accordingly, and cache for at least the interval between updates to avoid redundant calls.
- Crypto trades 24/7: Unlike traditional metals that may reflect market session nuances, BNB has no weekend closures. Still, the API refresh is plan-based; do not assume every second updates.
- Cache and retries:
- Use a TTL-based cache keyed by symbol and possibly by rounded timestamp window.
- On transient failure, serve the last known good quote with an “as-of” label and re-try in the background.
- Precision: Use Decimal in Python, especially when computing PnL, converting sizes, or aggregating fills.
- Monitoring: Log success, timestamp drift, and the delta between successive USDBNB values; alert if the feed is stale beyond a threshold you define.
- Display hygiene: Round display values, but compute with full precision. Keep internal precision at least 8 decimals for BNB and 2+ decimals for USD.
- Security: Load your API key from environment variables or your secret manager. Never commit keys to source control.
Troubleshooting and validation
- Symbol support: Verify BNB availability and symbol spelling on the Symbols page in your plan context.
- Sanity checks: Validate that 1 / rates.BNB ≈ rates.USDBNB within acceptable rounding to catch data anomalies early.
- Clock handling: Treat timestamp as UTC; convert to your local display timezone only when formatting for users.
- Error handling: Always check success. When false, log the payload for diagnosis and back off.
Extending your Python integration
If you need more than a single-point quote, these patterns help without overhauling your stack:
- Snapshot store: Persist the latest successful BNB quote (USDBNB, timestamp) in Redis or your SQL store to serve quickly and survive restarts.
- Derived analytics: Compute rolling averages or volatility from periodic polls. For full historical series, use the appropriate historical/time-series endpoints described in the Documentation.
- Backtesting: When backfilling, record the timestamp returned by the API along with your own ingestion time for reproducibility.
Quality assurance checklist
- Do you parse and check success, base, and timestamp before using rates?
- Are you using USDBNB for price display and BNB or USDBNB appropriately for conversions?
- Is your cache TTL aligned with your plan’s update frequency?
- Are you using Decimal or another precise numeric type for monetary math?
- Do you log the as-of timestamp and the raw JSON for audits?
Related resources
- BNB market overview (reference): Binance BNB Price Page
- API docs and examples: Metals-API Documentation
- Symbol catalog and coverage: Metals-API Supported Symbols
FAQ
1) Which field is the BNB price I should show to users?
Use rates.USDBNB (USD per 1 BNB). This is the canonical price for display and for BNB → USD conversions.
2) How do I convert a USD amount into BNB?
Either USD / rates.USDBNB or USD × rates.BNB (they are reciprocals). Use Decimal and round to an appropriate number of BNB decimals for display.
3) How fresh is the “latest” BNB price?
The latest endpoint updates at a cadence tied to your subscription plan. Cache responses at least for that interval and monitor the timestamp field to avoid stale quotes.
4) What timezone is the date and timestamp?
The timestamp is Unix epoch seconds in UTC. The date string corresponds to the UTC calendar date of the quote.
5) Can I request BNB together with other symbols?
You can query symbols according to your plan’s coverage. Check the exact availability and naming on the Symbols page and use the symbols parameter accordingly.
Ready to plug in BNB pricing to your Python stack? Create your API key and start integrating now: Register. For coverage details and planning, visit MCP.