A modern, high-performance RESTful blog API built with FastAPI, featuring JWT authentication, user management, and comprehensive blog operations. This project demonstrates best practices in API development with Python, including proper project structure, security implementation, and database management.
- JWT Token Authentication - Secure user authentication with JSON Web Tokens
- Password Hashing - Bcrypt encryption for secure password storage
- OAuth2 Bearer Token - Industry-standard authentication flow
- Protected Routes - Secure endpoints requiring authentication
- User Registration - Create new user accounts
- User Profile - Retrieve user information and associated blogs
- Secure Login - Authenticate users with email/password
- Create Blogs - Authenticated users can create new blog posts
- Read Blogs - Retrieve all blogs or specific blog by ID
- Update Blogs - Edit existing blog posts
- Delete Blogs - Remove blog posts
- User-Blog Relationships - Each blog is associated with its creator
- SQLAlchemy ORM - Robust database operations with relationships
- Pydantic Models - Data validation and serialization
- Alembic Migrations - Database schema version control
- Modular Architecture - Clean separation of concerns
- Environment Configuration - Production-ready settings management
- CORS Support - Cross-origin resource sharing enabled
- API Documentation - Auto-generated interactive docs
fastapi-project/
βββ blog/ # Main application package
β βββ routers/ # API route handlers
β β βββ authentication.py # Auth endpoints (/login)
β β βββ blog.py # Blog CRUD endpoints
β β βββ user.py # User management endpoints
β βββ repository/ # Data access layer
β β βββ blog.py # Blog database operations
β β βββ user.py # User database operations
β βββ alembic/ # Database migrations
β βββ config.py # Environment configuration
β βββ database.py # Database connection setup
β βββ hashing.py # Password hashing utilities
β βββ main.py # FastAPI application instance
β βββ models.py # SQLAlchemy database models
β βββ oauth2.py # OAuth2 authentication logic
β βββ schemas.py # Pydantic data models
β βββ token.py # JWT token operations
βββ assets/ # Project screenshots
βββ build.sh # Render deployment build script
βββ start.sh # Render deployment start script
βββ requirements.txt # Python dependencies
βββ README.md # Project documentation
- Python 3.8+
- pip (Python package manager)
-
Clone the repository
git clone <repository-url> cd fastapi-project
-
Create virtual environment
python -m venv fastapi-env # Windows fastapi-env\Scripts\activate # macOS/Linux source fastapi-env/bin/activate
-
Install dependencies
pip install -r requirements.txt
-
Run the application
uvicorn blog.main:app --reload
-
Access the API
- API: http://localhost:8000
- Interactive Docs: http://localhost:8000/docs
- Alternative Docs: http://localhost:8000/redoc
Method | Endpoint | Description | Auth Required |
---|---|---|---|
POST | /login |
User login | β |
Method | Endpoint | Description | Auth Required |
---|---|---|---|
POST | /user/ |
Create new user | β |
GET | /user/{id} |
Get user by ID | β |
Method | Endpoint | Description | Auth Required |
---|---|---|---|
GET | /blog/ |
Get all blogs | β |
POST | /blog/ |
Create new blog | β |
GET | /blog/{id} |
Get blog by ID | β |
PUT | /blog/{id} |
Update blog | β |
DELETE | /blog/{id} |
Delete blog | β |
Method | Endpoint | Description | Auth Required |
---|---|---|---|
GET | / |
Root endpoint | β |
GET | /health |
Health check | β |
Create a .env
file or set environment variables:
SECRET_KEY=your-super-secret-key-here
DATABASE_URL=postgresql://user:password@localhost/dbname # For production
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
- Development: SQLite (default)
- Production: PostgreSQL (recommended)
curl -X POST "http://localhost:8000/user/" \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "john@example.com",
"password": "securepassword"
}'
curl -X POST "http://localhost:8000/login" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=john@example.com&password=securepassword"
curl -X POST "http://localhost:8000/blog/" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"title": "My First Blog",
"body": "This is the content of my first blog post."
}'
This project is configured for easy deployment on Render:
- Push to GitHub
- Create Render Account
- Create PostgreSQL Database
- Create Web Service
- Set Environment Variables
- Deploy
Detailed deployment instructions are included in the project.
# Run with auto-reload
uvicorn blog.main:app --reload --host 0.0.0.0 --port 8000
- FastAPI - Modern, fast web framework for building APIs
- SQLAlchemy - SQL toolkit and ORM
- Pydantic - Data validation using Python type hints
- Alembic - Database migration tool
- Passlib - Password hashing library
- Python-JOSE - JWT implementation
- Uvicorn - ASGI server implementation
id
(Primary Key)name
(String)email
(String, Unique)password
(Hashed String)
id
(Primary Key)title
(String)body
(Text)user_id
(Foreign Key β Users.id)
- One User can have many Blogs
- Each Blog belongs to one User
- Password Hashing: Bcrypt with salt
- JWT Tokens: Secure authentication tokens
- Token Expiration: Configurable token lifetime
- Protected Routes: Authentication required for sensitive operations
- CORS Configuration: Cross-origin request handling
- Input Validation: Pydantic model validation
Run the test suite:
pytest
Once the server is running, visit:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- FastAPI team for the excellent framework
- SQLAlchemy team for the robust ORM
- The Python community for amazing libraries
Built with β€οΈ using FastAPI
Please give this repository a star and follow my account also, cheers! For questions or support, please open an issue in the repository.