π₯οΈ Step 1: Create Project Folder
π macOS / Linux
mkdir fastapi-project
cd fastapi-projectπͺ Windows
mkdir fastapi-project
cd fastapi-projectπ Step 2: Create Virtual Environment
π macOS / Linux
python3 -m venv venvπͺ Windows
python -m venv venvThis creates a folder named venv/ containing the isolated Python environment.
βΆοΈ Step 3: Activate Virtual Environment
π macOS / Linux
source venv/bin/activateπͺ Windows
venv\\Scripts\\activateAfter activation, your terminal should look like:
(venv) fastapi-project %That means your virtual environment is active β
π¦ Step 4: Install FastAPI & Uvicorn
We need:
FastAPI β Web framework
Uvicorn β ASGI server to run FastAPI
π What is ASGI?
ASGI (Asynchronous Server Gateway Interface) is a standard that allows Python apps to handle:
HTTP requests
WebSockets
Async operations
FastAPI is built on ASGI.
So it needs an ASGI-compatible server to run.
π macOS / Linux
pip install fastapi uvicornπͺ Windows
pip install fastapi uvicorn(Inside virtual environment, pip works correctly on both.)
π Step 5: Create Main Application File
Create a file named:
main.pyAdd this code:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Hello, FastAPI π"}βΆοΈ Step 6: Run the Server
π macOS / Linux
uvicorn main:app --reloadπͺ Windows
uvicorn main:app --reloadπ What This Command Means
mainβ filename (main.py)appβ FastAPI instance-reloadβ auto-restarts server on code changes
π Step 7: Open in Browser
Visit:
<http://127.0.0.1:8000>You should see:
{"message": "Hello, FastAPI π"}