Skip to main content
In this lesson we demonstrate two practical ways Claude Code helps when working with external APIs:
  • 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 presentation slide titled "Working with APIs and External Services" with a dark curved shape on the right containing the word "Demo" in blue. A small "© Copyright KodeKloud" note appears in the bottom-left.
Overview of the final deliverable
  • 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
Complete, consolidated, and runnable weather.py
Important usage notes
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.
Quick setup and run commands Example FastAPI request GET http://127.0.0.1:8000/weather/Forest%20Grove Sample JSON response
Troubleshooting
  • 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.
Resources and References Suggested next steps
  • 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.
This sequence demonstrates how Claude Code can scaffold working code, how to identify and debug small integration issues (API keys, headers, request params), and how to convert a simple script into a shareable HTTP API with FastAPI.

Watch Video

Practice Lab