An API (Application Programming Interface) is just a program that listens for requests over the internet and sends back responses. That's it.
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.
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:
from fastapi import FastAPI — Import the framework. FastAPI handles all the networking plumbing so you don't have to.
app = FastAPI() — Create your application. This is the "waiter" — the thing that will listen for requests.
@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.
def hello(): — Just a regular Python function. Nothing special about it.
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.
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.
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.
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.
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."}
{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.
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.
What does @app.get("/") do?
Why does FastAPI return JSON instead of plain text?
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}.