Courses Curriculum Cities Blog Enroll Now
API Development for AI · Day 1 of 5 ~45 minutes

Day 1: FastAPI Fundamentals: Build Your First API

FastAPI is the fastest way to build production-ready APIs in Python. In this lesson you'll install it, understand how it works, and build your first working endpoint.

1
Day 1
2
Day 2
3
Day 3
4
Day 4
5
Day 5
What You'll Build

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.

1
Section 1 · 10 min

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 /docs on 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.

2
Section 2 · 10 min

Install and First Server

Install FastAPI and uvicorn (the server that runs it):

bash
$ pip install fastapi uvicorn anthropic python-dotenv

Create main.py:

pythonmain.py
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def health_check():
    return {"status": "ok", "service": "AI API"}

Run it:

bash
$ uvicorn main:app --reload

Open 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.

3
Section 3 · 25 min

Add an AI Endpoint

Now build a real endpoint that accepts text and returns an AI-generated summary:

pythonmain.py
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
Your Challenge

Go Further on Your Own

  • Add a third endpoint /sentiment that 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
Day 1 Complete

Nice work. Keep going.

Day 2 is ready when you are.

Continue to Day 2
Course Progress
20%

Want live instruction and hands-on projects? Join the AI bootcamp — 3 days, 5 cities.

Finished this lesson?