This project provides a simple structure for creating a FastAPI-based web application for managing books, along with Alembic for database migrations. It is designed with beginners in mind and follows a structured folder organization.
- Python 3.9
- Deta account for deployment (optional but recommended)
-
Clone this repository:
git clone https://github.com/yudhaislamisulistya/book-api-fastapi.git cd book-api-fastapi
-
Install the project dependencies:
pip install -r requirements.txt
-
Run command for migration database:
alembic revision --autogenerate or alembic upgrade head
-
Run the application locally using the following command:
uvicorn main:app --reload
Or, if you want to use Deta for deployment:
-
Create an account on Deta.
-
Install Deta CLI:
curl -fsSL https://get.deta.dev/space-cli.sh | sh
-
Initialize a Deta project and deploy:
space new space push
-
or Alternative with Space CLI
space dev
-
Obtain the token and project ID from Deta and update them in
deploy.yml
for CI/CD setup.
-
-
Access the application at the following URL (if using Deta):
https://bookapi-1-v1905306.deta.app/ or http://localhost:8080/
-
Access the API documentation at the following URL:
https://bookapi-1-v1905306.deta.app/docs or http://localhost:8080/docs
Here are some example endpoints that you can use:
GET /api/v1/books/
Retrieves a list of all books.
POST /api/v1/books/
Creates a new book.
DELETE /api/v1/books/{book_id}
Deletes a book by its ID.
BookInput
- Schema for creating a book.BookOutput
- Schema for the book response.
The project follows this folder structure:
.github/workflows
- GitHub Actions workflowsalembic
- Alembic database migration scriptsapp
- Main application codeconfigs
- Database configurationcontrollers
- Controllers for business logicendpoints
- API endpointslibs
- Utility libraries (databases and templates)model
- Database table structuresroutes
- API routesschemas
- Pydantic request and response schemastests
- Unit testsutils
- Additional utility libraries
Spacefile
- Deta configurationmain.py
- FastAPI application entry pointrequirements.txt
- Project dependencies
Feel free to contribute to this project by submitting pull requests. Your contributions can help improve and expand this beginner-friendly FastAPI and Alembic structure.
This project is licensed under the MIT License - see the LICENSE file for details.