This is a fully functional FastAPI demo project that covers essential features like authentication, database integration, background tasks, WebSockets, and more.
✅ Class-Based Views (CBVs)
✅ RESTful Routes (GET, POST, PUT, DELETE)
✅ SQLAlchemy ORM & Alembic Migrations
✅ JWT Authentication
✅ Middleware for Logging
✅ Background Tasks
✅ WebSockets Support
✅ Pydantic for Data Validation
✅ Automatic API Documentation (Swagger & ReDoc)
✅ CORS Handling
✅ Asynchronous Request Handling
fastapi_demo/
│── main.py # Entry point for FastAPI app
│── config.py # Configuration settings
│── database.py # Database connection setup
│── models.py # SQLAlchemy models
│── schemas.py # Pydantic schemas for request/response
│── dependencies.py # Dependency injection setup
│── routers/
│ │── auth.py # Authentication routes (Register/Login)
│ │── users.py # User CRUD operations
│── middleware.py # Middleware for logging requests
│── background_tasks.py # Background task example
│── utils.py # WebSocket manager
│── requirements.txt # Required dependencies
│── alembic/ # Alembic migrations
git clone https://github.com/dhimanparas20/Learn_Fast_API
cd Learn_Fast_APIpython -m venv venv
source venv/bin/activate # On macOS/Linux
venv\Scripts\activate # On Windowspip install -r requirements.txtalembic upgrade headThis will create the database tables.
Start the server with:
uvicorn main:app --reload🚀 The API will be available at: http://127.0.0.1:8000
FastAPI automatically generates interactive API docs:
📌 Swagger UI: http://127.0.0.1:8000/docs
📌 ReDoc UI: http://127.0.0.1:8000/redoc
curl -X 'POST' 'http://127.0.0.1:8000/auth/register' \
-H 'Content-Type: application/json' \
-d '{"username": "testuser", "email": "test@example.com", "password": "testpassword"}'curl -X 'POST' 'http://127.0.0.1:8000/auth/login' \
-H 'Content-Type: application/json' \
-d '{"username": "testuser", "password": "testpassword"}'curl -X 'GET' 'http://127.0.0.1:8000/users/' \
-H 'Authorization: Bearer YOUR_JWT_TOKEN'Replace YOUR_JWT_TOKEN with the token received from login.
1️⃣ Install Pytest & HTTPX
pip install pytest httpx2️⃣ Run Tests
pytest test_main.pyTrigger an email send task in the background:
curl -X 'POST' 'http://127.0.0.1:8000/tasks/send-email' \
-H 'Content-Type: application/json' \
-d '{"email": "user@example.com", "message": "Hello from FastAPI!"}'Connect to the WebSocket:
import asyncio
import websockets
async def test_websocket():
async with websockets.connect("ws://127.0.0.1:8000/ws") as websocket:
await websocket.send("Hello Server!")
response = await websocket.recv()
print(f"Received: {response}")
asyncio.run(test_websocket())To deploy with Gunicorn:
gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:appFor Docker Deployment:
1️⃣ Create a Dockerfile
FROM python:3.11
WORKDIR /app
COPY . /app
RUN pip install --no-cache-dir -r requirements.txt
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]2️⃣ Build & Run
docker build -t fastapi-demo .
docker run -p 8000:8000 fastapi-demoThis project is MIT licensed.
Pull requests are welcome! Feel free to open an issue for discussions.