A RESTful Blog API built with FastAPI, SQLAlchemy, and JWT Authentication.
- 🔐 JWT Authentication - Secure token-based authentication
- 👤 User Management - Create, read, update, delete users
- 📝 Blog Management - Full CRUD operations for blog posts
- 🔒 Authorization - Users can only modify their own data
- 📚 API Documentation - Auto-generated Swagger UI and ReDoc
- 🧪 Testing - Comprehensive test suite with pytest
FastAPI_Blog_Project/
├── app/
│ ├── api/v1/routes/ # API route handlers
│ │ ├── auth.py # Authentication endpoints
│ │ ├── blog.py # Blog endpoints
│ │ └── user.py # User endpoints
│ ├── core/ # Core configurations
│ │ ├── config.py # Settings management
│ │ ├── database.py # Database connection
│ │ └── security.py # JWT & password utilities
│ ├── models/ # SQLAlchemy models
│ ├── schemas/ # Pydantic schemas
│ └── services/ # Business logic
├── migrations/ # Alembic migrations
├── tests/ # Test suite
└── .env # Environment variables
- Python 3.10+
- MySQL server running on localhost:3306
- Database
blog_dbcreated
- Clone the repository:
git clone <repository-url>
cd FastAPI_Blog_Project- Create and activate virtual environment:
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Configure environment variables - create a
.envfile:
DATABASE_URL=mysql+pymysql://user:password@localhost:3306/blog_db
SECRET_KEY="your-secret-key-here"
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30Tip: Generate a secure secret key with:
openssl rand -base64 32
- Run database migrations:
alembic upgrade headfastapi devfastapi runThe API will be available at:
- API:
http://127.0.0.1:8000 - Swagger Docs:
http://127.0.0.1:8000/docs - ReDoc:
http://127.0.0.1:8000/redoc
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /api/v1.1/auth/login |
Login and get JWT token | ❌ |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /api/v1.1/users/ |
Create a new user | ❌ |
| GET | /api/v1.1/users/ |
Get all users | ❌ |
| GET | /api/v1.1/users/me |
Get current user | ✅ |
| GET | /api/v1.1/users/{id} |
Get user by ID | ❌ |
| PUT | /api/v1.1/users/{id} |
Update user | ✅ (own profile only) |
| DELETE | /api/v1.1/users/{id} |
Delete user | ✅ (own account only) |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /api/v1.1/blogs/ |
Create a new blog | ✅ |
| GET | /api/v1.1/blogs/ |
Get all blogs | ❌ |
| GET | /api/v1.1/blogs/{id} |
Get blog by ID | ❌ |
| PUT | /api/v1.1/blogs/{id} |
Update blog | ✅ |
| DELETE | /api/v1.1/blogs/{id} |
Delete blog | ✅ |
curl -X POST "http://127.0.0.1:8000/api/v1.1/auth/login" \
-d "username=your_email@example.com&password=your_password"Response:
{
"access_token": "eyJhbGciOiJIUzI1NiIs...",
"token_type": "bearer"
}Include the token in the Authorization header:
curl -X GET "http://127.0.0.1:8000/api/v1.1/users/me" \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIs..."- Go to
http://127.0.0.1:8000/docs - Click the Authorize button (🔓)
- Enter your email as username and password
- Click Authorize
- All protected endpoints will now include your token automatically
After making changes to your models:
alembic revision --autogenerate -m "description of changes"Run all pending migrations:
alembic upgrade headRollback the last migration:
alembic downgrade -1alembic historyalembic currentpytestpytest --cov=app --cov-report=htmlpytest tests/test_users.py
pytest tests/test_blogs.pypytest tests/test_users.py::TestAuthentication
pytest tests/test_blogs.py::TestCreateBlogpytest -vcurl -X POST "http://127.0.0.1:8000/api/v1.1/users/" \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "john@example.com", "password": "securepass123"}'curl -X POST "http://127.0.0.1:8000/api/v1.1/auth/login" \
-d "username=john@example.com&password=securepass123"curl -X POST "http://127.0.0.1:8000/api/v1.1/blogs/" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <your-token>" \
-d '{"title": "My First Blog", "content": "Hello World!", "author_id": 1}'curl -X GET "http://127.0.0.1:8000/api/v1.1/blogs/"This project is licensed under the MIT License - see the LICENSE file for details.
Vinald - vinald.me