A full-featured social media backend API built with FastAPI, PostgreSQL, SQLAlchemy, and JWT authentication.
It demonstrates clean architecture, modular routing, Alembic migrations, password hashing, and robust testing using Pytest.
Add API demo screenshot or OpenAPI docs preview here.
User Authentication
- JWT-based login system
- Password hashing with
passlib - Token verification & protected routes
Post Management
- Create, Read, Update, Delete (CRUD) operations
- Ownership checks (only authors can modify/delete)
- Pagination & search support
Voting System
- Upvote / downvote functionality
- Prevents duplicate votes
Database & ORM
- PostgreSQL + SQLAlchemy ORM
- Alembic for migrations
- Pydantic models for validation
Testing
- Pytest with isolated test database
- Fixtures for posts, users, and auth
- CI-ready test configuration
app/
│
├── __init__.py
├── main.py # FastAPI entry point
├── models.py # SQLAlchemy models
├── schemas.py # Pydantic schemas
├── utils.py # Password hashing utilities
├── oauth2.py # JWT creation & validation
├── config.py # Environment configuration
├── database.py # SQLAlchemy engine & session
│
├── routers/ # API routes
│ ├── post.py
│ ├── user.py
│ ├── auth.py
│ └── vote.py
│
├── tests/ # Pytest suite
│ ├── conftest.py
│ ├── test_users.py
│ ├── test_posts.py
│ ├── test_votes.py
│ └── ...
│
├── alembic/ # Migration scripts
├── alembic.ini # Alembic config
├── requirements.txt
└── README.md
git clone https://github.com/<your-username>/fastapi-social-media.git
cd fastapi-social-mediapython -m venv venv
source venv/bin/activate # Linux / macOS
venv\Scripts\activate # Windowspip install -r requirements.txtCreate a .env file in the project root:
database_host_name=localhost
database_port=5432
database_password=yourpassword
database_name=fastapi
database_username=postgres
secret_key=your_secret_key
algorithm=HS256
access_token_expire_minutes=30alembic upgrade headuvicorn app.main:app --reloadServer runs at http://127.0.0.1:8000
Swagger docs at http://127.0.0.1:8000/docs
pytest -v| Method | Endpoint | Description |
|---|---|---|
| POST | /login |
User login, returns access token |
| Method | Endpoint | Description |
|---|---|---|
| POST | /users/ |
Create a new user |
| GET | /users/{id} |
Retrieve a user by ID |
| Method | Endpoint | Description |
|---|---|---|
| GET | /posts/ |
List all posts |
| GET | /posts/{id} |
Retrieve a single post |
| POST | /posts/ |
Create a new post |
| PUT | /posts/{id} |
Update an existing post |
| DELETE | /posts/{id} |
Delete a post |
| Method | Endpoint | Description |
|---|---|---|
| POST | /vote/ |
Cast or remove a vote on a post |
docker compose -f docker-compose-dev.yml up --builddocker compose -f docker-compose-prod.yml up --build -d| Layer | Technology |
|---|---|
| Framework | FastAPI |
| Database | PostgreSQL |
| ORM | SQLAlchemy |
| Migrations | Alembic |
| Authentication | JWT via python-jose |
| Testing | Pytest |
| Containerization | Docker, Docker Compose |
