This project is a RESTful Blog API built with FastAPI, using SQLAlchemy for ORM, SQLite as the database, and Passlib for password hashing. The API supports full CRUD operations for blogs and users and includes relationships between users and the blogs they create.
- Create, read, update, and delete (CRUD) blog posts
- User registration with hashed passwords
- User-to-blog relationship
- Structured responses using Pydantic
- Error handling with proper HTTP status codes
- Swagger documentation (auto-generated)
- SQLite support (ideal for testing and small-scale apps)
blog/
├── __init__.py
├── main.py # Main FastAPI application with API routes
├── models.py # SQLAlchemy models for Blog and User
├── schemas.py # Pydantic models for data validation and serialization
├── database.py # DB engine, session maker, Base declaration
├── Hashing.py # Password hashing using Passlib
├── blog.db # SQLite database (auto-created)
├── requirements.txt # Python dependencies
Install all the dependencies:
pip install -r requirements.txt- Clone the repository:
git clone https://github.com/SinaGhaffarzadeh/blogzilla
cd blogzilla- Run the API using Uvicorn:
uvicorn blog.main:app --reloadNote: We use
blog.mainbecause themain.pyfile is located inside theblog/folder.
- Access the interactive API docs:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
This project uses SQLite as the database. Tables are automatically created when you start the server.
You can manage and visualize your SQLite database (blog.db) using TablePlus or any SQLite viewer.
Passwords are securely hashed using bcrypt from Passlib:
from passlib.context import CryptContext
pwd_cxt = CryptContext(schemes=["bcrypt"], deprecated="auto")This ensures no plain-text password is stored in the database.
| Method | Endpoint | Description |
|---|---|---|
| POST | /blog | Create a new blog |
| GET | /blog | Get all blogs |
| GET | /blog/{id} | Get blog by ID |
| PUT | /blog/{id} | Update a blog by ID |
| DELETE | /blog/{id} | Delete a blog by ID |
| Method | Endpoint | Description |
|---|---|---|
| POST | /user | Create a new user |
| GET | /user/{id} | Get user by ID |
- HTTP Status Codes: Used extensively to provide proper feedback (e.g.,
200 OK,201 Created,404 Not Found). - Response Models: Control what information is shown to users (e.g., excluding sensitive data like passwords).
- Dependency Injection: Used to manage DB sessions via
Depends(get_db). - Relationships: SQLAlchemy's
relationship()andForeignKeyused to link users and blogs.
- JWT-based authentication & authorization
- Pagination for blog listings
- Update and delete operations for users
- Unit testing and validation