A clean, production-minded FastAPI service that fetches current weather data for a given city using the Weatherstack API.
- 🌍 Fetch current weather by city
- 🧱 Clean layered architecture
- ⚡ FastAPI performance
- 🧠 Smart response transformation
- ♻️ In-memory caching with TTL
- 🐳 Docker support
- 🔐 Environment-based configuration
- 📊 Logging for observability
- 📑 Interactive API docs with Swagger UI
-
Install dependencies:
pip install -r requirements.txt
-
Set environment variables:
Create a.envfile or export variables in your shell:WEATHER_API_URL=your_api_url_here WEATHER_API_KEY=your_api_key_here -
Start the server:
uvicorn app.main:app --reload
👉 Open: http://127.0.0.1:8000/docs
-
Build the Docker image:
docker build -t weather-api . -
Create a
.envfile in the project root with:WEATHER_API_URL=your_api_url_here WEATHER_API_KEY=your_api_key_here -
Run the container:
docker run -p 8000:8000 --env-file .env weather-api
GET /weather?city=Cairo
{
"location": {
"name": "Cairo",
"country": "Egypt",
"localtime": "2026-04-23 17:01"
},
"weather": {
"temperature": 28,
"description": "Sunny",
"feels_like": 26,
"humidity": 19
},
"wind": {
"speed": 13,
"direction": "NNW"
},
"air_quality": {
"pm2_5": 16.85,
"pm10": 31.35
}
}Create a .env file or set the following environment variables:
| Key | Description |
|---|---|
WEATHER_API_URL |
Weatherstack API base URL |
WEATHER_API_KEY |
Your Weatherstack API key |
Note:
An example.envfile is provided for your convenience.
However, in production you should never commit your real.envfile to version control.
Make sure to add.envto your.gitignoreto keep your secrets safe.
- Used layered architecture to separate concerns
- Implemented service layer for business logic
- Transformed external API response → clean internal format
- Used environment variables for secure configuration
- Added caching abstraction for scalability
- Dockerized for consistent deployment
- Used in-memory caching instead of Redis (for simplicity)
- No authentication or DB (out of scope)
- Focused on clarity over complexity
I intentionally kept the solution simple and production-ready, given the expected scope (~60–90 minutes).
- Redis caching (distributed)
- Async HTTP client (httpx)
- Retry mechanism
- Rate limiting
- Unit & integration testing
- Structured logging
- Python 3.11
- FastAPI
- Requests
- Pydantic
- Docker
The API handles:
- Invalid input (missing or incorrect city)
- External API errors (Weatherstack responses)
- Network failures (timeouts, connection issues)
Appropriate HTTP status codes are returned (400, 502).
A simple in-memory cache with TTL is implemented to:
- Reduce external API calls
- Improve response time
The caching logic is abstracted, allowing easy replacement with Redis in a production environment.
The API returns a structured and simplified response instead of exposing raw external API data.
This ensures:
- Stability of the API contract
- Decoupling from third-party changes
Esraa Raffik Mashaal
Senior Fullstack Engineer
LinkedIn
📧 esraa.mashaal96@gmail.com
📞 +201013589988
This project demonstrates:
- clean architecture
- production thinking
- scalable design approach
This implementation prioritizes clarity, maintainability, and production-readiness over unnecessary complexity.
