felixFELIX
forlearnai
PH00APIs & Keys
SRC
PHASE
00 · Setup & Tooling
TYPE
Build
LANG
Python, TypeScript

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

  • Store API keys securely using environment variables and .env files
  • 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:

  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:

APIWhen you need itFree tier
Anthropic (Claude)Phases 11-16 (agents, tools)$5 credit on signup
OpenAIPhase 11 (comparison)$5 credit on signup
Hugging FacePhases 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:

  • outputs/prompt-api-troubleshooter.md - diagnose common API errors

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

TermWhat people sayWhat 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
📦
WHAT THIS LESSON SHIPS
Prompts, skills, and artifacts you can use right now
PROMPTprompt-api-troubleshooter.md

Diagnose and fix common AI API errors (auth, rate limits, timeouts)

VIEW
💻
RUN THE CODE
Executable files from this lesson
🐍first_api_call.py1.6 KB
python phases/00-setup-and-tooling/04-apis-and-keys/code/first_api_call.py
🧠
KNOWLEDGE CHECK
5 questions · test your understanding
PRE

What is an API key used for when calling an LLM service?

PRE

Why should API keys never be hardcoded directly in source code?

POST

What is the recommended way to store API keys for local development?

POST

In the raw HTTP API call to Anthropic, which header carries the API key?

POST

What happens when you exceed an API's rate limit?

🗺
LEARNING PATH
Phase 00: Setup & Tooling
0/12 LESSONS COMPLETE IN THIS PHASE
Finished reading? Mark this lesson complete to track your progress.