Ethereum: Accurate Binance API Prices from Historical Data
As a Python developer, you probably know the importance of accurate and up-to-date Ethereum price data. In this article, we will look at how to get accurate Binance API prices at specific times in Python.
Prerequisites
- Binance API key
- A library that can handle asynchronous data retrieval (e.g. « requests », « aiohttp »)
- Python environment with the required libraries
Method 1: Using « Requests » and a callback function
We will use the requests library to send HTTP requests to the Binance API. We will create a callback function that will be executed every time a new data point is received from the API.
Import Requests
Import date from date
def get_api_price(start_date_str, end_date_str):

Set your Binance API key and API endpoint URL hereapi_key = "YOUR_API_KEY"
api_url = f"
When true:
Try:
response = request.get(api_url, headers={"x-api-key": api_key})
data = response.json()
Find the price of ETH on a given dayFor the item in data["result"]:
If item is "timestamp" and int(item["timestamp"]) >= start_date_str:
return float(element["price"])
requests.exceptions.RequestException as e:
print(f"Error: {e}")
Method 2: Usingaiohttpand a loop
If you want a more asynchronous approach, you can use theaiohttplibrary to retrieve data from the API.
import aiohttp
async def get_api_price(start_date_str, end_date_str):
Asynchronously as a session using aiohttp.ClientSession():
api_url = f"
start_date = datetime.strptime(start_date_str, "%Y-%m-%d")
end_date = datetime.strptime(end_date_str, "%Y-%m-%d")
start_date <= end_date:
try:
response = await session.get(api_url)
data = await response.json()
Find the price of ETH on a given dayFor item data["result"]:
If "timestamp" is in "timestamp" and int(item["timestamp"]) >= start_date:
return float(item["price"])
aiohttp.ClientResponseError as e:
print(f"Error: {e}"), except
Usage example:start_date_str = "2022-01-01"
end_date_str = "2022-12-31"
get_api_price(start_date_str, end_date_str)
Tips and Variations
- Make sure to replace the key « YOUR_API_KEY » with your actual Binance API key.
- In both examples, you can change the « limit » parameter to get more or fewer data points.
- Consider adding error handling for cases where the API request fails or returns an invalid response.
- If you need to perform further processing on the quotes you receive, feel free to modify the callback functions accordingly.