Great 👍 Let’s turn the theory into lab-style exercises that students can run in either a Python module (.py
) or a Jupyter notebook (.ipynb
) inside Cursor.
I’ll scaffold these in increasing difficulty, with clear instructions + expected outcomes.
🔬 API Practice Labs
🧪 Lab 1 — Your First GET Request (Public API)
Goal: Call a free API and print the JSON response.
Steps
- Install dependencies:
uv add requests
- Create a file
lab1_get.py
(or a notebook cell). - Paste:
import requests url = "https://api.chucknorris.io/jokes/random" response = requests.get(url) print("Status:", response.status_code) print("JSON:", response.json()) print("Joke:", response.json()["value"])
Expected outcome:
- Status code
200
- A JSON object with fields like
{"icon_url": "...", "value": "Chuck Norris joke"}
- Prints one random Chuck Norris joke.
🧪 Lab 2 — Using JSON data in Python
Goal: Work with API response data.
Steps
Modify Lab 1 code to:
joke_data = response.json()
print("Joke length:", len(joke_data["value"]))
Expected outcome:
- Prints how many characters long the joke is.
🧪 Lab 3 — Introducing Environment Variables
Goal: Store and retrieve a fake key.
Steps
- Create a
.env
file:TEST_KEY=supersecret123
- Install dotenv:
uv add python-dotenv
- Code:
import os from dotenv import load_dotenv load_dotenv() key = os.getenv("TEST_KEY") print("Loaded key:", key)
Expected outcome:
- Program prints:
Loaded key: supersecret123
🧪 Lab 4 — Calling OpenAI API
Goal: Make a chat completion request.
Steps
- Sign up at platform.openai.com, get API key.
- Add to
.env
:OPENAI_API_KEY=sk-xxxxx...
- Install:
uv add openai
- Code:
import os from dotenv import load_dotenv from openai import OpenAI load_dotenv() client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) messages = [ {"role": "system", "content": "You are a tutor."}, {"role": "user", "content": "Explain photosynthesis in 3 lines."} ] response = client.chat.completions.create( model="gpt-4o-mini", messages=messages ) print(response.choices[0].message.content)
Expected outcome:
- Prints a short 3-line explanation of photosynthesis.
🧪 Lab 5 — Send an Email with SendGrid
Goal: Use API + environment variable to send an email.
Steps
- Create a SendGrid account, get API key.
- Add to
.env
:SENDGRID_API_KEY=SG.xxxxx
- Install:
uv add sendgrid
- Code:
import os from dotenv import load_dotenv from sendgrid import SendGridAPIClient from sendgrid.helpers.mail import Mail load_dotenv() sg = SendGridAPIClient(os.getenv("SENDGRID_API_KEY")) message = Mail( from_email="you@example.com", to_emails="friend@example.com", subject="API Lab Test Email", html_content="<strong>This is a test email sent via SendGrid API</strong>" ) response = sg.send(message) print("Status:", response.status_code) # should be 202
Expected outcome:
- Console prints
Status: 202
. - Recipient receives your test email.
🧪 Lab 6 — Handle API Errors
Goal: Learn to handle failures gracefully.
Steps
- Intentionally break your
.env
key (e.g., renameSENDGRID_API_KEY
→SENDGRID_API_KEY_WRONG
). - Run the SendGrid script.
Modify code:
try:
response = sg.send(message)
print("Status:", response.status_code)
except Exception as e:
print("Error:", e)
Expected outcome:
- Error is caught and printed instead of crashing.
📝 Suggested Student Assignment
Build a script that:
- Calls the Chuck Norris API for a joke.
- Calls the OpenAI API to summarize that joke in 5 words.
- Sends the summary via SendGrid email to yourself.
(This combines GET request, JSON parsing, env keys, and two client libraries into one practical pipeline.)