Tutorial 01: Build a Todo API

Build an authenticated todo API with owner-scoped records, a collection trigger, and the admin dashboard.

Outcome: Register a user, create todos, list only your todos, and see trigger logs when todos are created.

Example project: examples/todo-api/

Prerequisites

  • Bun installed
  • Bakend repository cloned

Step 1: Start the server

Option A — use the bundled example:

cd examples/todo-api
bun run ../../src/index.ts start

Option B — scaffold your own project:

bak init my-todos
cd my-todos
# copy collections and functions from examples/todo-api/
bak start

Expected output includes Listening on :8080.

Verify:

curl http://localhost:8080/health

Step 2: Register a user

curl -X POST http://localhost:8080/api/auth/register \
  -H 'Content-Type: application/json' \
  -d '{"email":"you@example.com","password":"password123"}'

Save the accessToken from the JSON response.

Step 3: Create todos

curl -X POST http://localhost:8080/api/todos \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer <accessToken>' \
  -d '{"title":"Buy milk","completed":false}'

Create another:

curl -X POST http://localhost:8080/api/todos \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer <accessToken>' \
  -d '{"title":"Walk the dog","completed":false}'

Check server logs for Todo created: ... from the onCreate function.

Step 4: List your todos

curl http://localhost:8080/api/todos \
  -H 'Authorization: Bearer <accessToken>'

Only todos you own are returned (read: owner permission).

Step 5: Mark a todo complete

Copy a todo id from the list response, then:

curl -X PUT http://localhost:8080/api/todos/<id> \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer <accessToken>' \
  -d '{"completed":true}'

Step 6: Browse in the dashboard

Open http://localhost:8080/_/ and explore the todos collection.

Verify it works

  • POST /api/auth/register returns tokens
  • POST /api/todos creates records with your token
  • GET /api/todos returns only your todos
  • Server logs show Todo created: messages
  • Dashboard shows the todos collection

What you built

FilePurpose
collections/todos.jsonSchema and owner permissions
functions/todos/on-create.tsLogs when a todo is created
bakend.jsonServer config with JWT secret

Next steps