# Ninja Run API — guide for AI agents

You are connected to **Ninja Run**, an app for running ninja (obstacle course) competitions.
Base URL: `https://ninja.app.fearstudios.dev/api/v1` · OpenAPI spec: `https://ninja.app.fearstudios.dev/api/v1/openapi.json`

## Authentication
Send the API key on every request:
```
Authorization: Bearer nk_xxxxxxxx
```
Keys are created by the admin in **Settings → API keys** and are either `read` (GET only) or `read_write`.
Every change you make is recorded in the audit log under `api:<key name>`. Call `GET /api/v1/me` to check your key.

## How the data fits together
- **Event** — everything lives under an event.
- **Age group** — free-text label (e.g. "9-11"). Created automatically when you give a contestant an age group name. Matching is case-insensitive.
- **Contestant** — firstName, lastName, ageGroup.
- **Session** — a block of the day (e.g. "Session 1 — Morning").
- **Rotation group** — a group inside a session. A contestant can be in **one group per session**, and can be in several sessions. Adding a contestant to another group in the same session moves them.
- **Course** — an ordered list of **obstacles**. Each rotation group has an ordered **course rotation** (which courses it runs).
- **Run** — one contestant on one course in one session: a stopwatch **time** plus a **score per obstacle**.
  - Scores: `0` = missed, `1`, `2`, `3` = bonus. Obstacles left out count as 0. Max points = 3 × obstacles.
  - `hitBuzzer: true` means they finished (judge hit the buzzer); false means the clock was stopped early (fell / didn't finish). Either way the time counts.
  - `status`: `completed` (buzzer), `dnf` (stopped), `dns` (did not start — no time, unranked).
  - **Only one run per contestant per course per session. Posting a run again OVERWRITES the previous one.**
- **Leaderboard** — grouped by **session → course → age group**, only age groups that actually ran. Ranked by **most points, then lowest time**. Ties (same points and time) share a rank.

## Conventions
- JSON in, JSON out. IDs are UUIDs. Times are milliseconds (`timeMs`).
- Errors: `{"error": {"status": 400, "message": "..."}}` — 400 validation, 401 bad key, 403 read-only key, 404 not found, 409 duplicate name.
- Names of sessions/groups/age groups must be unique within their parent.

## Recipes
**Understand an event:** `GET /api/v1/events` → pick one → `GET /api/v1/events/{eventId}/overview` (sessions, groups, members, courses, obstacles in one call).

**Set up an event from a roster:**
1. `POST /api/v1/events` `{"name": "...", "date": "2026-12-13"}`
2. `POST /api/v1/events/{eventId}/courses` `{"name": "Pro Course", "obstacles": ["Steps", "Salmon Ladder", "Warped Wall"]}` (repeat per course)
3. `POST /api/v1/events/{eventId}/contestants/import` with `rows` (each: firstName, lastName, ageGroup, session, rotationGroup) — run with `"dryRun": true` first and check the result, then again with `"dryRun": false`. Sessions and groups are created for you.
4. For each session: `POST /api/v1/sessions/{sessionId}/auto-rotate` to give every group all courses (staggered), or `PUT /api/v1/groups/{groupId}/courses` for a custom order.

**Record a result:** look up ids via the overview, then `POST /api/v1/runs` with sessionId, courseId, contestantId, timeMs, hitBuzzer and scores keyed by obstacle id.

**Get results:** `GET /api/v1/events/{eventId}/leaderboard?sessionId=...`, or a report: `GET /api/v1/events/{eventId}/reports/awards?top=3` (add `&format=csv` for CSV). Reports: leaderboard, awards, runs, obstacles, missing, roster.

**Be careful:** DELETE on events, sessions, courses and contestants also deletes their runs. Confirm with the user before deleting.

## Example
```bash
curl -s https://ninja.app.fearstudios.dev/api/v1/events -H "Authorization: Bearer $NINJA_API_KEY"
curl -s -X POST https://ninja.app.fearstudios.dev/api/v1/runs -H "Authorization: Bearer $NINJA_API_KEY" -H "Content-Type: application/json" \
  -d '{"sessionId":"...","courseId":"...","contestantId":"...","timeMs":45230,"hitBuzzer":true,"scores":{"<obstacleId>":3}}'
```

## Endpoints
## Meta

### GET /api/v1/me

Who am I — the API key's name and scope.

Scope: `read`

### GET /api/v1/audit

Audit log of all changes (newest first).

Scope: `read`

Query: `limit` — Max rows (default 100, max 1000); `entity` — event | contestant | session | rotation_group | course | obstacle | run | age_group | api_key; `entityId` — Only changes to this id; `actor` — Filter by actor, e.g. "api:" or "admin:"

## Events

### GET /api/v1/events

List events.

Scope: `read`

### POST /api/v1/events

Create an event.

Scope: `read_write`

Body example:
```json
{
  "name": "Winter Ninja Open",
  "date": "2026-12-13",
  "location": "Main Gym"
}
```

### GET /api/v1/events/:eventId

Get an event with counts.

Scope: `read`

### GET /api/v1/events/:eventId/overview

Everything about an event in one call. Event, age groups, courses (with obstacles), sessions (with rotation groups, their members and course rotation). Start here to understand an event.

Scope: `read`

### PATCH /api/v1/events/:eventId

Update an event.

Scope: `read_write`

Body example:
```json
{
  "status": "live"
}
```

### DELETE /api/v1/events/:eventId

Delete an event and EVERYTHING in it.

Scope: `read_write`

## Age groups

### GET /api/v1/events/:eventId/age-groups

List age groups (created automatically from contestants).

Scope: `read`

### PATCH /api/v1/age-groups/:id

Rename / reorder an age group.

Scope: `read_write`

Body example:
```json
{
  "name": "9-11"
}
```

### DELETE /api/v1/age-groups/:id

Delete an age group (contestants keep existing with no age group).

Scope: `read_write`

## Contestants

### GET /api/v1/events/:eventId/contestants

List / search contestants.

Scope: `read`

Query: `q` — Search first/last name; `ageGroupId` — Filter by age group; `sessionId` — Only contestants in this session

### POST /api/v1/events/:eventId/contestants

Add a contestant. ageGroup is a free-text name; it is created if it doesn't exist (case-insensitive match).

Scope: `read_write`

Body example:
```json
{
  "firstName": "Ava",
  "lastName": "Stone",
  "ageGroup": "9-11"
}
```

### POST /api/v1/events/:eventId/contestants/import

Bulk import contestants (JSON rows or CSV text). Creates missing age groups, sessions and rotation groups and places contestants in them. Duplicates = same first+last name (case-insensitive). Use dryRun:true first to preview. All-or-nothing transaction.

Scope: `read_write`

Body example:
```json
{
  "onDuplicate": "skip",
  "dryRun": true,
  "rows": [
    {
      "firstName": "Ava",
      "lastName": "Stone",
      "ageGroup": "9-11",
      "session": "Session 1",
      "rotationGroup": "Group A"
    }
  ]
}
```

### GET /api/v1/contestants/:id

Get a contestant with all their runs.

Scope: `read`

### PATCH /api/v1/contestants/:id

Update a contestant.

Scope: `read_write`

Body example:
```json
{
  "ageGroup": "12-14"
}
```

### DELETE /api/v1/contestants/:id

Delete a contestant (and their runs).

Scope: `read_write`

## Sessions

### GET /api/v1/events/:eventId/sessions

List sessions.

Scope: `read`

### POST /api/v1/events/:eventId/sessions

Create a session.

Scope: `read_write`

Body example:
```json
{
  "name": "Session 1 — Morning"
}
```

### GET /api/v1/sessions/:id

Get a session with its rotation groups (members + courses).

Scope: `read`

### PATCH /api/v1/sessions/:id

Rename / reorder a session.

Scope: `read_write`

Body example:
```json
{
  "name": "Morning"
}
```

### DELETE /api/v1/sessions/:id

Delete a session, its groups and runs.

Scope: `read_write`

### POST /api/v1/sessions/:id/groups

Create a rotation group in a session.

Scope: `read_write`

Body example:
```json
{
  "name": "Group A"
}
```

### POST /api/v1/sessions/:id/auto-rotate

Assign all courses to every group, each starting on a different course.

Scope: `read_write`

## Rotation groups

### GET /api/v1/groups/:id

Get a group with members and course rotation.

Scope: `read`

### PATCH /api/v1/groups/:id

Rename / reorder a group.

Scope: `read_write`

Body example:
```json
{
  "name": "Group B"
}
```

### DELETE /api/v1/groups/:id

Delete a group (contestants stay in the event).

Scope: `read_write`

### POST /api/v1/groups/:id/members

Add contestants to a group (moves them if already in another group of the same session).

Scope: `read_write`

Body example:
```json
{
  "contestantIds": [
    "<contestant uuid>"
  ]
}
```

### DELETE /api/v1/groups/:id/members/:contestantId

Remove a contestant from a group.

Scope: `read_write`

### PUT /api/v1/groups/:id/courses

Set the group's course rotation (ordered, replaces existing).

Scope: `read_write`

Body example:
```json
{
  "courseIds": [
    "<course uuid>",
    "<course uuid>"
  ]
}
```

## Courses

### GET /api/v1/events/:eventId/courses

List courses with obstacles.

Scope: `read`

### POST /api/v1/events/:eventId/courses

Create a course, optionally with obstacles in order.

Scope: `read_write`

Body example:
```json
{
  "name": "Pro Course",
  "obstacles": [
    "Quad Steps",
    "Salmon Ladder",
    "Warped Wall"
  ]
}
```

### GET /api/v1/courses/:id

Get a course with obstacles.

Scope: `read`

### PATCH /api/v1/courses/:id

Rename / reorder a course.

Scope: `read_write`

Body example:
```json
{
  "name": "Rookie Course"
}
```

### DELETE /api/v1/courses/:id

Delete a course and its runs.

Scope: `read_write`

### POST /api/v1/courses/:id/obstacles

Add an obstacle to the end of a course.

Scope: `read_write`

Body example:
```json
{
  "name": "Warped Wall"
}
```

### PUT /api/v1/courses/:id/obstacles/order

Reorder obstacles (must list every obstacle id once).

Scope: `read_write`

Body example:
```json
{
  "obstacleIds": [
    "<obstacle uuid>",
    "<obstacle uuid>"
  ]
}
```

### PATCH /api/v1/obstacles/:id

Rename an obstacle.

Scope: `read_write`

Body example:
```json
{
  "name": "Salmon Ladder"
}
```

### DELETE /api/v1/obstacles/:id

Delete an obstacle (its scores on runs are deleted).

Scope: `read_write`

## Runs

### GET /api/v1/events/:eventId/runs

List runs.

Scope: `read`

Query: `sessionId` — Filter by session; `courseId` — Filter by course; `contestantId` — Filter by contestant

### POST /api/v1/runs

Record a run (overwrites the contestant's previous run on that course in that session). status defaults to "completed" when hitBuzzer is true, else "dnf". Use status "dns" (did not start) with no time. scores: 0 missed, 1, 2, 3 = bonus; obstacles left out count as 0. scores may be an object {obstacleId: score} or an array of {obstacleId, score}.

Scope: `read_write`

Body example:
```json
{
  "sessionId": "<session uuid>",
  "courseId": "<course uuid>",
  "contestantId": "<contestant uuid>",
  "timeMs": 45230,
  "hitBuzzer": true,
  "scores": {
    "<obstacle uuid>": 3,
    "<obstacle uuid 2>": 2
  }
}
```

### GET /api/v1/runs/:id

Get a run with per-obstacle scores.

Scope: `read`

### DELETE /api/v1/runs/:id

Delete a run.

Scope: `read_write`

## Results

### GET /api/v1/events/:eventId/leaderboard

Leaderboard grouped by session → course → age group. Ranked by most points, then lowest time. Ties share a rank (tied: true). DNS runs have rank null. scores[] follows the obstacles[] order.

Scope: `read`

Query: `sessionId` — Only this session; `courseId` — Only this course; `ageGroupId` — Only this age group ("none" = contestants with no age group)

### GET /api/v1/reports

List available reports.

Scope: `read`

### GET /api/v1/events/:eventId/reports/:report

Run a report (JSON table or CSV). report: leaderboard | awards | runs | obstacles | missing | roster. Add format=csv for a CSV download.

Scope: `read`

Query: `sessionId` — Only this session; `courseId` — Only this course; `ageGroupId` — Only this age group ("none" = contestants with no age group); `top` — awards only: number of places (default 3); `format` — json (default) or csv
