- Generate a Python script that looks up a city’s coordinates with Nominatim and fetches current weather from OpenWeather.
- Convert that script into a FastAPI application so others can request weather for a city via HTTP.

- A single, runnable Python file (weather.py) that:
- Accepts a city name
- Uses Nominatim to resolve latitude/longitude
- Uses OpenWeather to fetch current weather (API key read from OPENWEATHER_API_KEY)
- Provides a CLI entrypoint (prints Celsius)
- Exposes a FastAPI endpoint /weather/ that returns Fahrenheit JSON
Note: Nominatim requires a valid User-Agent identifying your application. Respect their usage policy and rate limits when making requests.
Warning: Never commit API keys to source control. Use environment variables (e.g., OPENWEATHER_API_KEY) and restrict keys where possible. Also monitor and respect API rate limits to avoid service interruptions.
| Step | Command | Purpose |
|---|---|---|
| Create venv | python3 -m venv venv | Create a virtual environment |
| Activate venv (macOS/Linux) | source venv/bin/activate | Activate the venv |
| Install deps | pip install requests fastapi uvicorn | Install required Python packages |
| Set API key | export OPENWEATHER_API_KEY=“your_api_key_here” | Provide OpenWeather credential |
| Run CLI | python3 weather.py “New York” | Use script as a CLI (prints Celsius) |
| Run FastAPI server | uvicorn weather:app —reload | Start server on http://127.0.0.1:8000 |
- 401 Unauthorized from OpenWeather: ensure OPENWEATHER_API_KEY is set in the same environment where you run Python or uvicorn. New keys may take a few minutes to activate.
- City not found: Nominatim returned no results; verify spelling or try a larger query (e.g., include country).
- requests missing: ensure your virtual environment is active and run pip install requests.
- Python not found: use python3 on macOS/Linux if python points to Python 2.
| Resource | Description | Link |
|---|---|---|
| Nominatim (OpenStreetMap) | Free geocoding to get coordinates from a city name | https://nominatim.org/ |
| OpenWeather API | Current weather API used for temperature and conditions | https://openweathermap.org/api |
| FastAPI | Python web framework used to expose the endpoint | https://fastapi.tiangolo.com/ |
| requests | HTTP library used for external API requests | https://docs.python-requests.org/ |
- Add caching for geocoding results to reduce Nominatim queries.
- Add input validation and rate limiting to the FastAPI app for production readiness.
- Containerize the app with Docker for easy deployment.
- Add tests for get_coordinates and get_weather functions to verify behavior with mocked HTTP responses.