A simple Flask-based Weather API that fetches live weather data from the OpenWeatherMap API, caches results with Redis, and applies rate limiting to prevent abuse.
- π Get current weather by city name
- β‘ Caching with Redis (12-hour expiry)
- π¦ Rate limiting using Flask-Limiter
- π Weather condition emojis for easy reading
- π Secure API key loading via
.env
- Flask β lightweight web framework
- Redis β in-memory cache for API responses
- Flask-Limiter β request rate limiting
- Requests β for external API calls
- python-dotenv β load environment variables
- OpenWeatherMap API β weather data source
git clone https://github.com/yourusername/weather-api.git
cd weather-api2οΈβ£ Create and activate a Conda environment
conda create -n weatherapi python=3.11 -y conda activate weatherapi
3οΈβ£ Install dependencies
pip install flask flask-limiter requests redis python-dotenv
4οΈβ£ Install Redis (if you are using Anaconda Powershell and windows) check out this installation guide: https://medium.com/@baertschi91/redis-installation-guide-for-windows-67ca177e2836
conda install -c binstar redis-server conda install -c anaconda redis-py
5οΈβ£ Run Redis on a powershell prompt in your conda environment
redis-server
6οΈβ£ On another powershell prompt you connect Redis using this
redis-cli ping
Sign in at OpenWeatherMap to get your free API key.
# .env
API_KEY=your_openweathermap_api_keyRunning it on the command line or powershell
python app.py
By default, the app runs at:
http://127.0.0.1:5000
Get current weather
http://127.0.0.1:5000/weather?city=<city_name>
http://127.0.0.1:5000/weather?city=londonResponse:
{
"city": "london",
"temperature": "17Β°C",
"emoji": "β",
"description": "scattered clouds"
}- The user sends a request like /weather?city=london.
- The app first checks Redis cache using the key weather:london.
- If data exists β itβs served instantly from cache.
- If not β it fetches from OpenWeatherMap, parses, stores it in Redis (12-hour TTL), and returns JSON.