A production-ready REST API for managing library books, members, and loans built with FastAPI and PostgreSQL.
- Book Management: Create, read, update, delete books
- Member Management: Register members, manage profiles
- Loan System: Borrow and return books with business logic validation
- Search: Search books by title, author, or ISBN
- Pagination: All list endpoints support pagination
- REST API: Full REST API with proper HTTP status codes
- API Documentation: Interactive Swagger documentation
library-api/
├── app/
│ ├── __init__.py
│ ├── main.py # FastAPI app entry point
│ ├── database.py # SQLAlchemy setup
│ ├── models.py # ORM models
│ ├── schemas/ # Pydantic schemas
│ │ ├── __init__.py
│ │ ├── book.py
│ │ ├── member.py
│ │ └── loan.py
│ ├── routers/ # API endpoints
│ │ ├── __init__.py
│ │ ├── books.py
│ │ ├── members.py
│ │ └── loans.py
│ ├── repositories/ # Data access layer
│ │ ├── __init__.py
│ │ ├── book_repository.py
│ │ ├── member_repository.py
│ │ └── loan_repository.py
│ └── services/ # Business logic layer
│ ├── __init__.py
│ └── loan_service.py
├── requirements.txt # Python dependencies
├── .env.example # Environment variables template
└── README.md # This file
This project follows Layered Architecture pattern:
| Layer | Responsibility |
|---|---|
| Router | Receives HTTP requests, validates with Pydantic, calls service |
| Service | Contains business rules (max 3 loans, book availability, member validation) |
| Repository | Executes SQL queries against PostgreSQL, returns data objects |
| Database | Stores actual data in PostgreSQL |
HTTP Request
↓
Router (books.py) - validates input
↓
LoanService - enforces business rules
↓
LoanRepository - executes SQL queries
↓
PostgreSQL Database
- Python 3.9+
- PostgreSQL 12+
- pip
git clone <repository-url>
cd library-apipython -m venv venvOn Windows:
venv\Scripts\activateOn macOS/Linux:
source venv/bin/activatepip install -r requirements.txtcp .env.example .envEdit .env and update PostgreSQL connection string:
DATABASE_URL=postgresql://username:password@localhost:5432/library_db
CREATE DATABASE library_db;python -m uvicorn app.main:app --reloadThe API will be available at http://localhost:8000
- API Documentation:
http://localhost:8000/api/docs - ReDoc:
http://localhost:8000/api/redoc
POST /books- Create a new bookGET /books- List all booksGET /books/search?q=<query>- Search booksGET /books/{book_id}- Get book detailsPUT /books/{book_id}- Update a bookDELETE /books/{book_id}- Delete a book
POST /members- Register new memberGET /members- List all membersGET /members/search?q=<query>- Search membersGET /members/{member_id}- Get member detailsPUT /members/{member_id}- Update memberDELETE /members/{member_id}- Deactivate member
POST /loans/borrow- Borrow a bookPOST /loans/{loan_id}/return- Return a bookGET /loans- List all loansGET /loans/member/{member_id}- Get member's active loansGET /loans/member/{member_id}/history- Get member's loan historyGET /loans/overdue- Get overdue loans
- Member must exist and be active
- Book must exist and have available copies
- Member cannot have more than 3 active loans
- Member cannot borrow the same book twice while it's unreturned
- Loan must exist and be active
- Book availability is restored upon return
- Fine calculation (optional): $1 per day overdue
curl -X POST "http://localhost:8000/books" \
-H "Content-Type: application/json" \
-d '{
"isbn": "978-0-596-00712-6",
"title": "Learning Python",
"author": "Mark Lutz",
"description": "A comprehensive guide to Python programming",
"total_copies": 3
}'curl -X POST "http://localhost:8000/members" \
-H "Content-Type: application/json" \
-d '{
"name": "Taher",
"email": "taherch2025@gmail.com",
"phone": "+1-555-0123",
"address": "123 Main St, Anytown, USA"
}'curl -X POST "http://localhost:8000/loans/borrow" \
-H "Content-Type: application/json" \
-d '{
"book_id": 1,
"member_id": 1,
"loan_duration_days": 14
}'curl -X POST "http://localhost:8000/loans/1/return"- FastAPI: Modern async web framework
- SQLAlchemy: ORM for database operations
- PostgreSQL: Relational database
- Pydantic: Data validation
- Uvicorn: ASGI server
pytestalembic upgrade head- User authentication and authorization
- Database migrations with Alembic
- Unit and integration tests
- Docker and Docker Compose setup
- Logging and monitoring
- Rate limiting
- Book reviews and ratings
- Reservation system
- Fine management system
- Email notifications
Created for learning purpose as part of a project-based learning curriculum.