This project demonstrates a simple but comprehensive backend API for a Bookstore application, built using FastAPI, a modern, fast (high-performance) web framework for building APIs with Python. The API provides features for managing books and authors, including full CRUD operations, JWT-based authentication, database integration using SQLAlchemy, and pagination. It also includes automated seeding of the database with fake data for testing purposes.
The Bookstore API provides a secure and scalable backend service for managing books and authors in a bookstore. It demonstrates key backend development concepts using FastAPI, such as asynchronous endpoints, JWT authentication, and database integration with SQLAlchemy. The API is designed to be easily extended and deployed, making it a great starting point for more complex applications.
Clone the Repository:
Clone the repository to your local machine:
git clone https://github.com/your-username/bookstore-api-fastapi.git
Navigate to the Directory:
Go to the project directory:
cd bookstore-api-fastapi
Install Dependencies:
Install the required packages using pip:
pip install -r requirements.txt
Run the Application:
Start the FastAPI application using Uvicorn:
uvicorn main:app --reload
The server will start at http://127.0.0.1:8000/
.
Access API Documentation:
FastAPI provides interactive API documentation at:
bookstore/
│
├── main.py # Entry point of the application
├── models.py # SQLAlchemy models representing the database schema
├── schemas.py # Pydantic models for request and response validation
├── crud.py # CRUD operations for interacting with the database
├── auth.py # Authentication logic (JWT token creation and verification)
├── database.py # Database setup and session management
├── config.py # Configuration settings (e.g., secrets, JWT settings)
├── seed_data.py # Script for seeding the database with fake data
└── requirements.txt # List of required Python packages
POST /users/
{
"username": "user1",
"password": "yourpassword"
}
POST /token
username
, password
).POST /books/
{
"title": "The Great Gatsby",
"description": "A novel by F. Scott Fitzgerald",
"author_id": 1
}
GET /books/
skip
(default: 0), limit
(default: 10).POST /authors/
{
"name": "F. Scott Fitzgerald"
}
GET /authors/
skip
(default: 0), limit
(default: 10).Register a New User:
curl -X POST -H "Content-Type: application/json" -d '{"username": "user1", "password": "yourpassword"}' http://127.0.0.1:8000/users/
Authenticate and Get Token:
curl -X POST -d "username=user1&password=yourpassword" -H "Content-Type: application/x-www-form-urlencoded" http://127.0.0.1:8000/token
Create a New Book:
curl -X POST -H "Authorization: Bearer <your-token>" -H "Content-Type: application/json" -d '{"title": "The Great Gatsby", "description": "A novel by F. Scott Fitzgerald", "author_id": 1}' http://127.0.0.1:8000/books/
Get All Books:
curl -X GET http://127.0.0.1:8000/books/
Contributions are welcome! If you have ideas for new features, improvements, or bug fixes, please feel free to open an issue or create a pull request.
git clone https://github.com/your-username/bookstore-api-fastapi.git
git checkout -b feature/your-feature-name
git commit -m "Add: feature description"
git push origin feature/your-feature-name
This project is licensed under the MIT License - see the LICENSE file for details.
Thank you for using the Bookstore API Backend with FastAPI! If you have any questions or feedback, feel free to reach out to me. Happy coding! 📚🚀