python-3.xbinancecryptocurrencybinance-api-client

Why can't get price pair (USDT/KGS, USDT/KZT) by ticker in BINANCE API?


`So, this is my code

# Import libraries
import json
import requests
  
# defining key/request url
key = "https://api.binance.com/api/v3/ticker/price?symbol=USDTKGS"
  
# requesting data from url
data = requests.get(key)  
data = data.json()
print(f"{data['symbol']} price is {data['price']}")

But for some reason I get this error:

Traceback (most recent call last):
  File "rate.py", line 11, in <module>
    print(f"{data['symbol']} price is {data['price']}")
KeyError: 'symbol'

Probably, this pair doesn't exist, but what to do in such situation? I need to get the pair by API, but don't see any other ways to do so... Please, help me!

I tried to use usual pairs like USDT/UAH, EUR/USDT - they work But USDT/KGS, USDT/KZT doesn't work - they print error, but I need to get it


Solution

  • This thing helped me

    def fetch_price(asset: str, fiat: str, trade_type: str, pay_type: str):
        """
        Function for getting Rate on Binance P2P.
        """
        headers = {
            "Accept": "*/*",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8",
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "content-type": "application/json",
            "Host": "p2p.binance.com",
            "Origin": "https://p2p.binance.com",
            "Pragma": "no-cache",
            "TE": "Trailers",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0"
        }
    
        data = {
            "asset": asset,
            "fiat": fiat,
            "merchantCheck": True,
            "page": 1,
            "payTypes": [pay_type],
            "publisherType": None,
            "rows": 3,
            "tradeType": trade_type
        }
    
        try:
            r = requests.post('https://p2p.binance.com/bapi/c2c/v2/friendly/c2c/adv/search', headers=headers, json=data).json()
    
            if r['data']:
                return r['data'][2]['adv']['price']
            else:
                print(f"No data available for {asset}/{fiat}")
                return None
        except Exception as e:
            print(f"An error occurred: {e}")
            return None