# 1. Get API key from https://www.iqair.com/dashboard/api
# 2. Install dependencies
pip install -r requirements.txt
# 3. Create .env file and add your API key
copy .env.example .env
# Edit .env and add: IQAIR_API_KEY=your_key_here
# 4. Run the app
python main.pyA beginner-friendly Python application that shows you how polluted the air is in any city around the world! This project teaches you how to:
- Build a graphical user interface (GUI) with Tkinter
- Make API calls to get real-time data
- Process and display information beautifully
- Handle errors gracefully
- Use environment variables to keep API keys secure π
Perfect for: Students learning Python, API integration, GUI development, and security best practices
AQI (Air Quality Index) is a number that tells you how clean or polluted the air is:
- 0-50: π’ Good - Go outside and play!
- 51-100: π‘ Moderate - Air is okay for most people
- 101-150: π Unhealthy for sensitive groups - Be careful if you have asthma
- 151-200: π΄ Unhealthy - Everyone should limit outdoor time
- 201-300: π£ Very Unhealthy - Avoid going outside
- 301+: π€ Hazardous - Stay indoors!
- π Real-time AQI data from IQAir API
- π Auto-detect location based on IP
- π¨ Color-coded AQI display (Good to Hazardous)
- π Detailed pollutant info (PM2.5, PM10, O3, NO2, SO2, CO)
- π‘οΈ Weather conditions (temperature, humidity, pressure, wind)
- π‘ Health recommendations based on AQI levels
- πΊπΈπ¨π³ Dual AQI standards (US EPA & China MEP)
- π Last updated timestamp
- Language: Python
- Libraries:
tkinter(GUI)requests==2.31.0(API calls)
- API: IQAir API (free tier included)
- Complexity: Intermediate
- Visit IQAir Dashboard: Go to https://www.iqair.com/dashboard/api
- Sign Up/Login:
- Click "Get Started" or "Sign In"
- Create a free account (use your email)
- Verify your email address
- Get API Key:
- Once logged in, you'll see your dashboard
- Look for "API Key" section
- Copy your API key (it looks like:
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
# 1. Install dependencies
pip install -r requirements.txt
# 2. Create .env file
# Copy the example file
copy .env.example .env
# 3. Add your API key to .env file
# Open .env in a text editor and replace 'your_api_key_here' with your actual API key
# Example:
# IQAIR_API_KEY=e6fa641d-6bc7-48db-94c8-f3f1e4a1a7acpython main.pyImportant: Never share your .env file or commit it to GitHub! It contains your secret API key.
- Enter City (e.g., "Beijing")
- Enter State (e.g., "Beijing")
- Enter Country (e.g., "China")
- Click "π Check AQI"
- Click "π Use My Location"
- App auto-fills your location based on IP
- Displays AQI instantly!
π URL: https://www.iqair.com/dashboard/api
βββββββββββββββββββββββββββββββββββββββ
β IQAir Air Quality API β
βββββββββββββββββββββββββββββββββββββββ€
β β
β [Get Started] or [Sign In] β
β β
β Email: [email protected] β
β Password: β’β’β’β’β’β’β’β’β’β’ β
β β
β [ Create Account ] β
β β
βββββββββββββββββββββββββββββββββββββββ
- Check your email inbox
- Click the verification link
- Return to IQAir dashboard
βββββββββββββββββββββββββββββββββββββββ
β Dashboard β
βββββββββββββββββββββββββββββββββββββββ€
β β
β π API Statistics β
β ββ Calls Today: 0/10,000 β
β ββ Calls This Month: 0/10,000 β
β β
β π Your API Key: β
β ββββββββββββββββββββββββββββββββββ
β β xxxxxxxx-xxxx-xxxx-xxxx-xxxxx ββ β Copy this!
β ββββββββββββββββββββββββββββββββββ
β [Copy to Clipboard] β
β β
βββββββββββββββββββββββββββββββββββββββ
# Open .env file in any text editor (Notepad, VS Code, etc.)
# Replace 'your_api_key_here' with your actual key
# Before:
IQAIR_API_KEY=your_api_key_here
# After:
IQAIR_API_KEY=e6fa641d-6bc7-48db-94c8-f3f1e4a1a7ac- β 10,000 calls per month
- β Real-time data
- β 30,000+ stations worldwide
- β No credit card required
β DO:
- Keep your API key in
.envfile - Add
.envto.gitignore - Never share your API key publicly
β DON'T:
- Commit
.envto GitHub - Share your API key in screenshots
- Hardcode API key in source code
| AQI Range | Category | Color | Health Impact |
|---|---|---|---|
| 0-50 | Good | π’ Green | Satisfactory |
| 51-100 | Moderate | π‘ Yellow | Acceptable |
| 101-150 | Unhealthy for Sensitive Groups | π Orange | Sensitive groups affected |
| 151-200 | Unhealthy | π΄ Red | Everyone affected |
| 201-300 | Very Unhealthy | π£ Purple | Health alert |
| 301+ | Hazardous | π€ Maroon | Emergency conditions |
Try these cities:
- Beijing, Beijing, China
- Los Angeles, California, USA
- Delhi, Delhi, India
- London, England, United Kingdom
- Tokyo, Tokyo, Japan
- PM2.5: Fine particulate matter (< 2.5 micrometers)
- PM10: Coarse particulate matter (< 10 micrometers)
- O3: Ground-level ozone
- NO2: Nitrogen dioxide
- SO2: Sulfur dioxide
- CO: Carbon monoxide
- Real-time temperature, humidity, pressure
- Wind speed and direction
- Synchronized with pollution data
import tkinter as tk # Creates windows, buttons, labels
import requests # Makes API calls to get data from internet
import os # Access environment variables
from dotenv import load_dotenv # Load .env file
load_dotenv() # This reads the .env fileWhat are Environment Variables?
- Special variables stored outside your code
- Used for sensitive data (like API keys, passwords)
- Keeps secrets safe and not visible in code
- Each developer can have their own
.envfile
Why use .env files?
- β Security: API keys not in source code
- β Flexibility: Easy to change without editing code
- β
Safety:
.envis in.gitignore(won't upload to GitHub) - β Best Practice: Industry standard for managing secrets
The entire app is organized in a class called AirQualityApp. Think of a class as a blueprint:
__init__(): Runs when app starts (sets up window, API key)create_widgets(): Creates all buttons, labels, input boxescheck_aqi(): Gets air quality data when you click "Check AQI"display_aqi_data(): Shows the results on screen
# We send a request to IQAir's server
response = requests.get(url, params=params)
# They send back data in JSON format
data = response.json()
# We extract what we need
aqi = data['current']['pollution']['aqius']βββββββββββββββββββββββββββββββββββ
β π Air Quality Detector β β Title Label
βββββββββββββββββββββββββββββββββββ€
β City: [________] β β Input Fields
β State: [________] β
β Country: [________] β
β [π Check AQI] [π My Location]β β Buttons
βββββββββββββββββββββββββββββββββββ€
β 157 β β Big AQI Number
β π·οΈ Unhealthy β β Category
β π Los Angeles, CA, USA β β Location
β [Detailed Information Box] β β Text Widget
βββββββββββββββββββββββββββββββββββ
The code uses try-except blocks to handle problems:
- Timeout: Internet too slow
- ConnectionError: No internet
- Empty fields: User didn't enter city/state/country
if aqi <= 50:
color = "#4caf50" # Green (Good)
elif aqi <= 100:
color = "#ffeb3b" # Yellow (Moderate)
# ... and so on- GUI Programming: How to create windows, buttons, and labels with Tkinter
- API Integration: How to get real-time data from the internet
- Environment Variables: How to keep API keys secure using
.envfiles - Data Processing: How to extract and format JSON data
- Error Handling: How to deal with problems gracefully (try-except)
- Security Best Practices: Never hardcode secrets in source code
- User Experience: How to make apps intuitive and easy to use
- Code Organization: How to structure code with classes and methods
- Documentation: How to write helpful comments and docstrings
- Read the comments: Every section has detailed explanations
- Experiment: Try changing colors, fonts, or messages
- Debug: Use
print()statements to see what's happening - Ask questions: If something is unclear, research it!
- Build on it: Add new features like saving favorite cities
Problem: App shows error popup about missing API key
Solution:
- Make sure you created the
.envfile (copy from.env.example) - Open
.envand check that your API key is there - Make sure the line looks like:
IQAIR_API_KEY=your-actual-key - No spaces around the
=sign - No quotes around the API key
Example of correct .env file:
IQAIR_API_KEY=e6fa641d-6bc7-48db-94c8-f3f1e4a1a7ac
Solution: Install python-dotenv
pip install python-dotenvSolution: Install all dependencies
pip install -r requirements.txtProblem: You've used all 10,000 free API calls this month
Solutions:
- Wait until next month (limit resets)
- Create a new account with different email
- Upgrade to paid plan (if needed)
Problem: Your API key is wrong or invalid
Solutions:
- Double-check you copied the entire API key
- Make sure there are no extra spaces
- Get a new API key from dashboard
- Make sure you verified your email
Solutions:
- Check your internet connection
- Make sure city/state/country names are correct (use exact spelling)
- Try "Beijing, Beijing, China" as a test
- Check if API key is valid
Solution:
- Use manual entry instead of auto-detect
- Make sure you have internet connection
- Try entering city manually
Checklist:
- File is named exactly
.env(not.env.txt) - File is in the same folder as
main.py - API key has no quotes:
IQAIR_API_KEY=abc123β - API key has no spaces:
IQAIR_API_KEY = abc123β - You saved the file after editing
Open source for educational purposes. Feel free to learn, modify, and share!