A fully functional Social Media API using FastAPI, PostgreSQL, SQLAlchemy, JWT authentication, and WebSocket for real-time chat.
This project demonstrates how modern social media platforms handle:
- User authentication & authorization
- Posts with media (images & videos)
- Comments & likes/votes
- Real-time messaging
- Database migrations
- API structuring and routing
- Security (JWT, password hashing)
- File uploads & static file serving
- User registration
- User login with JWT tokens
- Password hashing with bcrypt
- Profile management (update/delete)
- Protected routes
- Create text posts
- Upload images (JPEG, PNG, GIF, WebP)
- Upload videos (MP4, MPEG, QuickTime)
- Edit/Delete own posts
- Search & pagination
- View posts with vote counts
- Add comments to posts
- Edit/Delete own comments
- Fetch comments for posts
- Like/Unlike posts
- Vote count per post
- Prevent duplicate votes
- Send direct messages
- WebSocket real-time delivery
- Redis caching (optional)
- Chat history
- Online status detection
- Read receipts
| Technology | Purpose |
|---|---|
| FastAPI | Web framework |
| PostgreSQL | Database |
| SQLAlchemy | ORM |
| Alembic | Migrations |
| Pydantic | Schemas & validation |
| Uvicorn | ASGI server |
| WebSockets | Real-time chat |
| Redis | Caching (optional) |
| Bcrypt | Password hashing |
| JWT | Authentication |
fastapi/
βββ app/
β βββ main.py # Main application
β βββ models.py # Database models
β βββ schema.py # Pydantic schemas
β βββ auth.py # Login endpoint
β βββ oauth2.py # JWT authentication
β βββ db.py # Database connection
β βββ config.py # Settings
β βββ utils.py # Password hashing
β βββ redis_client.py # Redis connection
β βββ routers/
β βββ users.py # User endpoints
β βββ posts.py # Post endpoints
β βββ comments.py # Comment endpoints
β βββ votes.py # Vote endpoints
β βββ messages.py # Chat endpoints
βββ alembic/ # Database migrations
βββ uploads/ # Uploaded media files
βββ requirements.txt
βββ chat_test.html # Browser chat test
βββ README.md
git clone https://github.com/Hareg-dev/Socialmedia.git
cd Socialmediauv venv
.venv\Scripts\activate # Windowsuv pip install -r requirements.txt- Install PostgreSQL
- Create database:
fastapi-cource - Update
.envfile with your credentials
alembic upgrade headfastapi dev app/main.pyServer runs at: http://localhost:8000
API Docs: http://localhost:8000/docs
POST /users/- Register new userPOST /login- Login and get JWT tokenGET /users/me- Get current user
POST /posts/upload- Upload image/videoPOST /posts/- Create postGET /posts/- Get all postsGET /posts/{id}- Get single postPUT /posts/{id}- Update postDELETE /posts/{id}- Delete post
POST /comments/- Add commentGET /comments/- Get all commentsPUT /comments/{id}- Update commentDELETE /comments/{id}- Delete comment
POST /votes/- Like/Unlike post
POST /messages/- Send messageGET /messages/?user_id={id}- Get chat historyWS /messages/ws/{user_id}- WebSocket connection
- Go to
http://localhost:8000/docs - Click "Authorize" and enter JWT token
- Test any endpoint
- Create user:
POST /users/ - Login:
POST /login - Copy token and add to headers:
Authorization: Bearer {token} - Test endpoints
- Open
chat_test.htmlin Chrome - Open
chat_test.htmlin Edge - Login as different users
- Start chatting in real-time!
Create .env file:
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=your_password
DATABASE_NAME=fastapi-cource
HOST=localhost
PORT=5432
SECRET_KEY=your_secret_key
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30- Facebook - Posts, comments, likes, chat
- Instagram - Media posts, likes, comments
- Twitter - Posts, likes
- WhatsApp - Real-time messaging
- Celery background tasks
- User profile pictures
- Friend/Follow system
- News feed algorithm
- Push notifications
- Group chats
- Stories feature
- Video calls
Pull requests are welcome! For major changes, please open an issue first.
MIT
Built with β€οΈ using FastAPI