A REST API for a simple social media app, built while learning backend development with FastAPI. Users can sign up, log in, create posts, and vote on posts.
- User accounts with passwords hashed with bcrypt (
passlib) - JWT authentication: log in to get a token, then send it with every request
- Posts: create, read, update and delete (only the owner can edit or delete a post)
- Votes: like or unlike a post, with vote counts included when you fetch posts
- Search and pagination on posts (
search,limit,skip) - Database migrations with Alembic
- Docker setup with the API and PostgreSQL in separate containers
- Interactive API docs generated automatically by FastAPI
| Part | Tool |
|---|---|
| Web framework | FastAPI |
| Database | PostgreSQL |
| ORM | SQLAlchemy |
| Migrations | Alembic |
| Validation | Pydantic |
| Auth | python-jose (JWT), passlib |
| Deployment | Docker, Docker Compose |
| Method | Endpoint | Description | Auth |
|---|---|---|---|
POST |
/users/ |
Create an account | |
GET |
/users/{id} |
Get a user | |
POST |
/auth/login |
Log in and get an access token | |
GET |
/posts/ |
List your posts with vote counts | 🔒 |
GET |
/posts/{id} |
Get one post with its vote count | 🔒 |
POST |
/posts/ |
Create a post | 🔒 |
PUT |
/posts/{id} |
Update your post | 🔒 |
DELETE |
/posts/{id} |
Delete your post | 🔒 |
POST |
/vote/ |
Vote (dir: 1) or remove your vote (dir: 0) |
🔒 |
🔒 = needs the header Authorization: Bearer <token>
DATABASE_HOSTNAME=postgres
DATABASE_PORT=5432
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=postgres123
DATABASE_NAME=fastapi
SECRET_KEY=change-me-to-a-long-random-string
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30Use DATABASE_HOSTNAME=localhost instead if you run the API without Docker.
docker compose up --buildYou need a running PostgreSQL database first.
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
alembic upgrade head
uvicorn app.main:app --reloadOpen http://localhost:8000/docs to see every endpoint and test it in the browser.
app/
├── main.py # App setup, CORS, routers
├── config.py # Settings loaded from .env
├── database.py # Database connection
├── models.py # SQLAlchemy tables: User, Post, Vote
├── schemas.py # Pydantic request/response models
├── oauth2.py # JWT creation and checking
├── utils.py # Password hashing
└── routers/ # auth, user, post, vote
alembic/ # Database migrations