FastAPI REST API
A modern, fast REST API built with FastAPI and SQLite.
Features
- FastAPI framework with all its features
- SQLAlchemy ORM
- SQLite database
- Alembic migrations
- Pydantic data validation
- Automatic API documentation with Swagger UI and ReDoc
- CORS middleware
- Health check endpoint
Project Structure
.
├── alembic.ini # Alembic configuration
├── app # Main application package
│ ├── api # API endpoints
│ │ └── v1 # API version 1
│ │ ├── api.py # API router
│ │ └── endpoints # API endpoint modules
│ │ └── items.py # Items API endpoints
│ ├── core # Core modules
│ │ └── config.py # Application settings
│ ├── db # Database modules
│ │ └── database.py # Database connection
│ ├── models # SQLAlchemy models
│ │ ├── base.py # Base model
│ │ └── item.py # Item model
│ ├── schemas # Pydantic schemas
│ │ └── item.py # Item schemas
│ └── services # Business logic
├── main.py # Application entry point
├── migrations # Alembic migrations
│ ├── env.py # Alembic environment
│ ├── README # Alembic README
│ ├── script.py.mako # Migration script template
│ └── versions # Migration versions
│ └── 01_initial_migration.py # Initial migration
└── requirements.txt # Project dependencies
Installation
- Clone the repository:
git clone https://your-repository-url.git
cd fastapi-rest-api
- Install dependencies:
pip install -r requirements.txt
- Set up the database:
# The database will be automatically created at the configured location
# Make sure the directory exists
mkdir -p /app/storage/db
- Run the migrations:
alembic upgrade head
Running the API
Start the server with:
uvicorn main:app --reload
The API will be available at http://localhost:8000.
API Documentation
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
API Endpoints
GET /api/v1/items
: Get a list of itemsPOST /api/v1/items
: Create a new itemGET /api/v1/items/{item_id}
: Get an item by IDPUT /api/v1/items/{item_id}
: Update an itemDELETE /api/v1/items/{item_id}
: Delete an itemGET /health
: Check API health
Usage Examples
Get list of items
curl -X 'GET' 'http://localhost:8000/api/v1/items' -H 'accept: application/json'
Create a new item
curl -X 'POST' \
'http://localhost:8000/api/v1/items' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"name": "Example Item",
"description": "This is an example item",
"is_active": true
}'
Get an item by ID
curl -X 'GET' 'http://localhost:8000/api/v1/items/1' -H 'accept: application/json'
Update an item
curl -X 'PUT' \
'http://localhost:8000/api/v1/items/1' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"name": "Updated Item",
"description": "This item has been updated",
"is_active": true
}'
Delete an item
curl -X 'DELETE' 'http://localhost:8000/api/v1/items/1' -H 'accept: application/json'
Check API health
curl -X 'GET' 'http://localhost:8000/health' -H 'accept: application/json'
Description
Languages
Python
95.2%
Mako
4.8%