This project demonstrates a sophisticated 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.
- Introduction
- Features
- Installation
- Usage
- Project Structure
- API Endpoints
- Examples
- Contributing
- License
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.
- JWT Authentication: Secure API endpoints with JSON Web Tokens for authentication.
- CRUD Operations: Full Create, Read, Update, Delete operations for books and authors.
- Database Integration: Persistent storage using SQLite with SQLAlchemy ORM.
- Async Endpoints: Asynchronous endpoints for improved performance.
- Pagination: Efficient handling of large datasets with pagination.
- Error Handling: Graceful error handling with meaningful HTTP status codes and messages.
- Modular Structure: Organized codebase with multiple files for scalability and maintainability.
- Automated Database Seeding: Use of Faker library to generate and seed the database with fake data.
- Python 3.7 or higher
-
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:
- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc
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
- Endpoint:
POST /users/
- Description: Register a new user.
- Request Body:
{ "username": "user1", "password": "yourpassword" }
- Response: User details (excluding password).
- Endpoint:
POST /token
- Description: Authenticate user and receive a JWT token.
- Request Body: Form data (
username
,password
). - Response: JWT token.
- Endpoint:
POST /books/
- Description: Add a new book (requires authentication).
- Request Body:
{ "title": "The Great Gatsby", "description": "A novel by F. Scott Fitzgerald", "author_id": 1 }
- Response: Book details.
- Endpoint:
GET /books/
- Description: Retrieve all books with pagination.
- Query Parameters:
skip
(default: 0),limit
(default: 10). - Response: List of books.
- Endpoint:
POST /authors/
- Description: Add a new author (requires authentication).
- Request Body:
{ "name": "F. Scott Fitzgerald" }
- Response: Author details.
- Endpoint:
GET /authors/
- Description: Retrieve all authors with pagination.
- Query Parameters:
skip
(default: 0),limit
(default: 10). - Response: List of authors.
-
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.
- Fork the Repository: Click the 'Fork' button at the top right of this page.
- Clone Your Fork: Clone your forked repository to your local machine.
git clone https://github.com/your-username/bookstore-api-fastapi.git
- Create a Branch: Create a new branch for your feature or bug fix.
git checkout -b feature/your-feature-name
- Make Changes: Make your changes and commit them with a descriptive message.
git commit -m "Add: feature description"
- Push Changes: Push your changes to your forked repository.
git push origin feature/your-feature-name
- Create a Pull Request: Go to the original repository on GitHub and create a pull request.
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! 📚🚀