DEV Community

Lucas Gragg
Lucas Gragg

Posted on

Email alerts when products drop in price

I've always been frustrated when I buy something online, only to see the price drop a few days later. To combat this, I started experimenting with automated price tracking scripts that could alert me when a product's price falls below a certain threshold. After some trial and error, I settled on a Python-based approach using the Amazon Product Advertising API. This allowed me to fetch current prices and compare them to historical data, triggering an email alert when a price drop was detected.

The Script

My initial script was pretty straightforward. I used the boto3 library to interact with the Amazon API, and smtplib to send emails via a Gmail account. Here's a simplified example of how I structured the code:

import boto3
import smtplib
from email.mime.text import MIMEText

# Amazon API credentials
access_key = 'YOUR_ACCESS_KEY'
secret_key = 'YOUR_SECRET_KEY'

# Gmail credentials
gmail_user = 'YOUR_GMAIL_USER'
gmail_pass = 'YOUR_GMAIL_PASS'

# Set up Amazon API client
client = boto3.client('execute-api', aws_access_key_id=access_key, aws_secret_access_key=secret_key)

# Define the product we're tracking
product_id = 'B076MX9VG9'
target_price = 50.0

# Fetch current price from Amazon
response = client.get_item(ItemId=product_id)
current_price = float(response['Item']['LowestNewPrice']['FormattedPrice'].strip('$'))

# Check if price has dropped below target
if current_price <= target_price:
    # Send email alert
    msg = MIMEText(f'Price alert: {product_id} is now ${current_price:.2f}')
    msg['Subject'] = 'Price Drop Alert'
    msg['From'] = gmail_user
    msg['To'] = gmail_user

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(gmail_user, gmail_pass)
    server.sendmail(gmail_user, gmail_user, msg.as_string())
    server.quit()
Enter fullscreen mode Exit fullscreen mode

This script worked reasonably well, but I quickly realized I needed to add more features, like support for multiple products and a way to visualize price history.

Adding More Features

To make the script more useful, I started adding features like wishlist import, bulk product tracking, and daily digest emails. I also built a simple web scraper to fetch price history data, which I stored in a local database. This allowed me to generate price charts and trend analysis reports. As the script grew in complexity, I started to think about packaging it into a reusable tool that others could use.

Putting it all Together

I actually packaged this into a tool called Amazon Price Tracker if you want the full working version. It's a pretty robust application that supports all the features I mentioned earlier, plus a few more. You can find it at https://lukegraggster.gumroad.com/l/amazon-price-tracker?utm_source=devto&utm_medium=blog&utm_campaign=traffic_bot for $39. One of the things I'm most proud of is the real-time price monitoring feature, which uses a combination of API calls and web scraping to fetch current prices and trigger alerts instantly. I've also included a bunch of example code and documentation to help you get started. Overall, I'm pretty happy with how the project turned out, and I hope others find it useful for tracking prices and saving money on their online purchases.

Also available on Payhip with instant PayPal checkout.


If you need a server to run your bots 24/7, I use DigitalOcean — $200 free credit for new accounts.

Top comments (0)