A lightweight RESTful CRUD API built with Python and FastAPI for managing to-do tasks.
Built for Week 2 of the AI Fluency backend track to demonstrate:
- HTTP request-response cycles
- HTTP status codes
- Input validation
- RESTful CRUD operations
- Automated OpenAPI documentation with Swagger UI
- Python 3.10+
pippackage manager
pip install fastapi uvicorn pydanticpython -m uvicorn main:app --reloadBase URL: http://127.0.0.1:8000
Interactive Swagger UI: http://127.0.0.1:8000/docs
| Method | Endpoint | Description | Expected Body | Success Status | Error Status |
|---|---|---|---|---|---|
| GET | / |
API Metadata & Routes | None | 200 OK |
- |
| GET | /health |
Server Health Status | None | 200 OK |
- |
| GET | /stats |
Task Counts (Total/Done/Open) | None | 200 OK |
- |
| GET | /tasks |
List Tasks (Optional done/search) |
None | 200 OK |
- |
| GET | /tasks/{id} |
Get Single Task by ID | None | 200 OK |
404 Not Found |
| POST | /tasks |
Create New Task | {"title": "Buy milk"} |
201 Created |
400 Bad Request |
| PUT | /tasks/{id} |
Update Task | {"title": "New", "done": true} |
200 OK |
400 Bad Request, 404 Not Found |
| DELETE | /tasks/{id} |
Delete Task | None | 204 No Content |
404 Not Found |
| POST | /reset |
Reset DB to 3 initial seed items | None | 200 OK |
- |
The API can be tested from the terminal using curl.
curl -i -X POST http://127.0.0.1:8000/tasks \
-H "Content-Type: application/json" \
-d '{"title":"Complete Week 2 Assignment"}'Expected response:
HTTP/1.1 201 Created
date: Sun, 16 Aug 2026 17:00:00 GMT
server: uvicorn
content-type: application/json
{
"id": 4,
"title": "Complete Week 2 Assignment",
"done": false
}When new tasks are created and the Uvicorn server is restarted using:
Ctrl+C
followed by:
python -m uvicorn main:app --reloadall newly created tasks disappear, and the application returns to the initial 3 seed tasks.
The application stores its data in a volatile Python list called:
tasks_dbThis list exists only in the computer's RAM (memory) while the Python process is running.
When the server process stops:
- The Python process terminates.
- The
tasks_dblist is destroyed. - All newly created tasks are lost.
- When the server starts again, the original seed data is created.
This demonstrates why real-world applications require persistent storage such as:
- SQL databases
- NoSQL databases
- Other persistent storage engines
Write a single-file Python REST API using FastAPI that manages an in-memory task to-do list. Implement GET /, GET /health, GET /tasks, GET /tasks/{id} (404 if missing), POST /tasks (generates next ID, validates non-empty title returning 400 Bad Request, sets default done=False), PUT /tasks/{id} (updates title/done, returns 400 or 404), and DELETE /tasks/{id} (returns 204 No Content). Ensure Swagger UI is available at /docs.
The AI used Pydantic's:
Field(min_length=1)for declarative validation directly inside the request model.
This provides a clean way to prevent completely empty strings from being submitted.
The AI accepted whitespace-only strings such as:
" "
as valid task titles.
Although min_length=1 rejects an actually empty string, whitespace characters still count toward the length.
Manual string stripping was required:
payload.title.strip()This ensures that a title containing only whitespace is rejected.
The AI returned:
{
"message": "Task deleted"
}alongside:
204 No Content
This is incorrect.
A 204 No Content response must not contain a response body.
The DELETE endpoint should return only:
204 No Content
with no JSON response body.
The original prompt did not explicitly specify how new task IDs should be generated.
The AI used:
len(tasks_db) + 1This can create duplicate IDs.
Suppose the database contains:
1
2
3
If task 3 is deleted, the database now contains:
1
2
Using:
len(tasks_db) + 1would generate:
3
which happens to work in this case.
But if an earlier ID is deleted:
1
3
4
then:
len(tasks_db) + 1would produce:
4
which is already being used.
The ID-generation logic was updated to:
max(existing_ids) + 1This generates the next ID based on the highest existing ID rather than the number of tasks currently stored.
This project demonstrates practical backend concepts including:
- REST API architecture
- FastAPI routing
- HTTP methods
- HTTP status codes
- CRUD operations
- Pydantic validation
- Swagger / OpenAPI documentation
- In-memory data storage
- API testing with
curl - Server lifecycle and data persistence
- AI-generated code evaluation
- Identifying and fixing AI coding mistakes
Week 2 — AI Fluency Backend Track
Technology: Python + FastAPI + Pydantic + Uvicorn
Storage: In-memory Python list
Documentation: OpenAPI / Swagger UI