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 in Python at a given point in time.
Prerequisites
- Binance API key
- A library that can handle asynchronous data retrieval (e.g. “requests”, “aiohttp”)
- Python environment with the required libraries installed
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 fired when a new data point is received from the API.
import requests
from datetime import datetime
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"
while true:
try:
response = requests.get(api_url, headers={"x-api-key": api_key})
data = response.json()
Find the ETH price on the given datefor item in data["result"]:
if "timestamp" in item and int(item["timestamp"]) >= start_date_str:
return float(element["price"])
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
Method 2: Using `aiohttp'' and a loop
If you want a more asynchronous approach, we can use theaiohttp
library to retrieve data from the API.
import aiohttp
async def get_api_price(start_date_str, end_date_str):
async with aiohttp.ClientSession() as session:
api_url = f"
start_date = datetime.strptime(start_date_str, "%Y-%m-%d")
end_date = datetime.strptime(end_date_str, "%Y-%m-%d")
while start_date <= end_date:
try:
response = await session.get(api_url)
data = await response.json()
Find the ETH price on the given datefor item in data["result"]:
if "timestamp" in item and int(item["timestamp"]) >= start_date:
return float(item["price"])
except aiohttp.ClientResponseError as e:
print(f"Error: {e}")
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 Options
- Be sure to replace 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 an API request returns an unsuccessful or invalid response.
- If you need to perform further processing on the prices received, feel free to modify the callback functions accordingly.