CoingGecko coins: https://api.coingecko.com/api/v3/coins/list, look for the ids in this url and use them in quantities dict, here are some popular currencies:
- bitcoin
- ethereum
- ripple
- binancecoin
- dogecoin
Change the currency variable if necessary, some popular options are:
- usd: US Dollar
- eur: Euro
- gbp: British Pound
- jpy: Japanese Yen
- aud: Australian Dollar
- btc: Bitcoin
- eth: Ethereum
- usdt: Tether
- bnb: Binance Coin
- xrp: XRP
pip install requests
import requests
def get_crypto_prices(cryptos, currency='eur'):
crypto_ids = ','.join(cryptos)
url = f'https://api.coingecko.com/api/v3/simple/price?ids={crypto_ids}&vs_currencies={currency}'
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
return {crypto: data[crypto][currency] for crypto in cryptos}
except requests.RequestException as e:
print(f"Error: {e}")
return None
currency = 'eur'
quantities = {
'bitcoin': 0.00114522,
'ripple': 319.33343233,
'stellar': 449.44127517
}
cryptos = list(quantities.keys())
prices = get_crypto_prices(cryptos, currency)
if prices:
total_value = 0
for crypto, amount in quantities.items():
price = prices[crypto]
value = amount * price
total_value += value
print(f"{amount} {crypto.capitalize()} worth {value:.2f} {currency.upper()}.")
print(f"Total: {total_value:.2f} {currency.upper()}.")
Python
06 Jan. 2025
|
Last Updated: 22 Nov. 2025
|
jaimedcsilva Related