APIs & Keys

> Every AI API works the same way: send a request, get a response. The details change, the pattern doesn't.

Type: Build

Languages: Python, TypeScript

Prerequisites: Phase 0, Lesson 01

Time: ~30 minutes

Learning Objectives

The Problem

Starting from Phase 11, you'll call LLM APIs (Anthropic, OpenAI, Google). In Phase 13-16 you'll build agents that use these APIs in loops. You need to know how API keys work, how to store them safely, and how to make your first API call.

The Concept

sequenceDiagram participant C as Your Code participant S as API Server C->>S: HTTP Request (with API key) S->>C: HTTP Response (JSON)

Every API call has:

  1. An endpoint (URL)
  2. An API key (authentication)
  3. A request body (what you want)
  4. A response body (what you get back)

Build It

Step 1: Store API keys safely

Never put API keys in code. Use environment variables.

export ANTHROPIC_API_KEY="sk-ant-..."
export OPENAI_API_KEY="sk-..."

Or use a .env file (add it to .gitignore):

ANTHROPIC_API_KEY=sk-ant-...
OPENAI_API_KEY=sk-...

Step 2: First API call (Python)

import anthropic

client = anthropic.Anthropic()

response = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=256,
    messages=[{"role": "user", "content": "What is a neural network in one sentence?"}]
)

print(response.content[0].text)

Step 3: First API call (TypeScript)

import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic();

const response = await client.messages.create({
  model: "claude-sonnet-4-20250514",
  max_tokens: 256,
  messages: [{ role: "user", content: "What is a neural network in one sentence?" }],
});

console.log(response.content[0].text);

Step 4: Raw HTTP (no SDK)

import os
import urllib.request
import json

url = "https://api.anthropic.com/v1/messages"
headers = {
    "Content-Type": "application/json",
    "x-api-key": os.environ["ANTHROPIC_API_KEY"],
    "anthropic-version": "2023-06-01",
}
body = json.dumps({
    "model": "claude-sonnet-4-20250514",
    "max_tokens": 256,
    "messages": [{"role": "user", "content": "What is a neural network in one sentence?"}],
}).encode()

req = urllib.request.Request(url, data=body, headers=headers, method="POST")
with urllib.request.urlopen(req) as resp:
    result = json.loads(resp.read())
    print(result["content"][0]["text"])

This is what the SDKs do under the hood. Understanding the raw HTTP call helps when debugging.

Use It

For this course:

API When you need it Free tier
Anthropic (Claude) Phases 11-16 (agents, tools) $5 credit on signup
OpenAI Phase 11 (comparison) $5 credit on signup
Hugging Face Phases 4-10 (models, datasets) Free

You don't need all of them right now. Set them up when the lesson requires it.

Ship It

This lesson produces:

Exercises

  1. Get an Anthropic API key and make your first API call
  2. Try the raw HTTP version and compare the response format to the SDK version
  3. Intentionally use a wrong API key and read the error message

Key Terms

Term What people say What it actually means
API key "Password for the API" A unique string that identifies your account and authorizes requests
Rate limit "They're throttling me" Maximum requests per minute/hour to prevent abuse and ensure fair usage
Token "A word" (in API context) A billing unit: input and output tokens are counted and charged separately
Streaming "Real-time responses" Getting the response word by word instead of waiting for the full response