A FastAPI backend project that combines a Todo CRUD API with an LLM-powered task extraction assistant.
Users can create todos manually, or describe tasks in natural language and let the AI extract tasks, due times, and save them into the SQLite database.
- FastAPI backend
- Todo CRUD API
- SQLite persistence with SQLModel
- Request and response validation with Pydantic
- HTTP status codes and error handling
- Swagger UI API documentation
- Docker support
- Docker volume support for persistent SQLite data
- Bailian / DashScope LLM integration
- AI chat endpoint
- Natural language task extraction
- AI-generated todos with due time extraction
- Python
- FastAPI
- Pydantic
- SQLModel
- SQLite
- Uvicorn
- Docker
- OpenAI-compatible SDK
- Alibaba Cloud Bailian / DashScope
fastapi-todo-api/
├── data/
│ └── todos.db
├── .dockerignore
├── .env
├── .env.example
├── .gitignore
├── Dockerfile
├── main.py
├── README.md
└── requirements.txt
Create a .env file in the project root:
DASHSCOPE_API_KEY=your_dashscope_api_key_here
DASHSCOPE_BASE_URL=https://dashscope.aliyuncs.com/compatible-mode/v1
DASHSCOPE_MODEL=qwen3.5-plusDo not commit .env to GitHub.
Use .env.example as the public template.
Install dependencies:
pip install -r requirements.txtStart the server:
uvicorn main:app --reloadOpen API docs:
http://127.0.0.1:8000/docs
Build the Docker image:
docker build -t fastapi-todo-api .Run the container:
docker run -p 8000:8000 fastapi-todo-apiOpen API docs:
http://127.0.0.1:8000/docs
If port 8000 is already in use:
docker run -p 8001:8000 fastapi-todo-apiThen open:
http://127.0.0.1:8001/docs
Create a local data directory:
mkdir dataBuild the image:
docker build -t fastapi-todo-api .Run with a volume:
docker run -p 8001:8000 -v "${PWD}/data:/app/data" fastapi-todo-apiFor Windows PowerShell:
$projectPath = (Get-Location).Path
docker run -p 8001:8000 -v "${projectPath}\data:/app/data" fastapi-todo-apiThe SQLite database will be stored in:
data/todos.db
| Method | Path | Description |
|---|---|---|
| GET | / |
Root endpoint |
| GET | /health |
Health check |
| GET | /about |
Project information |
| POST | /echo |
Echo test endpoint |
| POST | /chat |
Mock chat endpoint |
| Method | Path | Description |
|---|---|---|
| GET | /todos |
List all todos |
| POST | /todos |
Create a todo |
| GET | /todos/{todo_id} |
Get a todo by id |
| PATCH | /todos/{todo_id} |
Update a todo |
| DELETE | /todos/{todo_id} |
Delete a todo |
| Method | Path | Description |
|---|---|---|
| POST | /ai/chat |
Chat with the LLM |
| POST | /ai/extract-tasks |
Extract todo tasks from natural language |
| POST | /ai/create-todos |
Extract tasks and save them into the database |
Request:
{
"title": "Learn FastAPI",
"due_time": "Tomorrow morning"
}Response:
{
"id": 1,
"title": "Learn FastAPI",
"completed": false,
"due_time": "Tomorrow morning"
}Request:
GET /todos
Response:
[
{
"id": 1,
"title": "Learn FastAPI",
"completed": false,
"due_time": "Tomorrow morning"
}
]Request:
{
"title": "Review FastAPI",
"completed": true,
"due_time": "Tonight"
}Response:
{
"id": 1,
"title": "Review FastAPI",
"completed": true,
"due_time": "Tonight"
}Request:
DELETE /todos/1
Response:
{
"message": "Todo deleted"
}Endpoint:
POST /ai/chat
Request:
{
"message": "用一句话鼓励我继续学习 FastAPI"
}Response:
{
"user_message": "用一句话鼓励我继续学习 FastAPI",
"assistant_message": "继续加油,掌握 FastAPI 会让你具备构建现代 Web 服务的核心能力。",
"model": "qwen3.5-plus"
}Endpoint:
POST /ai/extract-tasks
Request:
{
"text": "明天上午九点学习 FastAPI,下午三点写 Docker 总结,晚上复习 SQLModel"
}Response:
{
"tasks": [
{
"title": "学习 FastAPI",
"time": "明天上午九点"
},
{
"title": "写 Docker 总结",
"time": "明天下午三点"
},
{
"title": "复习 SQLModel",
"time": "明天晚上"
}
]
}Endpoint:
POST /ai/create-todos
Request:
{
"text": "明天上午九点学习 FastAPI,下午三点写 Docker 总结,晚上复习 SQLModel"
}Response:
{
"extracted_tasks": [
{
"title": "学习 FastAPI",
"time": "明天上午九点"
},
{
"title": "写 Docker 总结",
"time": "明天下午三点"
},
{
"title": "复习 SQLModel",
"time": "明天晚上"
}
],
"created_todos": [
{
"id": 1,
"title": "学习 FastAPI",
"completed": false,
"due_time": "明天上午九点"
},
{
"id": 2,
"title": "写 Docker 总结",
"completed": false,
"due_time": "明天下午三点"
},
{
"id": 3,
"title": "复习 SQLModel",
"completed": false,
"due_time": "明天晚上"
}
]
}v0.9 - AI Todo Assistant backend with Docker, SQLite persistence, and LLM-powered task creation.
- How to build a FastAPI backend
- How to design RESTful CRUD endpoints
- How to use Pydantic request and response models
- How to use path parameters and request bodies
- How to handle HTTP errors
- How to use SQLModel with SQLite
- How to persist data with a database
- How to Dockerize a FastAPI project
- How to use Docker volumes for persistent data
- How to call an OpenAI-compatible LLM API
- How to extract structured JSON from natural language
- How to connect AI output with backend business logic
- How to manage configuration with environment variables
- How to add basic application logging
- How to write API tests with pytest and TestClient
- How to use a separate test database
- Add filtering by completed status
- Add filtering by due time
- Add better time parsing
- Add tests with pytest
- Add logging and configuration cleanup
- Add a simple frontend
- Deploy the API online
Install test dependencies:
pip install -r requirements.txtRun tests:
pytestCurrent test coverage includes:
- Health check endpoint
- Todo creation
- Full Todo CRUD flow
- Request validation for empty title
The tests use a separate SQLite database:
data/test_todos.db
This avoids polluting the local development database:
data/todos.db