This is a FastAPI-based application that retrieves weather data for a given location using the OpenWeatherMap API. The application includes caching with Redis to improve performance and rate limiting using SlowAPI to prevent abuse.
This is a solution to the roadmap.sh project weather api
- Fetch current weather data for a specified location.
- Caching of weather data using Redis to reduce API calls.
- Rate limiting to control the number of requests per user.
- Python 3.8+
- Redis server
- OpenWeatherMap API key
-
Clone the repository:
git clone https://github.com/adobhal95/weather-api.git cd weather-api
-
Set up environment variables:
OPENWEATHER_API_KEY
: Your OpenWeatherMap API key.REDIS_URL
: Redis server URL (e.g.,redis://localhost:6379
).OPENWEATHER_API_WEATHER_URL
: url for open weather api weather location for a cityOPENWEATHER_API_GEO_URL
: url for open weather api geo code locationCacheTTL
: int value containing for how long api cache will last in seconds
Example
.env
file:OPENWEATHER_API_KEY=your_api_key REDIS_URL=redis://redis:6379
-
Use Docker and docker compose:
docker compose build --no-cache docker compose up # to start services docker compose stop # to stop services
- Access the API documentation at
http://localhost:8000/docs
.
Retrieve weather data for a given location.
city
(string, required): Name of the location (e.g., "London").country
(string, required): Name of the Country (e.g., "England").
curl -X GET "http://127.0.0.1:8000/weather/city=London/country=England"
{
"coord": {
"lon": -0.1257,
"lat": 51.5072
},
"weather": [
{
"id": 802,
"main": "Clouds",
"description": "scattered clouds",
"icon": "03d"
}
],
"base": "stations",
"main": {
"temp": 285.44,
"feels_like": 284.29,
"temp_min": 285.44,
"temp_max": 285.44,
"pressure": 1004,
"humidity": 60,
"sea_level": 1004,
"grnd_level": 1000
},
"visibility": 10000,
"wind": {
"speed": 7.27,
"deg": 219,
"gust": 11.52
},
"rain": null,
"snow": null,
"clouds": {
"all": 31
},
"dt": 1744801912,
"sys": {
"type": null,
"id": null,
"country": "GB",
"sunrise": 1744779734,
"sunset": 1744829873
},
"timezone": 3600,
"id": 2643743,
"name": "London",
"cod": 200
}
- Weather data is cached in Redis for a configurable duration (default: 1 minute).
- Cached responses are served for repeated requests to the same location within the cache duration.
- The application uses SlowAPI to enforce rate limits.
- Default limit: 5 requests per minute per user.
- Requests exceeding the limit will receive a
429 Too Many Requests
response.
This project is licensed under the MIT License. See the LICENSE file for details.