A high-quality FastAPI CRUD application with SQLite database, featuring a single table (users) with comprehensive CRUD operations.
- FastAPI: Modern, fast web framework for building APIs
- SQLAlchemy: SQL toolkit and ORM
- SQLite: Lightweight database
- Pydantic: Data validation using Python type annotations
- CRUD Operations: Create, Read, Update, Delete
- Pagination: Efficient data retrieval with pagination
- Search: Search functionality across multiple fields
- Error Handling: Comprehensive error handling with proper HTTP status codes
- API Documentation: Auto-generated OpenAPI/Swagger documentation
- Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txtStart the FastAPI server:
python main.pyThe application will be available at http://localhost:8000
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
| Method | Endpoint | Description |
|---|---|---|
| POST | /users/ |
Create a new user |
| GET | /users/ |
Get users with pagination and search |
| GET | /users/{user_id} |
Get a specific user |
| PUT | /users/{user_id} |
Update a user |
| DELETE | /users/{user_id} |
Delete a user |
| Method | Endpoint | Description |
|---|---|---|
| GET | / |
Root endpoint |
| GET | /health |
Health check |
{
"id": "integer",
"email": "string (unique)",
"username": "string (unique)",
"full_name": "string (optional)",
"is_active": "boolean",
"created_at": "datetime",
"updated_at": "datetime (optional)"
}curl -X POST "http://localhost:8000/users/" \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"username": "johndoe",
"full_name": "John Doe",
"is_active": true
}'curl "http://localhost:8000/users/?page=1&size=10"curl "http://localhost:8000/users/?search=john"curl -X PUT "http://localhost:8000/users/1" \
-H "Content-Type: application/json" \
-d '{
"full_name": "John Smith"
}'curl -X DELETE "http://localhost:8000/users/1"fastapi-boilerplate/
├── main.py # FastAPI application and endpoints
├── models.py # SQLAlchemy models
├── schemas.py # Pydantic schemas
├── crud.py # CRUD operations
├── database.py # Database configuration
├── requirements.txt # Python dependencies
└── README.md # This file
This project follows high code quality standards:
- Type Hints: Full type annotation coverage
- Error Handling: Comprehensive error handling with proper HTTP status codes
- Validation: Input validation using Pydantic schemas
- Separation of Concerns: Clear separation between models, schemas, CRUD operations, and API endpoints
- Documentation: Comprehensive docstrings and API documentation
- Security: Input validation and SQL injection prevention through ORM
- Performance: Efficient database queries with pagination
- Testing Ready: Structure designed for easy unit testing
The application uses SQLite with the following table structure:
CREATE TABLE users (
id INTEGER PRIMARY KEY,
email VARCHAR UNIQUE NOT NULL,
username VARCHAR UNIQUE NOT NULL,
full_name VARCHAR,
is_active BOOLEAN DEFAULT 1 NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME
);The database file (test.db) will be created automatically when you run the application.