secret = 'YOUR_SECRET_KEY'
url = f'
try:
secret = 'YOUR_SECRET_KEY'
url = f'
try:
response = requests.get(url, headers={'x-api-key': api_key})
if response.status_code == 200:
data = json.loads(response.text)
return data['price']
otherwise:
print(f"Failed to get price for {symbol}. Status code: {response.status_code}")
return None
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return None
if __name__ == "__main__":
symbol = 'ETH'
price = get_eth_price(symbol)
if price is not None:
print(f"Last price for {symbol}: {price}")
Important Notes:
You need to replace YOUR_API_KEY
and YOUR_SECRET_KEY
with your actual Binance API credentials.
x-api-key
header is required to authenticate the request. Make sure you understand how to get an access token from Binance for this purpose.Code Explanation:
get_eth_price
function takes a symbol as input and creates an API endpoint URL using the symbol
, tokenSymbol
, and interval
parameters.Remember: Always check the Binance API documentation for the most up-to-date information on available symbols, pricing intervals, and authentication methods.