A robust RESTful API built with FastAPI that provides a complete backend solution for a social media application with posts, comments, and likes functionality. Features include JWT-based authentication, PostgreSQL database integration, and comprehensive CRUD operations.
- User Authentication & Authorization: JWT token-based authentication with secure password hashing
- Posts Management: Create, read, update, and delete posts with search functionality
- Comments System: Add and manage comments on posts
- Likes/Reactions: Like posts and comments
- User Profiles: User registration and profile management
- Database Migrations: Alembic for database version control
- Security: Password hashing with bcrypt, OAuth2 implementation
- Search Functionality: Search posts by title and content
- Framework: FastAPI - Modern, fast web framework for building APIs
- Database: PostgreSQL with SQLAlchemy ORM
- Authentication: JWT (JSON Web Tokens) with python-jose
- Password Hashing: Bcrypt via passlib
- Migrations: Alembic
- Validation: Pydantic schemas
- Environment Management: python-dotenv
- Python 3.8+
- PostgreSQL database
- pip (Python package manager)
-
Clone the repository
git clone github.com/dhruvkshah75/Backend_API cd Backend_API -
Create a virtual environment
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
-
Set up environment variables
Create a
.envfile in the root directory with the following variables:# Database Configuration DB_NAME=your_database_name DB_USER=your_database_user DB_PASSWORD=your_database_password DB_HOST=localhost DB_PORT=5432 # JWT Configuration SECRET_KEY=your_secret_key_here ALGORITHM=HS256 ACCESS_TOKEN_EXPIRE_MINUTES=30
-
Run database migrations
alembic upgrade head
-
Start the application
uvicorn app.main:app --reload
The API will be available at http://localhost:8000
Once the application is running, you can access:
- Interactive API docs (Swagger UI): http://localhost:8000/docs
- Alternative API docs (ReDoc): http://localhost:8000/redoc
POST /login- User login, returns JWT access token
POST /users- Create a new user accountGET /users/{id}- Get user details by ID
GET /posts- Get all posts (with pagination and search)POST /posts- Create a new post (requires authentication)GET /posts/{id}- Get a specific post by IDPUT /posts/{id}- Update a post (owner only)DELETE /posts/{id}- Delete a post (owner only)
GET /comments- Get comments for postsPOST /comments- Add a comment to a post (requires authentication)PUT /comments/{id}- Update a comment (owner only)DELETE /comments/{id}- Delete a comment (owner only)
POST /likes/posts/{id}- Like/unlike a postPOST /likes/comments/{id}- Like/unlike a comment
API-Development/ <-- Root Directory
├── alembic/ <-- Database migration files
│ ├── versions/
│ ├── env.py
│ ├── script.py.mako
│ └── README
├── app/ <-- Main application source code
│ ├── routers/ <-- Route handlers
│ │ ├── __init__.py
│ │ ├── auth.py <-- Login/Authentication routes
│ │ ├── post.py <-- Post CRUD routes
│ │ ├── user.py <-- User creation/retrieval routes
│ │ ├── comment.py <-- Comment management routes
│ │ └── likes.py <-- Likes/reactions routes
│ ├── __init__.py
│ ├── config.py <-- Environment variable settings
│ ├── database.py <-- Database connection logic
│ ├── main.py <-- App entry point
│ ├── models.py <-- SQLAlchemy database models
│ ├── oauth2.py <-- JWT token creation & verification
│ ├── schemas.py <-- Pydantic models (Request/Response schemas)
│ └── utils.py <-- Password hashing utilities
├── .env <-- Environment variables (not in git)
├── .gitignore <-- Files to ignore in git
├── alembic.ini <-- Alembic configuration
├── dockerfile <-- Docker image instructions
└── README.md <-- This file
id(Primary Key)email(Unique)username_id(Unique)password(Hashed)created_at
id(Primary Key)titlecontentpublishedcreated_atowner_id(Foreign Key → User)
id(Primary Key)contentcreated_atowner_id(Foreign Key → User)post_id(Foreign Key → Post)
- Composite Primary Key: (
user_id,post_id)
- Composite Primary Key: (
user_id,comment_id)
- Password Hashing: All passwords are hashed using bcrypt before storage
- JWT Authentication: Secure token-based authentication
- Authorization: Route-level protection ensuring users can only modify their own content
- Input Validation: Pydantic schemas validate all incoming data
Run tests using pytest:
pytestBuild and run with Docker:
docker build -t backend-api .
docker run -p 8000:8000 backend-api- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Dhruv Shah
- FastAPI documentation and community
- SQLAlchemy documentation
- PostgreSQL documentation