A RESTful Library Management API built using FastAPI and MongoDB. The system supports managing authors, books, users, and a borrowing system with loan tracking.
- Author CRUD operations
- Book CRUD operations with author reference
- User registration and login
- JWT authentication
- Borrow and return books
- Loan tracking system
- Role-based access (admin / user)
- Pagination support for listing resources
- Validation to prevent borrowing unavailable books
- MongoDB integration using Motor (async driver)
- FastAPI automatic documentation (Swagger)
library-management-api
│
├── main.py
├── database.py
├── auth.py
├── schemas.py
├── models.py
│
├── routers
│ ├── authors.py
│ ├── books.py
│ ├── users.py
│ └── loans.py
│
├── tests
│ ├── test_api.py
│ └── test_books.py
│
├── requirements.txt
├── README.md
└── .gitignore
- Python 3.10+
- MongoDB
- FastAPI
- Uvicorn
- Motor (MongoDB async driver)
Clone the repository:
git clone https://github.com/arunkmr13/library-management-api.git
Navigate to the project folder:
cd library-management-api
Create a virtual environment:
python -m venv venv
Activate the environment:
Mac/Linux
source venv/bin/activate
Windows
venv\Scripts\activate
Install dependencies:
pip install -r requirements.txt
Start the FastAPI server:
uvicorn main:app --reload
The API will run at:
http://127.0.0.1:8000
FastAPI automatically generates interactive API documentation.
Swagger UI
http://127.0.0.1:8000/docs
ReDoc
http://127.0.0.1:8000/redoc
These interfaces allow you to test all endpoints directly from the browser.
| Method | Endpoint | Description |
|---|---|---|
| POST | /authors | Create a new author |
| GET | /authors | List all authors |
| GET | /authors/{id} | Get author by ID |
| PUT | /authors/{id} | Update author |
| DELETE | /authors/{id} | Delete author |
| Method | Endpoint | Description |
|---|---|---|
| POST | /books | Create a book |
| GET | /books | List books (with pagination) |
| GET | /books/{id} | Get book by ID |
| PUT | /books/{id} | Update book |
| DELETE | /books/{id} | Delete book |
| Method | Endpoint | Description |
|---|---|---|
| POST | /users/register | Register a user |
| POST | /users/login | Login and receive JWT token |
| Method | Endpoint | Description |
|---|---|---|
| POST | /loans/borrow/{book_id} | Borrow a book |
| PUT | /loans/return/{loan_id} | Return a book |
| GET | /loans | List loans (admin sees all, user sees own loans) |
-
A user cannot borrow a book that is already borrowed.
-
When a book is borrowed:
- A loan record is created.
- The book availability is set to false.
-
When a book is returned:
- The loan record is updated with the return date.
- The book availability becomes true.
You can test the API using:
- Swagger UI
Recommended testing order:
- Register a user
- Login to obtain JWT token
- Create authors
- Create books
- Borrow a book
- Return a book
- Check loan records
Protected endpoints require a JWT token.
Arun Kumar
GitHub Repository https://github.com/arunkmr13/library-management-api