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

  1. Install dependencies: uv add requests
  2. Create a file lab1_get.py (or a notebook cell).
  3. 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

  1. Create a .env file: TEST_KEY=supersecret123
  2. Install dotenv: uv add python-dotenv
  3. 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

  1. Sign up at platform.openai.com, get API key.
  2. Add to .env: OPENAI_API_KEY=sk-xxxxx...
  3. Install: uv add openai
  4. 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

  1. Create a SendGrid account, get API key.
  2. Add to .env: SENDGRID_API_KEY=SG.xxxxx
  3. Install: uv add sendgrid
  4. 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

  1. Intentionally break your .env key (e.g., rename SENDGRID_API_KEYSENDGRID_API_KEY_WRONG).
  2. 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:

  1. Calls the Chuck Norris API for a joke.
  2. Calls the OpenAI API to summarize that joke in 5 words.
  3. Sends the summary via SendGrid email to yourself.

(This combines GET request, JSON parsing, env keys, and two client libraries into one practical pipeline.)


Pages: 1 2 3 4


Discover more from HintsToday

Subscribe to get the latest posts sent to your email.

Posted in

Discover more from HintsToday

Subscribe now to keep reading and get access to the full archive.

Continue reading