3.2 KiB

Simple Todo App API

A simple Todo API built with FastAPI and SQLite.

Features

  • Create, Read, Update, Delete (CRUD) operations for todo items
  • Filter todos by completion status
  • RESTful API with FastAPI
  • SQLite database with SQLAlchemy ORM
  • Alembic for database migrations
  • API documentation with Swagger UI and ReDoc

Project Structure

.
├── alembic/               # Database migration files
│   ├── versions/          # Migration versions
│   ├── env.py             # Alembic environment configuration
│   └── script.py.mako     # Migration script template
├── app/                   # Application code
│   ├── api/               # API endpoints
│   │   └── v1/            # API version 1
│   │       └── todos.py   # Todo endpoints
│   ├── core/              # Core application code
│   │   └── config.py      # Application configuration
│   ├── crud/              # CRUD operations
│   │   ├── base.py        # Base CRUD operations
│   │   └── todo.py        # Todo specific CRUD operations
│   ├── db/                # Database setup
│   │   ├── base.py        # Import all models
│   │   ├── base_class.py  # Base class for models
│   │   └── session.py     # Database session
│   ├── models/            # SQLAlchemy models
│   │   └── todo.py        # Todo model
│   └── schemas/           # Pydantic schemas
│       └── todo.py        # Todo schemas
├── alembic.ini            # Alembic configuration
├── main.py                # Application entry point
└── requirements.txt       # Python dependencies

Getting Started

Prerequisites

  • Python 3.7 or higher

Installation

  1. Clone the repository

  2. Install the dependencies:

    pip install -r requirements.txt
    
  3. Initialize the database directory:

    python init_db.py
    
  4. Apply the database migrations:

    alembic upgrade head
    
  5. Run the application:

    uvicorn main:app --reload
    

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

API Documentation

API Endpoints

Todos

  • GET /api/v1/todos - Get all todos
  • GET /api/v1/todos?is_completed=true|false - Get todos filtered by completion status
  • GET /api/v1/todos/{id} - Get a todo by id
  • POST /api/v1/todos - Create a new todo
  • PUT /api/v1/todos/{id} - Update a todo
  • DELETE /api/v1/todos/{id} - Delete a todo

Health Check

  • GET /health - Check if the API is running

Data Models

Todo

  • id: Integer (Primary Key)
  • title: String (Required)
  • description: Text (Optional)
  • is_completed: Boolean (Default: false)
  • created_at: DateTime (Auto-generated)
  • updated_at: DateTime (Auto-updated)

Example

Create a Todo

curl -X 'POST' \
  'http://localhost:8000/api/v1/todos/' \
  -H 'Content-Type: application/json' \
  -d '{
  "title": "Learn FastAPI",
  "description": "Build a simple todo app using FastAPI",
  "is_completed": false
}'

Get All Todos

curl -X 'GET' 'http://localhost:8000/api/v1/todos/'