Single logo
  • Courses
  • Blog
  • About
Join for freeJoin free
Learning pathShip Your First Model: From Notebook to a FastAPI Service
  1. 1
    Saving and Loading Models with joblib: One Pipeline, One File
  2. 2
    FastAPI Hello World: Routes, uvicorn and the Free Swagger Page
  3. 3
    Deploy a Machine Learning Model with FastAPI: POST /predict for a Saved Model
  4. 4
    Project: Car Price Predictor End to End, from CSV to a FastAPI Price API
  5. 5
    Python Virtual Environments: One Project, One Toolbox
  6. 6
    Git and GitHub Fundamentals: Commits, Branches, Push and Pull
  7. 7
    Testing a FastAPI App with pytest: Three Small Tests
  8. 8
    Config and Logging Instead of Hard-Coded Values in ML Training
  9. 9
    SQL Essentials for Python: SELECT, WHERE, JOIN and GROUP BY
  1. Blog
  2. /
  3. Machine Learning
  4. /
  5. MLOps and Production ML

Learning path: Ship Your First Model: From Notebook to a FastAPI Service · Step 2 of 9

FastAPI Hello World: Routes, uvicorn and the Free Swagger Page

The smallest FastAPI app: two routes in main.py, started with uvicorn. Follow one request from the browser to the route table and back as JSON, see a 404, and try the routes on the free Swagger page at /docs.

Chaand Sheikh

·27 November 2026Serving Models

What you will learn

  • Explain what an API is and why a saved model file needs a server in front of it
  • Write a FastAPI app with two GET routes, one with a path parameter
  • Start it with uvicorn and follow one request from URL to route to function to JSON
  • Test routes without code on the Swagger UI page at /docs

Before you start

  • Basic Python: functions and dictionaries
  • Installing packages with pip

You've saved a model in a file. On one side there's an app, like a phone app or a website, and on the other side there's model.joblib. The app has a question. But a file just sits on the disk. It can't answer anyone.

So we put a server in between: a small program that waits at a web address. The app sends it a request, and the server sends back a response, as JSON. A web address that answers requests like this is called an API. That's all the word means.

Before we put a model behind it, let's get to know FastAPI on its own, with nothing else in the way.

Four layers, top to bottom

Browser, uvicorn, FastAPI app, your functions Browser: asks for a URL uvicorn: the server, listens for requests FastAPI app: the route table GET / → home GET /hello/{name} → hello Your functions: home(), hello(name) request JSON
The whole picture. The layer that matters today is the third one: it connects a URL to one of your functions.

Three names to know:

  • FastAPI is a Python framework where a plain function can answer a web request. Its docs live on the official site, fastapi.tiangolo.com.
  • uvicorn is its partner: the server that listens and passes requests in.
  • Pydantic checks data. It installs along with FastAPI, and we don't need it yet.

The project: two files

The whole project is a folder with main.py (the app) and requirements.txt:

fastapi==0.162.0
uvicorn==0.54.0

Install both in your virtual environment:

pip install -r requirements.txt

Pydantic, Starlette and a few others come along with them (the real run installed pydantic-2.14.2 and starlette-1.10.0, among others).

main.py: a decorator adds a route

from fastapi import FastAPI

app = FastAPI(title="Hello API")


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


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

Line by line:

  • app = FastAPI(title="Hello API") makes one object called app. It holds everything.
  • @app.get("/") is a decorator, and here's the idea that matters: it writes one line into the app's route table. A GET on / goes to the function home.
  • home itself is plain Python. It returns a dictionary.
  • The second route is the same idea: GET /hello/{name} goes to hello.

So each @app.get writes one line in the route table.

Start the server

So far this is only a file. In the terminal, from the project folder:

python -m uvicorn main:app --port 8000

main is the file main.py, app is the object inside it, and 8000 is the door number (the port).

INFO:     Started server process [27316]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

127.0.0.1 just means this computer. And notice it doesn't finish like a normal script. It keeps running and waits.

Follow one request

Open a browser at http://127.0.0.1:8000/, with the slash at the end. Here's the trip:

  1. uvicorn takes the request.
  2. It hands it to the app, and the app looks in the route table. GET / is a match, and it points to home.
  3. home runs and returns the dictionary.
  4. FastAPI turns it into JSON, and it rides back to the browser.
{"message":"Hello World"}

And the terminal prints one line for the request:

INFO:     127.0.0.1:59109 - "GET / HTTP/1.1" 200 OK

URL, route, function, JSON. That's the whole trip.

A piece of the URL becomes a parameter

Now go to /hello/Emma. The table finds the second route, because the curly braces in /hello/{name} match any word. So Emma becomes the parameter name, and you didn't write any code to pull it out.

{"message":"Hello Emma"}
INFO:     127.0.0.1:59113 - "GET /hello/Emma HTTP/1.1" 200 OK

What about a URL with no route? /nope finds no match, so FastAPI answers 404 by itself:

{"detail":"Not Found"}
INFO:     127.0.0.1:59114 - "GET /nope HTTP/1.1" 404 Not Found

And your code never ran.

A free test page at /docs

Here's the part people like about FastAPI. Go to http://127.0.0.1:8000/docs and there's a full page you never wrote. This is Swagger UI.

Swagger UI page titled Hello API, version 0.1.0, OAS 3.1, with a link to /openapi.json and two rows: GET / Home and GET /hello/{name} Hello.
The real /docs page of this app: one row per route.

It starts from the same route table. FastAPI writes that table out as one JSON description at /openapi.json, and the page reads it. That's why your two routes show up as two rows. The server log shows both requests:

INFO:     127.0.0.1:54978 - "GET /docs HTTP/1.1" 200 OK
INFO:     127.0.0.1:54978 - "GET /openapi.json HTTP/1.1" 200 OK

Try a route, no code

Open the /hello/{name} row and click Try it out. A box for name appears. Type Emma and click Execute. The page sends a real request, on the same trip as before:

Swagger UI response section. Request URL http://127.0.0.1:8000/hello/Emma. Code 200. Response body: message Hello Emma. Response headers include content-type application/json and server uvicorn.
Code 200 and the message "Hello Emma", with the exact URL the page called.

Type a value, click Execute, read the answer. No code at all. The same GET /hello/Emma line appears in the server log.

Common mistakes

  • Running python main.py. That just defines the app and exits. Start it with uvicorn: python -m uvicorn main:app.
  • Wrong file:object pair. main:app means the file main.py and the variable app. Rename either one and the command must change too.
  • Closing the terminal. The server lives in that terminal. Close it (or press CTRL+C) and the address stops answering.
  • Expecting a crash on an unknown URL. FastAPI answers 404 on its own. If you get 404, check the path in the route table on /docs.

Quick recap

PieceWhat it does
app = FastAPI(...)One object that holds the app and its route table
@app.get("/path")Writes one route: this URL goes to this function
{name} in the pathA piece of the URL becomes a function parameter
python -m uvicorn main:app --port 8000Starts the server that listens and passes requests in
/docsSwagger UI, built from /openapi.json, to try routes without code

A function can return anything, even a forecast. That's exactly what we do next: put the saved rain model behind a FastAPI route.

Try it yourself

Add a third route, @app.get("/bye/{name}"), that returns {"message": "Bye <name>"}. Restart uvicorn, then check that it shows up as a new row on /docs and try it from there.

Built this? Share your solution in the StudyEasy AI Community.

Join free on Skool →
← Previous
Saving and Loading Models with joblib: One Pipeline, One File
Next →
Deploy a Machine Learning Model with FastAPI: POST /predict for a Saved Model
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool
Go deeper with a course

Machine Learning Engineer: From First Model to ML Engineer

Learn machine learning in Python, then ship it: data analysis, regression, classification, tuning, deployment and MLOps.View on Udemy ↗: Machine Learning Engineer: From First Model to ML Engineer (opens in a new tab)

Related articles
Python Virtual Environments: One Project, One Toolbox

Why every Python project gets its own virtual environment: two projects that need different versions of requests, python -m venv .venv, activation, pip install and the PyCharm setting that activates it for you.

Git and GitHub Fundamentals: Commits, Branches, Push and Pull

The Git you use every day, on a tiny hello-ml project: commits as save points, the staging area, a branch merged with a fast-forward, then push, clone, pull and pull requests with a teammate.

Testing a FastAPI App with pytest: Three Small Tests

A tiny FastAPI app with a Hello World route and a name route that refuses numbers, three pytest tests that check it, and what happens when the validation is deleted by mistake.

Config and Logging Instead of Hard-Coded Values in ML Training

Take the hard-coded values out of a car price training script: one config.toml read with tomllib, logging with levels and a run id on every line, and secrets read from the environment. Same RMSE, 2195, before and after.

On this page
  • Four layers, top to bottom
  • The project: two files
  • main.py: a decorator adds a route
  • Start the server
  • Follow one request
  • A piece of the URL becomes a parameter
  • A free test page at /docs
  • Try a route, no code
  • Common mistakes
  • Quick recap
  • Try it yourself
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool
Go deeper with a course

Machine Learning Engineer: From First Model to ML Engineer

Learn machine learning in Python, then ship it: data analysis, regression, classification, tuning, deployment and MLOps.View on Udemy ↗: Machine Learning Engineer: From First Model to ML Engineer (opens in a new tab)

Single logo

Empowering Education, One Course at a Time

About
About usContact usFAQs
Legal
Terms of ServicePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.