Skip to main content
Albero Academy

Project: API Client

A weather (or GitHub) client calling a real API, handling errors and JSON.

Projects13 min

Now reach out to the wider internet: build a client for a real public API (weather, GitHub, or similar) that fetches data, parses the JSON, and presents a clean summary. It applies requests, robust error handling, and modelling a messy response into just what you need.

What to build

  • Call a public API with requests and a timeout.
  • Handle network errors and non-200 responses gracefully.
  • Extract only the fields you care about from the JSON.
  • Present a clean, readable summary to the user.

A robust client function

api_client.py
import requests
def get_weather(city: str) -> dict | None:
try:
r = requests.get("https://api.example.com/weather",
params={"q": city}, timeout=10)
r.raise_for_status()
except requests.RequestException as e:
print(f"Request failed: {e}")
return None
data = r.json()
return { # model just what you need
"city": data["name"],
"temp": data["main"]["temp"],
"desc": data["weather"][0]["description"],
}

Stretch goals

Cache responses to avoid repeat calls, add a retry with backoff, load the API key from an environment variable, or add a small CLI with argparse.

Key takeaways

  1. 01Applies requests, JSON parsing and robust error handling end to end.
  2. 02Always use timeouts and check status codes (raise_for_status).
  3. 03Catch requests.RequestException so network failures do not crash the app.
  4. 04Model the response into just the fields you need.
  5. 05Stretch it with caching, retries, env-var config, or a CLI.

Finished this chapter?

Mark it complete to track your progress through Python.