A simple RESTful API for a blogging platform built with FastAPI. It provides authentication, post management, and token-based security.
- JWT authentication with access & refresh tokens
- Token versioning for secure logout
- Dockerized environment for easy setup
- Clean modular architecture (routers, services, dependencies)
- Database migrations with Alembic
- FastAPI
- SQLAlchemy
- PostgreSQL
- Alembic
- JWT (PyJWT)
- uv
- Docker installed and running
git clone https://github.com/yusufKh7-ctrl/Blog-API.git
cd Blog_APIDATABASE_URL=postgresql://postgres:your_password@db:5432/BlogAPI
SECRET_KEY=your_secret_key_here
POSTGRES_PASSWORD=your_passworddocker compose up --buildThat's it! The API will be available at:
http://localhost:8000
And you can explore the interactive docs at:
http://localhost:8000/docs
docker compose downIf you want to also delete the database data:
docker compose down -v
POST /api/users → Register a new account
POST /api/users/token → Login (returns access + refresh token)
POST /api/users/token/refresh → Get a new access token
POST /api/users/logout → Logout 🔒
GET /api/posts → List all posts
GET /api/posts/{post_id} → Get a specific post
GET /api/posts/user/{user_id} → Get all posts by a user
POST /api/posts → Create a new post 🔒
PUT /api/posts/{post_id} → Update a post (full) 🔒
PATCH /api/posts/{post_id} → Update a post (partial) 🔒
DELETE /api/posts/{post_id} → Delete a post 🔒
🔒 = requires login (Bearer token)
This API uses JWT tokens with two token types:
- Access token — short-lived (30 minutes), used for protected endpoints
- Refresh token — long-lived (7 days), used to get a new access token without logging in again
To use protected endpoints, add this header to your request:
Authorization: Bearer <your_access_token>
Blog_API/
├── alembic/ # Database migrations
├── core/ # Config, security, rate limiter
├── dependencies/ # Auth dependencies
├── models/ # Database models
├── routers/ # API endpoints (users, posts)
├── schemas/ # Pydantic schemas
├── tests/ # Tests
├── main.py # App entry point
├── database.py # DB session setup
├── docker-compose.yml
├── Dockerfile
└── pyproject.toml
docker compose exec api uv run pytestBuilt by Yusuf Khlif