Your First API in 7 Lines

Lesson 001 · APIs · ~10 minutes

What is an API, really?

An API (Application Programming Interface) is just a program that listens for requests over the internet and sends back responses. That's it.

The Restaurant Analogy

Think of a restaurant. You (the client) don't walk into the kitchen. You tell the waiter (the API) what you want. The waiter takes your order to the kitchen (the server logic), and brings back your food (the response).

Every AI service — ChatGPT, image generators, speech-to-text — works exactly this way. You send a request, the API processes it, and sends back a result.

The Simplest API on Earth

Here's a complete, working API in Python using FastAPI:

# api.py — your first API
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def hello():
    return {"message": "Hello, I am an API!"}

That's 7 lines. Let's break down every single one:

1

from fastapi import FastAPI — Import the framework. FastAPI handles all the networking plumbing so you don't have to.

2

app = FastAPI() — Create your application. This is the "waiter" — the thing that will listen for requests.

3

@app.get("/") — This is a decorator. It says: "when someone visits the root URL / with a GET request, run the function below." This is called a route or endpoint.

4

def hello(): — Just a regular Python function. Nothing special about it.

5

return {"message": "Hello, I am an API!"} — Return a Python dictionary. FastAPI automatically converts it to JSON — the standard format APIs use to send data.

Run it

Two commands in your terminal:

# Install FastAPI + a server to run it
pip install fastapi uvicorn

# Start your API
uvicorn api:app --reload

Now open your browser to http://127.0.0.1:8000 and you'll see:

{"message": "Hello, I am an API!"}

🎉 That's a live API. Running on your machine. Responding to requests.

What just happened?

uvicorn is the server — it listens on port 8000 and forwards incoming HTTP requests to your FastAPI app. api:app means "in the file api.py, use the object called app." The --reload flag restarts the server when you edit the code.

Free bonus: automatic docs

FastAPI gives you something wild for free. Go to:

http://127.0.0.1:8000/docs

You'll see a full interactive documentation page where you can test your API directly in the browser. This is powered by Swagger UI and it's generated automatically from your code. No extra work.

Make it useful: add a parameter

APIs that always return the same thing are boring. Let's make it take input:

# api.py — now with a parameter
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def hello():
    return {"message": "Hello, I am an API!"}

@app.get("/greet/{name}")
def greet(name: str):
    return {"message": f"Hello, {name}! Welcome to my API."}

Now visit http://127.0.0.1:8000/greet/Shamil and you get:

{"message": "Hello, Shamil! Welcome to my API."}

🔑 Key Concept: Path Parameters

{name} in the route is a path parameter. Whatever you put after /greet/ gets captured and passed to your function. FastAPI even validates the type — if you said name: int, it would reject non-numbers automatically.

Why this matters for AI engineering

Every AI model you'll ever use is behind an API just like this one. When you call OpenAI's ChatGPT:

# This is just an API call
response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}]
)

Under the hood, that's sending an HTTP POST request to https://api.openai.com/v1/chat/completions — an API endpoint, exactly like the ones you just built. The only difference is the function behind the endpoint runs a massive neural network instead of returning a greeting.

You just learned the foundation that every AI system is built on.

🧠 Check Your Understanding

What does @app.get("/") do?

🧠 Check Your Understanding

Why does FastAPI return JSON instead of plain text?

🛠️ Try It Yourself

Add a third endpoint to your API: @app.get("/add/{a}/{b}") that takes two numbers and returns their sum. Hint: use a: int, b: int as the function parameters.

Test it by visiting http://127.0.0.1:8000/add/5/3 — you should get {"result": 8}.

📖 Recommended reading: FastAPI — First Steps (official tutorial, ~5 min read)

🤔 Questions? Ask Pixie anything — that's what I'm here for. "Why do we need uvicorn?" or "What's the difference between GET and POST?" — no question is too basic.