# API conventions (v1)

- Base path: `/api/v1`. JSON only. Staff SPA uses Sanctum cookie sessions (stateful, same origin).
- Sign-in flow: `GET /sanctum/csrf-cookie` → `POST /api/v1/auth/login` (`{email, password}`) → session cookie.
- Public identifiers are ULIDs (`id` in payloads); numeric primary keys are never exposed.
- Success: resources are wrapped as `{ "data": ... }`. (Health endpoints are exempt: `{status, timestamp}`.)
- Errors (every `/api/*` failure):

```json
{ "message": "Human readable text", "code": "machine_code", "request_id": "uuid-or-client-id",
  "errors": { "field": ["message"] } }
```

| HTTP | `code` |
|---|---|
| 401 | `unauthenticated` |
| 403 | `forbidden` |
| 404 | `not_found` (also used for out-of-scope records — no existence leak) |
| 405 | `method_not_allowed` |
| 419 | `session_expired` (CSRF/session) |
| 422 | `validation_failed` (+ `errors`) |
| 429 | `too_many_requests` (+ `Retry-After`) |
| 5xx | `server_error` / `service_unavailable` (details only in the log; `debug` block only when `APP_DEBUG=true`) |

- `X-Request-Id`: accepted if well-formed (8–64 chars `A-Za-z0-9._-`), otherwise generated; always echoed.
- Rate limits: `api` group 120/min per user or IP; login 5/min per email+IP.

## Implemented endpoints (Phase 1 foundation)

Public: `GET /up`, `GET /api/v1/health`, `POST /api/v1/auth/login` (throttled), `POST /api/v1/auth/mfa/verify` (throttled),
`POST /api/v1/auth/forgot-password` (throttled, always 202), `POST /api/v1/auth/reset-password` (throttled).
Everything else requires a signed-in, active user.

| Group | Endpoints |
|---|---|
| Session | `POST /auth/login` → 200 + user, or **202 `{data:{mfa_required:true}}`**; `POST /auth/mfa/verify` `{code}` or `{recovery_code}`; `POST /auth/logout` (204); `GET /auth/me` (user, company, roles, permissions, `mfa`) |
| Account security | `POST /auth/password`; `POST /auth/mfa/enroll`, `/confirm` (returns recovery codes once), `/disable` |
| Users | `GET/POST /users`; `GET/PATCH /users/{id}`; `PUT /users/{id}/roles`; `POST /users/{id}/deactivate|activate|invite|mfa/reset` |
| Roles | `GET/POST /roles`; `GET/PATCH/DELETE /roles/{id}`; `PUT /roles/{id}/permissions`; `GET /permissions` |
| Organization | `GET/POST /branches|departments|teams`; `GET/PATCH/PUT/DELETE /{kind}/{id}`; `PUT /teams/{id}/members` |
| Audit | `GET /audit-logs` (filters `action` prefix, `entity_type`, `actor`, `from`, `to`; no write routes exist) |
| Health | `GET /health/details` (permission `admin.settings.manage`) |

New business-rule error codes: `mfa_enrollment_required`, `mfa_required_by_policy`, `mfa_already_enabled`, `invalid_mfa_code`, `invalid_token`,
`last_administrator`, `cannot_deactivate_self`, `protected_role`, `protected_permission`, `role_in_use`, `in_use`, `user_disabled`.
List endpoints return `{data, links, meta}` with `per_page` capped at 100; sorting/filtering use allow-lists.
