Python
Fetch Crypto Prices with Python APIs
Get real-time cryptocurrency prices using Python. CoinGecko, Binance, and CoinMarketCap API tutorials with code examples.
Getting Started
pip install requests pandasCore Implementation
import requests
import pandas as pd
# CoinGecko API (free, no key needed)
def get_crypto_prices(coins=["bitcoin", "ethereum", "solana"]):
url = "https://api.coingecko.com/api/v3/simple/price"
params = {
"ids": ",".join(coins),
"vs_currencies": "gbp,usd",
"include_24hr_change": "true",
"include_market_cap": "true"
}
response = requests.get(url, params=params)
data = response.json()
rows = []
for coin, info in data.items():
rows.append({
"Coin": coin.title(),
"GBP": f"£{info['gbp']:,.2f}",
"USD": f"${info['usd']:,.2f}",
"24h Change": f"{info.get('gbp_24h_change', 0):.2f}%",
"Market Cap": f"£{info.get('gbp_market_cap', 0)/1e9:.1f}B"
})
return pd.DataFrame(rows)
df = get_crypto_prices()
print(df.to_string(index=False))Extended Example
# Historical price data from CoinGecko
def get_crypto_history(coin="bitcoin", days=365, currency="gbp"):
url = f"https://api.coingecko.com/api/v3/coins/{coin}/market_chart"
params = {"vs_currency": currency, "days": days}
response = requests.get(url, params=params)
data = response.json()
df = pd.DataFrame(data["prices"], columns=["timestamp", "price"])
df["date"] = pd.to_datetime(df["timestamp"], unit="ms")
df = df.set_index("date").drop("timestamp", axis=1)
return df
btc = get_crypto_history("bitcoin", 365)
eth = get_crypto_history("ethereum", 365)
print(f"Bitcoin - Current: £{btc['price'].iloc[-1]:,.0f}")
print(f"Bitcoin - 1Y High: £{btc['price'].max():,.0f}")
print(f"Bitcoin - 1Y Low: £{btc['price'].min():,.0f}")
print(f"Bitcoin - 1Y Return: {((btc['price'].iloc[-1] / btc['price'].iloc[0]) - 1) * 100:.1f}%")