How to get btc rate against usd using binance api ?

Hi, I have checked the api docs throughly but I dont know where is the endpoint to get the rate of e.g btc to usd or eth to usd ? Everywhere there is btc to USDT and not USD … Please guide me

Hi, there is no symbol trading in USD on binance.com, so you can’t get the coin rate in USD. You may get the rate of USDT to USD from third party, which can be helpful for you.

I got the solution, this is the required endpoint to get btc/etc or any othey coin rate to USD.
{{url}}/sapi/v1/portfolio/asset-index-price?asset=BTC

it gives response like this
[
{
“asset”: “BTC”,
“assetIndexPrice”: “42584.3643268”,
“time”: 1705505244045
}
]

Try the following code

import requests

apis = {
    "BINANCE": {
        "base": "https://api.binance.com",
        "endpoint": "/api/v3/avgPrice"
    },
    "BITSTAMP": {
        "base": "https://www.bitstamp.net",
        "endpoint": "/api/v2/ticker/"
    }
}

def get_binance_price(symbol):
    base = apis['BINANCE']['base']
    endpoint = apis['BINANCE']['endpoint']
    price = 0.0
    payload = {
        "symbol": symbol
    }
    url = f"{base}{endpoint}"
    r = requests.get(url, payload)
    if r.status_code == 200:
        d = r.json()
        if "price" in d:
            price = float(d['price'])
        else:
            print(d)
    return price

def get_bitstamp_price(symbol):
    s = f"{symbol[:-4]}{symbol[-4:]}"
    base = apis['BITSTAMP']['base']
    endpoint = apis['BITSTAMP']['endpoint']
    price = 0.0
    url = f"{base}{endpoint}"
    r = requests.get(url)
    if r.status_code == 200:
        d = r.json()
        for item in d:
            pair = item['pair']
            if pair == s:
                price = float(item['last'])
    return price

def price_of(symbol):
    usdt = get_binance_price(symbol)
    if usdt > 0.0:
        usd = get_bitstamp_price(f"USDT/USD")
        if usd > 0.0:
            return usdt, usd

if __name__ == "__main__":
    symbol = "BTCUSDT"
    btc_usdt, usd_usdt = price_of(symbol)
    print(f"BTCUSDT : {btc_usdt:,.3f}")
    print(f"USDTUSD : {usd_usdt:,.4f}")
    print(f"BTCUSD  : {btc_usdt * usd_usdt:,.3f}")