# Your first backend A small local task API with persistent SQLite storage, input validation and real HTTP tests. Requires Python 3.10+ with SQLite support. No dependencies, accounts, API keys or AI assistant are needed. Python's built-in HTTP server is for local learning; keep this example off public networks. From the Foundry repository: ```sh cd examples/backend-starter python3 app.py ``` Leave that terminal running. In another terminal: ```sh curl -i http://127.0.0.1:8000/health curl -i -X POST http://127.0.0.1:8000/tasks \ -H 'Content-Type: application/json' \ -d '{"title":"Learn backend development"}' curl -i http://127.0.0.1:8000/tasks ``` Expect `200` with `{"status":"ok"}`, `201` with the created task, then `200` with a `tasks` array. The ID increments as you create tasks. Windows PowerShell users can use `curl.exe` or an HTTP client if shell quoting differs. Stop with Ctrl+C. Restarting keeps tasks in `tasks.sqlite3` in your working directory. To use another file or port: `python3 app.py --database practice.sqlite3 --port 8001`. The database parent directory must exist. Stop the server before deleting a practice database to reset it. Database files are ignored by this example's Git configuration. Run the tests from this directory: ```sh python3 -m unittest discover -s tests -v ``` Tests use an isolated temporary database and an automatically assigned loopback port. They verify persistence across restart, validation, malformed requests and parameterized SQL behavior without changing your practice data. ## API contract | Method | Route | Success | Failure | |---|---|---|---| | GET | `/health` | 200, process is responding | Does not check database readiness | | GET | `/tasks` | 200, `{"tasks":[{"id":1,"title":"…"}]}` | Database failures are not yet mapped to JSON | | POST | `/tasks` | 201, created task | 400 invalid JSON/framing, 413 body over 8192 bytes, 415 wrong media type, 422 invalid fields | A create request must contain exactly one field, `title`: a string with 1–200 characters after trimming whitespace. Unknown paths return 404. Other HTTP methods use the standard server's 501 response, which is HTML. There is no authentication; all local clients share all tasks. Listing is unpaginated. This deliberately small example has no migrations, deployment setup, rate limiting or production database error handling. ## Understand and extend it Read `app.py` in request order: route selection, body limits, JSON parsing, field validation, parameterized SQL and JSON response. A backend accepts requests, enforces rules, reads or writes data, and returns a defined response. The tests exercise that full path through an actual HTTP socket. Try these increments, adding a failing behavior test before implementing each: 1. Fetch a task by ID; distinguish an invalid ID from a missing task. 2. Add pagination with a bounded page size and deterministic ordering. 3. Add completion with a database migration that preserves existing tasks. 4. Add authentication and ownership; test that one user cannot access another user's task. For an application with a chosen framework, use the [backend starting guide](../../docs/backend-start.md). Copy this folder elsewhere if you want a separate learning repository; the main Foundry template remains stack-neutral.