Sitemap
Erlan Akbaralievich

Erlan Akbaralievich

Fastapi Tutorial 1

4 min readAug 2, 2026

--

Setup project

2 ways of project setup:

  • Option A: uv (Recommended)
uv init Fastapi_Tutorial --bare
cd Fastapi_Tutorial
uv venv # creates .venv and activates
uv add "fastapi[standard]" # add fastapi with standard dependencies (pydantic, uvicorn...)
  • Option B: standard pip + venv
mkdir Fastapi_Tutorial && cd Fastapi_Tutorial
python -m venv .venv
source .venv/bin/activate
pip install "fastapi[standard]"

What’s uv?

It’s a modern, extremely fast python package, project manager written in Rust. Most python devs are switching to it right now.

It’s all in one replacement for pip, venv, poetry (pyproject.toml).

# Install python -- pyenv install 3.12    -- uv python install 3.12
# Virtual envir -- python3 -m venv .venv -- uv venv
# Add package -- pip install fastapi -- uv add fastapi
# Run a script -- python main.py. -- uv run main.py

uv also creates pyproject.toml

Why use pyproject.toml?

  1. pyproject.toml is an improved version of requirements.txt

In requirements.txt even sub-dependencies of a package are stored and when we want to remove a package, it becomes hard to clean requirements.txt including all sub-dependencies of the package.

In pyproject.toml we only list packages:

dependencies = [
"fastapi[standard]>=0.110.0"
]

2. Seperating production and developer packages

Developers usually seperate dependencies into requirements.txt and requirements-dev.txt. pyproject.toml natively handles groups:

[project]
dependencies = [
"fastapi[standard]",
]

[dependency-groups]
dev = [
"pytest",
"ruff",
]

3. Make your code importable

Have you ever seen this type of error:

ModuleNotFoundError: No module named 'app'

To resolve this issue developers usually use PYTHONPATH, everytime a developer clones or opens a project, he needs to set PYTHONPATH. With pyproject.toml we set package path only once and we never have to set PYTHONPATH again. Nobody will get ModuleNotFoundError including those who clone the project for the first time.

First steps

After completing the following project setup with uv, create main.py:

uv init Fastapi_Tutorial --bare
cd Fastapi_Tutorial
uv venv # creates .venv and activates
uv add "fastapi[standard]" # add fastapi with standard dependencies (pydantic, uvicorn...)

touch main.py

The simplest FastApi file looks like this:

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
return {"message": "Hello fastapi"}

FastAPI is a python class that provides all the functionality for your API.

Get Erlan Akbaralievich’s stories in your inbox

Join Medium for free to get updates from this writer.

@app.get(“/”) tells FastApi that the function right below is in charge of handling requests that go to:

  • the path “/”
  • using get http method

You can also use other operations like:

  • @app.post()
  • @app.put()
  • @app.delete()
  • @app.patch()

You could also define the root api as a normal, non-async function:

@app.get("/")
def root():
return {"message": "Hello fastapi"}

If you don’t the know the difference between sync and async, check out this async in a hurry tutorial https://fastapi.tiangolo.com/async/

Run the server:

❯ uv run fastapi dev

⚡️ Starting FastAPI in development mode

🐍 Using import string: main:app (auto-discovered, use --verbose to learn more)

💡 You can configure an entrypoint in pyproject.toml for this app with:

[tool.fastapi]
entrypoint = "main:app"

🌐 Server started at http://127.0.0.1:8000
Documentation at http://127.0.0.1:8000/docs

Logs:

▕ Will watch for changes in these directories: ['/Users/erlan/Documents/Dev/Fastapi_Tutorial']
▕ Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
▕ Started reloader process [5132] using WatchFiles
▕ Started server process [5134]
▕ Waiting for application startup.
▕ Application startup complete.

Open your browser at http://127.0.0.1:8000/

You will see the JSON response as: { “message”: “Hello fastapi” }

Interactive API docs

http://127.0.0.1:8000/docs

Press enter or click to view image in full size

http://127.0.0.1:8000/redoc

Press enter or click to view image in full size

The first is provided by Swagger UI and is the most popular one.

The second one is provided by Redoc and is the more modern version of /doc interactive api.

Thanks to these interactive apis, we don’t need Postman for testing our apis at least basic cases (you’ll learn about postman and api testing later).

Configure the app entrypoint in pyproject.toml

If your code was structured like this:

.
├── backend
│ ├── main.py
│ ├── __init__.py

You must configure where your app is located in pyproject.toml like this:

[tool.fastapi]
entrypoint = "backend.main:app"

Deploy your app (optional)

You can also deploy your app to https://fastapicloud.com/ with a single command:

❯ uv run fastapi deploy

Deploying to FastAPI Cloud...

✅ Deployment successful!

🐔 Ready the chicken! Your app is ready at https://myapp.fastapicloud.dev

If you’re not logged with your account to fastapicloud, your browser will open to complete the authentication process.

--

--