Fast Api : Python Backend Framework
mkdir fastapi-backend
cd fastapi-backend
Create virtual environment (THIS IS IMPORTANT)
python3 -m venv venv
Activate it:
source venv/bin/activate
You should see:
(venv) wajahat@ubuntu:...
Install FastAPI + Uvicorn
pip install fastapi uvicorn
Create main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def root():
return {"message": "Hello FastAPI"}
Run the server
uvicorn main:app --reload
Open browser:
http://127.0.0.1:8000
Docs:
http://127.0.0.1:8000/docs
uv add python-dotenv
pip equivalent:
pip install python-dotenv
What it does:
This library loads environment variables from a .env file. Useful for secrets like API keys or database URLs without hardcoding them in your code.
uv add fastapi-users[sqlalchemy]
pip equivalent:
pip install "fastapi-users[sqlalchemy]"
What it does:
FastAPI Users is a user authentication and management library. The [sqlalchemy] extra installs support for SQLAlchemy databases, so you can easily handle signup, login, and user models.
uv add imagekitio
pip equivalent:
pip install imagekitio
What it does:
ImageKit.io is an SDK to work with the ImageKit service. It helps you upload, optimize, and manage images in your backend without writing custom image processing code.
uv add uvicorn[standard]
pip equivalent:
pip install "uvicorn[standard]"
What it does:
Uvicorn is the ASGI server that runs your FastAPI app. The [standard] extra adds recommended packages like httptools and uvloop for better performance.
uv add aiosqlite
pip equivalent:
pip install aiosqlite
What it does:
Aiosqlite is an async wrapper for SQLite. It lets your FastAPI app talk to a SQLite database using async/await, keeping your server fast and non-blocking.
Now you can add .env file and remember to
Project structure for scalability
As projects grow, it’s better to separate files:
project-root/
├── app/
│ ├── main.py # FastAPI app
│ ├── models.py # DB models
│ ├── routes.py # API endpoints
│ └── config.py # Settings, env loader
├── env/ # virtual environment
├── requirements.txt
└── .env # your secret keys
do this as projects grows…
peace out for now :)
from fastapi import FastAPI
app = FastAPI()
@app.get("/auth") # This is called a decorator in Python. Takes a function and modifies or registers it in some way.
def authenticating():
return {"isLoggedIn" : "saeed here "}
This is called a decorator in Python.
A decorator is something that:
Takes a function and modifies or registers it in some way.
@app.get("/auth")
“Whenever someone sends a GET request to /auth, run the function written below.”
Python internally converts this:
@app.get(“/auth”)
def authentication():
return {“hi” : “ I am saeed”}
into something like this:
def authenticating():
return {"isLoggedIn": "saeed here"}
app.get("/auth")(authenticating)
So basically:
app.get("/auth")returns a functionThat function takes
authenticatingas argumentFastAPI stores it in a routing table
So FastAPI now knows:
Route: GET /auth → Call authenticating()
@app.get("/user/{user_id}")
def get_user(user_id: int):
return {"user_id": user_id}
If you open:
/user/10
FastAPI automatically:
Extracts 10
Converts to int
Passes it into get_user()
This is called:
Path Parameter
run this command to run you app :
uvicorn app.app:app –reload


