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 weatherapi3️⃣ Install dependencies
pip install flask flask-limiter requests redis python-dotenv4️⃣ 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-py5️⃣ Run Redis on a powershell prompt in your conda environment
redis-server6️⃣ On another powershell prompt you connect Redis using this
redis-cli pingSign 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.pyBy default, the app runs at:
http://127.0.0.1:5000Get 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.