A modern REST API for managing a bookstore with authors, books, orders, and user authentication built with FastAPI and SQLAlchemy.
- FastAPI - Modern web framework
- SQLAlchemy 2.0 - Async ORM
- PostgreSQL - Primary database
- Alembic - Database migrations
- Pydantic - Data validation
- JWT - Token-based authentication
- pytest - Testing framework
- Python 3.11+
- PostgreSQL
- pip or poetry
- Clone the repository
git clone https://github.com/drizzy1772/libraryAPI-api.git
cd bookstore-api- Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies
pip install -r requirements.txt- Setup environment variables
cp .env.example .envEdit .env with your configuration:
DATABASE_URL=postgresql+asyncpg://postgres:postgres@localhost:5432/bookstore
SECRET_KEY=your-secret-key-here
DEBUG=False
ACCESS_TOKEN_EXPIRE_MINUTES=40- Initialize database
# Run migrations
alembic upgrade head- Start the server
uvicorn app.main:app --reloadOnce the server is running, visit:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
POST /auth/register
{
"username": "john_doe",
"email": "john@example.com",
"password": "secure_password"
}POST /auth/login
{
"username": "john_doe",
"password": "secure_password"
}Response:
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"token_type": "bearer"
}Use the token in subsequent requests:
Authorization: Bearer <your_token>| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| GET | /authors |
List all authors | No |
| GET | /authors/{id} |
Get author details with books | No |
| POST | /authors |
Create new author | Admin only |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| GET | /books |
List all books (paginated) | No |
| GET | /books/{id} |
Get book details | No |
| POST | /books |
Create new book | Admin only |
| PATCH | /books/{id} |
Update book | Admin only |
| DELETE | /books/{id} |
Delete book | Admin only |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /orders |
Place an order | User |
Run the test suite:
pytestRun with coverage:
pytest --cov=app tests/Run specific test file:
pytest tests/test_books.pybookstore-api/
├── alembic/ # Database migrations
│ ├── versions/ # Migration files
│ └── env.py
├── app/
│ ├── routers/ # API routes
│ │ ├── auth.py
│ │ ├── authors.py
│ │ ├── books.py
│ │ └── orders.py
│ ├── config.py # Configuration settings
│ ├── crud.py # Database operations
│ ├── database.py # Database connection
│ ├── dependencies.py # FastAPI dependencies
│ ├── main.py # Application entry point
│ ├── models.py # SQLAlchemy models
│ ├── schemas.py # Pydantic schemas
│ └── security.py # Authentication utilities
├── tests/ # Test suite
│ ├── conftest.py
│ ├── test_auth.py
│ ├── test_authors.py
│ ├── test_books.py
│ └── test_orders.py
├── .env.example
├── alembic.ini
├── requirements.txt
└── README.md
Key settings in app/config.py:
DATABASE_URL- PostgreSQL connection stringSECRET_KEY- JWT signing keyACCESS_TOKEN_EXPIRE_MINUTES- Token expiration timeDEFAULT_PAGE_SIZE- Default pagination sizeMAX_PAGE_SIZE- Maximum items per page