Skip to content

a4amaan/fastapi-boilerplate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FastAPI CRUD Application

A high-quality FastAPI CRUD application with SQLite database, featuring a single table (users) with comprehensive CRUD operations.

Features

  • 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

Installation

  1. Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt

Running the Application

Start the FastAPI server:

python main.py

The application will be available at http://localhost:8000

API Documentation

  • Swagger UI: http://localhost:8000/docs
  • ReDoc: http://localhost:8000/redoc

API Endpoints

Users

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

Other

Method Endpoint Description
GET / Root endpoint
GET /health Health check

User Model

{
  "id": "integer",
  "email": "string (unique)",
  "username": "string (unique)",
  "full_name": "string (optional)",
  "is_active": "boolean",
  "created_at": "datetime",
  "updated_at": "datetime (optional)"
}

Example Usage

Create a User

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
  }'

Get Users with Pagination

curl "http://localhost:8000/users/?page=1&size=10"

Search Users

curl "http://localhost:8000/users/?search=john"

Update a User

curl -X PUT "http://localhost:8000/users/1" \
  -H "Content-Type: application/json" \
  -d '{
    "full_name": "John Smith"
  }'

Delete a User

curl -X DELETE "http://localhost:8000/users/1"

Project Structure

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

Code Quality Standards

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

Database

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages