In python, these 3 functions work to fetch price data for 1 hour, 4 hour or 1 day:
import requests
import time
from typing import List, Dict, Any
import os
from dotenv import load_dotenv
load_dotenv()
#TOKEN_ADDRESS = “ED5nyyWEzpPPiWimP8vYm7sD7TD3LAt3Q3gRTWHzPJBY” - moodeng for testing
BASE_URL = “https://public-api.birdeye.so/defi/history_price"
def fetch_price_data(time_from: int, time_to: int, interval: str, token_address: str) -> List[Dict[str, Any]]:
url = f”{BASE_URL}?address={token_address}&address_type=token&type={interval}&time_from={time_from}&time_to={time_to}”
headers = “accept”: “application/json”,
“X-API-KEY”: os.getenv(“BIRDEYE_API_KEY”) # Replace with your actual API key
}
print(os.getenv(“BIRDEYE_API_KEY”) )
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()[‘data’][‘items’]
def get_live_chart(token_address: str) -> List[Dict[str, Any]]:
time_to = int(time.time())
time_from = time_to - 3600 # 1 hours ago
return fetch_price_data(time_from, time_to, “1m”, token_address)
def get_4h_chart(token_address: str) -> List[Dict[str, Any]]:
time_to = int(time.time())
time_from = time_to - 4 * 3600 # 4 hours ago
return fetch_price_data(time_from, time_to, “1m”, token_address)
def get_1d_chart(token_address: str) -> List[Dict[str, Any]]:
time_to = int(time.time())
time_from = time_to - 24 * 3600 # 1 day ago
return fetch_price_data(time_from, time_to, “15m”, token_address)
HOWEVER, when i tried to do the same for 1w, 1 month or all time, it show 400 client error: bad request:
def get_1w_chart(token_address: str) -> List[Dict[str, Any]]:
time_to = int(time.time())
time_from = time_to - 7 24 3600 # 1 week ago
return fetch_price_data(time_from, time_to, “1h”, token_address)
def get_1m_chart(token_address: str) -> List[Dict[str, Any]]:
time_to = int(time.time())
time_from = time_to - 30 24 3600 # 1 month ago
return fetch_price_data(time_from, time_to, “6h”, token_address)
def get_max_chart(token_address: str) -> List[Dict[str, Any]]:
time_to = int(time.time())
time_from = 0 # From the beginning
return fetch_price_data(time_from, time_to, “1w”, token_address)
For API support, please contact: https://t.me/thao191101