# Local Agile Planner API

The app exposes a small JSON API from the same server that serves the board UI. By default, run `npm start` and use `http://localhost:3000`.

The local Node server stores board data in `data/board.json` (or `AGILE_DATA_DIR`) and does not require authentication. The deployed Worker stores accounts, sessions, settings, columns, tickets, and comments in Cloudflare D1. All registered accounts currently share one board; each account has its own profile.

## Authentication (Cloudflare deployment)

All authentication endpoints accept and return JSON. The browser receives an HttpOnly, SameSite=Strict session cookie; the raw session token is not returned in JSON. Passwords require 12-128 characters and are stored as salted chained PBKDF2-SHA-256 hashes (six 100,000-iteration rounds to fit Workers WebCrypto limits). Sessions expire after 30 days.

`POST /api/auth/signup` with `{"name":"Ada Lovelace","email":"ada@example.com","password":"at-least-12-characters"}` creates an account and signs it in. Returns the user object and sets the session cookie. Signup is open to visitors.

`POST /api/auth/signin` with `{"email":"ada@example.com","password":"..."}` signs in and sets the session cookie.

`GET /api/auth/session` returns `{"authenticated":true,"user":{"id":"...","email":"...","name":"...","role":"..."}}` or `{"authenticated":false,"user":null}`.

`POST /api/auth/signout` revokes the current session and clears the cookie.

All board and ticket endpoints below require a valid session on Cloudflare (401 otherwise). Send browser requests with same-origin credentials. For command-line use, retain the session cookie from signin, then pass it with `-b cookies.txt` and `-c cookies.txt` to curl.

## Endpoints

### Get the board

`GET /api/board`

Returns project metadata, workflow columns, all issues, and the next issue number.

```json
{
  "project": { "key": "PROJ", "name": "My Project", "description": "" },
  "columns": [
    { "id": "backlog", "name": "Backlog", "color": "#9298a5" }
  ],
  "issues": [],
  "nextIssue": 1
}
```

### Update board or columns

`PATCH /api/board`

Update the project name, description, or key; update the local profile; rename, add, or delete columns. Deleting a column moves its issues to another existing column.

```json
{ "project": { "key": "PROJ", "name": "My Project", "description": "" } }
```

```json
{ "addColumn": { "name": "QA" } }
```

```json
{ "column": { "id": "todo", "name": "Ready" } }
```

```json
{ "deleteColumn": "custom-123" }
```

### Create an issue

`POST /api/issues`

Content type: `application/json`

| Field | Type | Required | Notes |
| --- | --- | --- | --- |
| `title` | string | Yes | Must contain non-whitespace text. |
| `type` | string | No | Defaults to `Task`. Supported UI values: `Task`, `Story`, `Bug`, `Epic`. |
| `priority` | string | No | Defaults to `Medium`. UI values: `Highest`, `High`, `Medium`, `Low`, `Lowest`. |
| `status` | string | No | Column ID; defaults to `todo` when omitted or invalid. |
| `assignee` | string | No | Defaults to an empty string. |
| `labels` | string | No | Comma-separated label names. |
| `description` | string | No | Defaults to an empty string. |

Example request:

```bash
curl -X POST http://localhost:3000/api/issues \
  -H 'Content-Type: application/json' \
  -d '{"title":"Add CSV export","type":"Story","priority":"Medium","status":"todo","assignee":"Developer","labels":"reporting, export","description":"Let users export issues."}'
```

Returns `201 Created` and the created issue. Issue IDs are assigned from the project key and incrementing counter, for example `PROJ-1`.

### Update an issue

`PATCH /api/issues/{issueId}`

Send one or more fields as JSON. Supported fields are `title`, `type`, `priority`, `status`, `assignee`, `labels`, and `description`. For `labels`, send either a comma-separated string or an array of strings. Invalid status IDs are ignored.

Example:

```bash
curl -X PATCH http://localhost:3000/api/issues/PROJ-1 \
  -H 'Content-Type: application/json' \
  -d '{"status":"progress","priority":"High"}'
```

Returns the updated issue, or `404 Not Found` if the ID does not exist.

### Add a comment

`POST /api/issues/{issueId}/comments`

Send `{ "text": "Comment text" }`. On Cloudflare, the author is always the signed-in account name. Local mode also accepts an optional `author`. Returns the saved comment with its ID and UTC timestamp.

### Delete an issue

`DELETE /api/issues/{issueId}`

Returns `{ "ok": true }`, or `404 Not Found` when the ID does not exist.

## Issue object

```json
{
  "id": "PROJ-1",
  "title": "Add CSV export",
  "type": "Story",
  "priority": "Medium",
  "status": "todo",
  "assignee": "Developer",
  "labels": ["reporting", "export"],
  "description": "Let users export issues.",
  "comments": [],
  "created": "2026-11-29T10:00:00.000Z"
}
```

`status` is a column ID from the board response. `created` is an ISO 8601 timestamp in UTC.

## Errors

API errors are JSON objects with an `error` message. Common status codes:

- `400 Bad Request` — missing or blank issue title.
- `404 Not Found` — missing issue, column, or static file; unknown non-API paths return a plain-text 404.
- `405 Method Not Allowed` — unsupported method for an API route.
- `413 Payload Too Large` — request body exceeds 1 MB.
- `500 Internal Server Error` — storage or request handling error.
