Fetching Ethereum Price from Binance API in Python

========================================================

In this article, we will go through a step-by-step guide on how to store and update the latest price of Ethereum (ETH) using the Binance API in Python.

Prerequisites

————–

  • You have installed the requests library (pip install request) and the Binance API client library for Python (pip install binance-client)
  • You have a Binance API account with sufficient balance and permissions to access the ETH market
  • You have set up your API credentials in your Python script

Code

——

import os

import json

from binance.client import Client


Set up your Binance API credentials

API_KEY = 'your_api_key'

API_SECRET = 'your_api_secret'


Create a new Binance client instance with your API credentials

client = Client(api_key=API_KEY, api_secret=API_SECRET)

def get_latest_eth_price():

"""

Get the latest ETH price from Binance API.

Returns:

tuple: the latest price of ETH and its corresponding exchange symbol (e.g. 'ETHUSDT')

"""


Get the list of available exchanges

exchanges = client.get_exchanges()


Filter the list to include only Ethereum (ETH) exchange

eth_exchange = [exchange by exchange in exchanges if exchange['symbol'] == 'ETH']


If no ETH exchange found, raise an error

if not eth_exchange:

raise ValueError('No ETH exchange found')


Get the latest price of ETH exchange

last_price = eth_exchange[0]['lastPrice']

return last_price

def store_latest_eth_price(price):

"""

Store the latest ETH price to a file.

Arguments:

price (float): The last price of ETH to be stored.

"""

with open('latest_eth_prices.json', 'w') as f:

json.dump({'latestPrice': price, 'symbol': 'ETHUSDT'}, f)

main() definition:


Get the latest ETH price

last_price = get_last_eth_price()


Store the price to a file

store_latest_eth_price(last_price['lastPrice'])

print(f'Last ETH price stored: {latest_price["latestPrice"]} USD')

if __name__ == '__main__':

play()

Explanation

————–

  • In the get_latest_eth_price function, we create a new Binance client instance with our API credentials.
  • We then filter the list of available exchanges for Include only Ethereum (ETH) exchange using client.get_exchanges() method.
  • If no ETH exchange is found, we throw an error.
  • We get the latest price from the ETH exchange and store it in a dictionary for later use.
  • In the store_latest_eth_price function, we open a file called latest_eth_prices.json in write mode ('w') and dump our latest ETH price and symbol (ETHUSDT) to the file using JSON format.
  • Finally, in the main function, we call the get_latest_eth_price function to retrieve the latest ETH price and store it in a file.

Example use case

——————-

To use this script, save it as a Python file (e.g. eth_price_tracker.py) and run it using your Binance API credentials. The script will print the latest ETH price stored in the file. You can modify the script to suit your needs by adding additional functions or error handling.

Note: Make sure to replace your_api_key and your_api_secret with your actual Binance API credentials.

ethereum deflation destroy