Skip to content

A RESTful blog application built with FastAPI featuring user authentication (JWT tokens), CRUD operations for blog posts, user management, & SQLAlchemy ORM integration. The project includes secure password hashing, database models for users & blogs, and organized routing with separate modules for authentication, blog operations, & userΒ management.

Notifications You must be signed in to change notification settings

XBanTs/fastapi-blogapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

14 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ FastAPI Blog API

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.

FastAPI Blog API Screenshot 1

✨ Features

πŸ” Authentication & Security

  • 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 Management

  • User Registration - Create new user accounts
  • User Profile - Retrieve user information and associated blogs
  • Secure Login - Authenticate users with email/password

πŸ“ Blog Operations

  • 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

πŸ› οΈ Technical Features

  • 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 Blog API Screenshot 2

πŸ—οΈ Project Structure

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

πŸš€ Quick Start

Prerequisites

  • Python 3.8+
  • pip (Python package manager)

Installation

  1. Clone the repository

    git clone <repository-url>
    cd fastapi-project
  2. Create virtual environment

    python -m venv fastapi-env
    
    # Windows
    fastapi-env\Scripts\activate
    
    # macOS/Linux
    source fastapi-env/bin/activate
  3. Install dependencies

    pip install -r requirements.txt
  4. Run the application

    uvicorn blog.main:app --reload
  5. Access the API

πŸ“š API Endpoints

Authentication

Method Endpoint Description Auth Required
POST /login User login ❌

Users

Method Endpoint Description Auth Required
POST /user/ Create new user ❌
GET /user/{id} Get user by ID βœ…

Blogs

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 βœ…

System

Method Endpoint Description Auth Required
GET / Root endpoint ❌
GET /health Health check ❌

πŸ”§ Configuration

Environment Variables

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

Database Configuration

  • Development: SQLite (default)
  • Production: PostgreSQL (recommended)

πŸ§ͺ Usage Examples

1. Create a User

curl -X POST "http://localhost:8000/user/" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "John Doe",
    "email": "john@example.com",
    "password": "securepassword"
  }'

2. Login

curl -X POST "http://localhost:8000/login" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "username=john@example.com&password=securepassword"

3. Create a Blog (with token)

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."
  }'

πŸš€ Deployment

Render Deployment

This project is configured for easy deployment on Render:

  1. Push to GitHub
  2. Create Render Account
  3. Create PostgreSQL Database
  4. Create Web Service
  5. Set Environment Variables
  6. Deploy

Detailed deployment instructions are included in the project.

Local Development

# Run with auto-reload
uvicorn blog.main:app --reload --host 0.0.0.0 --port 8000

πŸ› οΈ Technologies Used

πŸ“ Database Schema

Users Table

  • id (Primary Key)
  • name (String)
  • email (String, Unique)
  • password (Hashed String)

Blogs Table

  • id (Primary Key)
  • title (String)
  • body (Text)
  • user_id (Foreign Key β†’ Users.id)

Relationships

  • One User can have many Blogs
  • Each Blog belongs to one User

πŸ”’ Security Features

  • 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

πŸ§ͺ Testing

Run the test suite:

pytest

πŸ“ API Documentation

Once the server is running, visit:

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • 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.

About

A RESTful blog application built with FastAPI featuring user authentication (JWT tokens), CRUD operations for blog posts, user management, & SQLAlchemy ORM integration. The project includes secure password hashing, database models for users & blogs, and organized routing with separate modules for authentication, blog operations, & userΒ management.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published