Simple REST API

A simple REST API built with FastAPI and SQLite.

Features

  • FastAPI with automatic OpenAPI documentation
  • SQLite database with SQLAlchemy ORM
  • Alembic migrations
  • CRUD operations for items
  • Search functionality with multiple filters
  • Health check endpoint
  • Input validation and error handling
  • Pagination and filtering

Project Structure

.
├── alembic.ini             # Alembic configuration
├── app                     # Application package
│   ├── api                 # API endpoints
│   │   ├── endpoints       # API route handlers
│   │   └── router.py       # API router
│   ├── core                # Core application code
│   │   └── config.py       # Application settings
│   ├── crud                # Database CRUD operations
│   ├── database            # Database configuration
│   │   ├── crud_base.py    # Base CRUD class
│   │   └── session.py      # Database session
│   ├── models              # SQLAlchemy models
│   │   └── item.py         # Item model
│   ├── schemas             # Pydantic schemas
│   │   └── item.py         # Item schemas
│   └── utils               # Utility functions
├── main.py                 # Application entry point
├── migrations              # Alembic migrations
│   ├── env.py              # Alembic environment
│   ├── README              # Migrations README
│   ├── script.py.mako      # Migration script template
│   └── versions            # Migration versions
│       └── *.py            # Migration scripts
└── requirements.txt        # Project dependencies

Getting Started

Prerequisites

  • Python 3.8 or higher
  • SQLite (included with Python)

Installation

  1. Clone the repository:
git clone https://github.com/yourusername/simple-rest-api.git
cd simple-rest-api
  1. Create a virtual environment:
python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Apply database migrations:
alembic upgrade head

Running the API

Start the application with:

uvicorn main:app --reload

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

API Endpoints

Health Check

  • GET /api/v1/health - Check if the API is running

Items

  • GET /api/v1/itemsss - List all items (with pagination, filtering, and search)

    • Query Parameters:
      • filter - Filter text to match in name and description
      • active - Filter by active status (true/false)
      • price_min - Filter by minimum price (in cents)
      • price_max - Filter by maximum price (in cents)
      • skip - Number of records to skip (for pagination)
      • limit - Maximum number of records to return (for pagination)
  • GET /api/v1/itemsss/search - Search for items by name or description

    • Query Parameters:
      • filter - Filter text to match in name and description (required)
      • active - Filter by active status (true/false)
      • price_min - Filter by minimum price (in cents)
      • price_max - Filter by maximum price (in cents)
      • skip - Number of records to skip (for pagination)
      • limit - Maximum number of records to return (for pagination)
  • GET /api/v1/itemsss/{item_id} - Get a specific item

  • POST /api/v1/itemsss - Create a new item

  • PUT /api/v1/itemsss/{item_id} - Update an item

  • DELETE /api/v1/itemsss/{item_id} - Delete an item

Search Functionality

The API supports searching items by:

  1. Text matching in name and description fields (case-insensitive)
  2. Filtering by active status
  3. Price range filtering (minimum and maximum price)
  4. Combination of all the above filters

Example search request:

GET /api/v1/itemsss/search?filter=phone&active=true&price_min=10000&price_max=50000

This will find all active items that contain "phone" in their name or description, with a price between 100.00 and 500.00 (prices are stored in cents).

Environment Variables

You can configure the application using environment variables:

  • PROJECT_NAME - Application name (default: "SimpleRestAPI")
  • BACKEND_CORS_ORIGINS - CORS origins, comma-separated (default: empty)

Development

Adding a New Migration

alembic revision --autogenerate -m "description of changes"

Applying Migrations

alembic upgrade head

Code Linting

ruff .

Code Auto-Formatting

ruff . --fix

License

This project is licensed under the MIT License.

Description
Project: Simple REST API
Readme 53 KiB
Languages
Python 97.3%
Mako 2.7%