← Back to Home

APIs with FastAPI

What is an API?

An API (Application Programming Interface) is like a waiter at a restaurant—it takes your order (request), tells the kitchen (server) what you want, and brings you back food (response).

With FastAPI, you can build a web API in Python that other programs (or websites) can talk to.

Set up a FastAPI project

In your terminal, create a new folder and install FastAPI:

mkdir -p python-tutorials/fastapi-hello
cd python-tutorials/fastapi-hello

uv venv
uv pip install fastapi uvicorn

fastapi is the framework, and uvicorn runs the server.

Your First API

Create a file main.py:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello, World!"}

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

This creates two endpoints:

  • / — says "Hello, World!"
  • /hello/YourName — greets you by name

Run the API

Start the server:

uv run uvicorn main:app --reload

Open http://127.0.0.1:8000 in your browser—you'll see the JSON response.

Try http://127.0.0.1:8000/hello/Alice to see a personalized greeting.

Explore the docs

FastAPI auto-generates documentation. Visit http://127.0.0.1:8000/docs to test your API interactively.

What's Next?

In the next tutorial, you'll add a local NoSQL database and build an API for a simple store.

← Previous Next: Store API + Database →