← Python Coding Track

Module 09IntermediateWeeks 5–7

Working with APIs

01

Learning outcomes

  • Fetch and use data from external sources
02

Core concepts

HTTP requestsJSONThe requests libraryAPI keys
03

Lessons

Module goal

Learner understands how programs communicate with external services over the internet, and can pull real, live data into their own Python programs.

Lesson 1 of 5 · lecture

What is an API?

Step 1 of 2

API stands for Application Programming Interface. In practical terms, it's a defined way for one piece of software to ask another for information or to perform an action, without needing to know how that other software works internally.

Teaching point

A restaurant menu is a good comparison. You don't walk into the kitchen and cook — you look at the menu (the API's options), tell the waiter what you want (a request), and the kitchen (the external service) sends back your food (the response). You never see how the kitchen prepares it.

In software terms: a weather API lets your program ask “what's the weather in Accra right now?” and get back an answer, without your program running its own weather sensors or forecasting models. Someone else built that system — the API is the doorway to using it.

04

Guided labs

Lab — build a live weather fetcher

  1. 1.Sign up for a free account with a public weather API (e.g. OpenWeatherMap) to get a free API key
  2. 2.Create api_basics.py
  3. 3.Install requests via pip install requests
  4. 4.Ask the user for a city name, send a GET request to the weather API, and print the temperature and condition
  5. 5.Handle the case where the city isn't found, using the error handling learned in Module 8
import requests

api_key = "your_api_key_here"
city = input("Enter a city name: ")

url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"

try:
    response = requests.get(url)
    data = response.json()

    if response.status_code == 200:
        temperature = data["main"]["temp"]
        condition = data["weather"][0]["description"]
        print(f"Weather in {city}: {temperature}°C, {condition}")
    else:
        print(f"Could not find weather for '{city}'. Please check the spelling.")

except requests.exceptions.ConnectionError:
    print("Could not connect. Please check your internet connection.")

Teaching point

This connects Module 8 directly back into practice — requests.exceptions.ConnectionError is a real-world example of anticipating failure: a dropped internet connection shouldn't crash the whole program.

Success criteria

Entering a valid city prints real, live temperature and condition data; entering an invalid or misspelled city prints a friendly error message instead of crashing.

05

Knowledge Check

Question 1 of 6

In plain terms, what is an API?

06

What you read

  • What an API is
  • How requests and responses work
  • How JSON structures data
07

What you understand

Key takeaways

  • Modern software rarely works in isolation — it connects to external services
08

Hands-on lab

Lab tasks

  1. 1.Pull data from a free public API (e.g., weather or currency exchange) and display it
09

Knowledge check

  • Interpret a JSON response
  • Identify GET vs. POST use cases

Write your answers in your own notes before moving on — explaining a concept in your own words is the fastest way to find the gaps.

10

Mark progress