Search code examples
pythonrestrequestpython-requestsbinance

Request .get how to use a string as a parameter


I believe it may have something to do with the **kwarg param but I have no idea how to work with that,

To specify I am trying to use the Binanace rest API to simply get the avg price for an altcoin and I can not understand how to specify which ticker to look at

Link to the specific code I’m trying to work https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#current-average-price

Any answers would be appreciated I’m sure it will all be the same but I’m looking for the answer in python

Code:

binance = "https://api.binance.com"

print(requests.get(binance + "/api/v1/ticker/24hr", "BTC"))

Output:

Status code [400] 

Expected [BTC price avg for the last 24 hour (or any numbers really)]

 "mins": 5,
 "price": "9.35751834"
 / or some equivalent 

But despite me trying that and failing i am unable how to understand this api for what ever reason, i used just one before so eh,

i tried

print( binance + " /api/v3/ticker/price" )

Output:

Status code[200]

i understand that this is an success message but i do know know to how to access the data,

anyhelp would be appreciated thanks


Solution

  • As per documentation on GitHub, you need to make a GET request with string parameter 'symbol'.
    Try this code:

    import requests
    binance = "https://api.binance.com"
    avg_price = "/api/v3/avgPrice"
    
    response = requests.get(binance + avg_price + "?symbol=LTCBTC")
    print(response.text)
    

    This should get you output like below:

    {"mins":5,"price":"0.00918091"}