APIs & Keys
Every AI API works the same way: send a request, get a response. The details change, the pattern doesn't.
On the Developer Experience and Education career route
Learning Objectives
- Store API keys securely using environment variables and
.envfiles - Make an LLM API call using both the Anthropic Python SDK and raw HTTP
- Compare SDK-based and raw HTTP request/response formats for debugging
- Identify and handle common API errors including authentication and rate limits
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
Every API call has:
- An endpoint (URL)
- An API key (authentication)
- A request body (what you want)
- A response body (what you get back)
s0-secret-injectBuild 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 os
import anthropic
client = anthropic.Anthropic()
MODEL = os.environ.get("LLM_MODEL", "claude-sonnet-5")
response = client.messages.create(
model=MODEL,
max_tokens=256,
messages=[{"role": "user", "content": "What is a neural network in one sentence?"}]
)
print(response.content[0].text)LLM_MODEL selects the Anthropic model id, and the default is the un-dated Sonnet alias. Other providers (OpenAI, Google, and others) follow the same pattern of a key plus a model id, but each has its own SDK, endpoint, and request/response schema.
Step 3: First API call (TypeScript)
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic();
const MODEL = process.env.LLM_MODEL ?? "claude-sonnet-5";
const response = await client.messages.create({
model: MODEL,
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": os.environ.get("LLM_MODEL", "claude-sonnet-5"),
"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 |
| KatalockAi | Any lesson that calls an LLM; one key, OpenAI-compatible | Pay as you go from a prepaid wallet |
You don't need all of them right now. Set them up when the lesson requires it.
Ship It
This lesson produces:
outputs/prompt-api-troubleshooter.md- diagnose common API errors
Exercises
- Get an Anthropic API key and make your first API call
- Try the raw HTTP version and compare the response format to the SDK version
- Intentionally use a wrong API key and read the error message
- Make the same call through the KatalockAi API (below) and compare its response with the other two
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 |
Lesson check
Answer a few questions on this lesson to mark it as done. You need 70% to pass and can retry as often as you like.
Pass the lesson check above to mark this lesson as done.