A high-performance RESTful API built with FastAPI for managing a book inventory. This project demonstrates full CRUD (Create, Read, Update, Delete) operations, database integration using SQLAlchemy ORM, and data validation with Pydantic.
- FastAPI: Modern, fast (high-performance) web framework for building APIs.
- Python: The core programming language.
- SQLAlchemy: Python SQL Toolkit and Object-Relational Mapper (ORM).
- MySQL: Persistent SQL database for data storage.
- Pydantic: Used for data validation and schema definition.
- CRUD Operations: Fully functional endpoints for adding, viewing, updating, and deleting books.
- Database Session Management: Utilizes FastAPI's Dependency Injection (
get_db) for efficient and safe database connections. - Automatic Documentation: API documentation (Swagger UI) is automatically generated at
/docs. - Data Models: Clear separation of Pydantic schemas (
schemas.py) and SQLAlchemy ORM models (model.py).
| Method | Endpoint | Description | Pydantic Schema |
|---|---|---|---|
GET |
/ |
Root endpoint confirmation. | - |
GET |
/books |
Retrieve a list of all books. | model.book |
GET |
/book/{book_id} |
Retrieve a specific book by ID. | model.book |
POST |
/book |
Create a new book entry. | schemas.add_book |
PUT |
/book/{book_id} |
Update an existing book's details. | schemas.update_book |
DELETE |
/book/{book_id} |
Delete a book by ID. | - |
Follow these steps to set up and run the FastAPI Book Manager API locally.
You need an instance of a MySQL server running to connect to.
- Create Database: Create a MySQL database named
book_manager. - Create User: Create a dedicated MySQL user (e.g.,
bookuser) with a strong password and grant it permissions on thebook_managerdatabase. - Update Connection String: Edit the
database_urlindatabase.pyto match your credentials:# database.py database_url = "mysql+mysqlconnector://bookuser:Roshaan_DB2024!@localhost:3306/book_manager"
It is highly recommended to use a virtual environment (venv) to isolate project dependencies.
-
Create a Virtual Environment:
python -m venv venv
-
Activate the Environment:
- Windows (Command Prompt/Git Bash):
./venv/Scripts/activate
- (Linux/macOS):
source venv/bin/activate
(Your terminal prompt should now show
(venv)to indicate activation.) - Windows (Command Prompt/Git Bash):
-
Install Dependencies: Install all necessary libraries and their exact versions using the provided
requirements.txtfile:pip install -r requirements.txt
-
Start the Uvicorn Server:
uvicorn main:app --reload
-
The API will be running at
http://127.0.0.1:8000. You can test all endpoints and view the documentation using the interactive Swagger UI athttp://127.0.0.1:8000/docs.