A running FastAPI server with three endpoints: a health check, a text analysis endpoint that calls Claude, and a JSON response with proper status codes and error handling.
Why FastAPI for AI Apps
When you build an AI application, you need a way for other programs to talk to it. That's what an API does — it's a defined interface for making requests and getting responses. FastAPI is the Python framework that makes building these interfaces fast, reliable, and automatic.
Why FastAPI specifically:
- Speed: One of the fastest Python web frameworks, comparable to Node.js and Go
- Automatic documentation: Visit
/docson any FastAPI server and you get a live interactive API reference — free, automatic, always up to date - Type safety: Uses Python type hints to validate request and response data automatically
- Async support: Built for handling many requests concurrently — important when each request calls an AI API
Prerequisites: This course assumes you're comfortable with Python basics — functions, imports, and dictionaries. You don't need previous API or web development experience. Install Python 3.11+ before starting.
Install and First Server
Install FastAPI and uvicorn (the server that runs it):
$ pip install fastapi uvicorn anthropic python-dotenvCreate main.py:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def health_check():
return {"status": "ok", "service": "AI API"}Run it:
$ uvicorn main:app --reloadOpen http://localhost:8000 — you get your first response. Open http://localhost:8000/docs — you get auto-generated documentation you can run right in the browser.
Add an AI Endpoint
Now build a real endpoint that accepts text and returns an AI-generated summary:
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import anthropic
app = FastAPI(title="AI Text API")
client = anthropic.Anthropic()
class SummarizeRequest(BaseModel):
text: str
max_words: int = 100
@app.get("/")
def health_check():
return {"status": "ok"}
@app.post("/summarize")
def summarize(req: SummarizeRequest):
if not req.text.strip():
raise HTTPException(status_code=400, detail="text cannot be empty")
msg = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=512,
system=f"Summarize the text in {req.max_words} words or fewer. Return only the summary.",
messages=[{"role": "user", "content": req.text}]
)
return {
"summary": msg.content[0].text,
"input_tokens": msg.usage.input_tokens,
"output_tokens": msg.usage.output_tokens
}Test it at /docs — click the /summarize endpoint, click "Try it out," paste any text, hit Execute. You'll see the raw request and response.
What You Learned Today
- Why FastAPI is the right choice for AI application backends
- How Python type hints + Pydantic models automatically validate request data
- How to add proper HTTP status codes and error responses
- How the auto-generated /docs interface works for testing
Go Further on Your Own
- Add a third endpoint
/sentimentthat takes text and returns: positive/negative/neutral with a confidence score (use AI to determine this) - Add input validation: reject requests where text is longer than 5,000 characters
- Add a
/word-countendpoint that doesn't call AI — just returns word count, character count, and estimated reading time. This tests your understanding of FastAPI without AI dependency
Nice work. Keep going.
Day 2 is ready when you are.
Continue to Day 2Want live instruction and hands-on projects? Join the AI bootcamp — 3 days, 5 cities.