GitHub Bitcoin Price Tracker API: Monitor BTC Value with Email Alerts

ยท

Overview

This Python-based Bitcoin Price Tracker API continuously monitors BTC/USD exchange rates using AlphaVantage data and delivers real-time email notifications every 60 seconds. Ideal for crypto traders and enthusiasts, this solution combines financial data integration with automated alert systems.

Key Features

Technical Implementation

Required Libraries

import requests  # API data fetching
import time      # Interval timing
import smtplib   # Email delivery
import ssl       # Secure connections

Core Components

  1. Email Notification System

    def send_email(crypto_symbol, current_price):
        # Configure SMTP server (Gmail example)
        email_from = "[email protected]"
        pswd = "your_password"
        email_to = "[email protected]"
        
        # Create secure SSL context
        context = ssl.create_default_context()
        
        try:
            with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
                server.login(email_from, pswd)
                message = f"Subject: {crypto_symbol} Price Alert\n\nCurrent {crypto_symbol} price: ${current_price}"
                server.sendmail(email_from, email_to, message)
            print("Email notification sent successfully")
        except Exception as e:
            print(f"Email sending failed: {str(e)}")
  2. Price Tracking Loop

    api_key = "YOUR_ALPHAVANTAGE_API_KEY"
    symbol = "BTC"
    
    while True:
        url = f"https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency={symbol}&to_currency=USD&apikey={api_key}"
        response = requests.get(url)
        data = response.json()
        
        if "Realtime Currency Exchange Rate" in data:
            current_price = data["Realtime Currency Exchange Rate"]["5. Exchange Rate"]
            print(f"Current BTC Price: ${current_price}")
            send_email(symbol, current_price)
        else:
            print("Error retrieving price data")
        
        time.sleep(60)  # 60-second interval

Security Recommendations

๐Ÿ‘‰ Best practices for API key management
Always store sensitive credentials like API keys and email passwords securely:

Customization Options

  1. Track Multiple Cryptocurrencies

    crypto_list = ["BTC", "ETH", "SOL"]
    for symbol in crypto_list:
        # Modify API call for each cryptocurrency
  2. Adjust Notification Frequency

    # Change sleep duration (in seconds)
    time.sleep(300)  # 5-minute intervals

FAQ Section

Q: How do I get an AlphaVantage API key?
A: Visit AlphaVantage's website and sign up for a free account. Free tier includes 25 API requests per day.

Q: Can I use this with non-Gmail email providers?
A: Yes, but you'll need to modify SMTP server settings (e.g., "smtp.office365.com" for Outlook).

๐Ÿ‘‰ Compare crypto exchange APIs
Q: Why is my script stopping after several hours?
A: This could be due to API rate limits. Consider adding error recovery logic or upgrading your API plan.

Advanced Implementation Tips

Remember to test your implementation thoroughly before deployment, and monitor script performance regularly for optimal results.