Copy ##### 1. Import dependencies #####
import requests
import pandas as pd
##### 2. Choose the value of the query's parameters #####
# ---- Required parameters ---- #
pool_address = "0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852"
# ---- Optional parameters ---- #
blockchain = "ethereum"
live = "false"
start_block = "129870"
end_block = "129880"
start_time = None
end_time = None
sort = "desc"
page_size = 100
# ---- API key configuration ---- #
api_key = "YOUR_API_KEY"
##### 3. Get the data #####
# ---- Function to run an API call ---- #
# Get the data in a dataframe --------- #
def get_kaiko_data(api_key: str, pool_address: str, blockchain: str, live: str, start_block: str, end_block: str, start_time: str, end_time: str, sort: str, page_size: int):
headers = {'Accept': 'application/json', 'X-Api-Key': api_key}
url = f'https://eu.market-api.kaiko.io/v2/data/liquidity.v1/snapshots'
params = {
"pool_address": pool_address,
"blockchain": blockchain,
"live": live,
"start_block": start_block,
"end_block": end_block,
"start_time": start_time,
"end_time": end_time,
"sort": sort,
"page_size": page_size
}
try:
res = requests.get(url, headers=headers, params=params)
res.raise_for_status()
data = res.json()
if 'data' not in data:
print("No data returned.")
return pd.DataFrame()
df = pd.DataFrame(data['data'])
# Handle pagination with continuation token
while 'next_url' in data:
next_url = data['next_url']
if next_url is None:
break
res = requests.get(next_url, headers=headers)
res.raise_for_status()
data = res.json()
if 'data' in data:
df = pd.concat([df, pd.DataFrame(data['data'])], ignore_index=True)
return df
except requests.exceptions.RequestException as e:
print(f"API request error: {e}")
return pd.DataFrame()
# ---- Get the data ---- #
df = get_kaiko_data(api_key=api_key, pool_address=pool_address, blockchain=blockchain,
live=live, start_block=start_block, end_block=end_block,
start_time=start_time, end_time=end_time, sort=sort,
page_size=int(page_size))