🎓 AIIQM-WELL University · Builder · Module B2 of 5

Talking to AI Programmatically
APIs and Requests

60–75 min
📋 Prerequisites: B1 complete, Python functions working
📊 Builder Level
Lesson Outcome: You can make an authenticated API call to an AI provider and receive a response in Python code.
AIMY: The chat interface — ChatGPT, Claude.ai — is a wrapper. Underneath it is an API: a programmable interface that takes your text, sends it to the model, and returns a response as structured data. When you use the API directly, you control everything: the model, the prompt, the temperature, the output format. No interface. No limitations. This is how every AI product in the world is built.
ANALYZE

API Fundamentals

1

What an API is

Application Programming Interface. A set of rules for how your code talks to a service. You send a request. You receive a response. The AI model is a service. You are the client.
2

REST and HTTP

APIs communicate over HTTP — the same protocol your browser uses. You send a POST request with your message. The API returns a JSON object with the response.
3

Authentication

Every API requires a key. Your API key is like a password — it identifies you and tracks your usage. It goes in the request header, not in your code directly (we use environment variables).
4

The Anthropic API structure

A call to Claude looks like: send messages array with role:user and content:your_prompt, receive messages array with role:assistant and content:response. That's the full pattern.
INTEGRATE

Make Your First API Call

  1. Install the Anthropic Python library: in your terminal: pip install anthropic
  2. Get your API key from console.anthropic.com
  3. Set it as an environment variable:
    • Mac/Linux: export ANTHROPIC_API_KEY="your-key"
    • Windows: set ANTHROPIC_API_KEY=your-key
  4. Create: touch first_api_call.py
  5. Write:
    import anthropic client = anthropic.Anthropic() message = client.messages.create( model="claude-3-haiku-20240307", max_tokens=256, messages=[{"role": "user", "content": "Explain what an API is in exactly two sentences."}] ) print(message.content[0].text)
  6. Run: python first_api_call.py
  7. You should receive a response directly in your terminal — no browser, no chat interface.
PTR (PROOF THAT IT RUNS)
  • Your API call runs without errors.
  • You receive a real AI-generated response in the terminal.
  • You understand what each line of the script does.

Common Mistakes

AuthenticationError
Your API key is wrong or not set. Check: echo $ANTHROPIC_API_KEY (Mac) or echo %ANTHROPIC_API_KEY% (Windows).
Module not found
Run pip install anthropic again. Make sure you're in the right Python environment.
CHECKPOINT
  • You installed the Anthropic library.
  • You set your API key as an environment variable.
  • You made a successful API call and received a response.
  • You understand the request/response structure.
AIM COMMITMENT

Analyze: You understood APIs as programmable interfaces — not magic, not mystery, just structured HTTP requests and JSON responses.

Integrate: You made a direct API call to Claude and received a response in your terminal.

Manage: You can now build AI into any Python script — not just use it through a chat interface.