# Iron SFX Creator — External Agent API Guide (`/api/v1`)

Iron SFX Creator designs Diggy-ready **48 kHz 16-bit `.wav` sound effects** from real recorded Vault clips—filtered, layered, meter-checked, and judged autonomously against `pop4.wav` and a mechanic video.

---

## 1. Authentication & Routing

Include your Personal Account API Key (copied from your `@iron.gs` Connections drawer at `https://sfx-creator.koda.games`) on every `/api/v1/*` request. **Both `isfx_...` and migrated `isc_...` keys are accepted:**

```http
Authorization: Bearer isfx_YOUR_PERSONAL_API_KEY
```
*(Also accepted via `X-API-Key: isfx_YOUR_PERSONAL_API_KEY` or with an `isc_...` key.)*

Your account automatically **routes using your connected OAuth sessions first** (Google Gemini, OpenAI Codex, Anthropic Claude) when supported by the role, with **automatic fallback to your saved provider API keys** (for example, OpenAI `gpt-audio-1.5` calls).

---

## 2. Pre-Flight Check: Phase 0 & Vault Ingest

Before requesting a new sound, check system readiness:

```bash
curl -sS "https://sfx-creator.koda.games/api/v1/status" \
  -H "Authorization: Bearer isfx_YOUR_KEY"
```

1. **If `vault.totalClips === 0`**, ingest `audio_bank/` into the SQLite Vault (blocks until done with `?wait=true`):
   ```bash
   curl -sS -X POST "https://sfx-creator.koda.games/api/v1/ingest?wait=true" \
     -H "Authorization: Bearer isfx_YOUR_KEY" \
     -H "Content-Type: application/json" \
     -d '{ "computeEmbeddings": true }'
   ```
2. **If `phase0.completedAt` is unset** (or `judges` is empty), run Phase 0 anchor measurement & judge qualification:
   ```bash
   curl -sS -X POST "https://sfx-creator.koda.games/api/v1/phase0/run?wait=true" \
     -H "Authorization: Bearer isfx_YOUR_KEY" \
     -H "Content-Type: application/json" \
     -d '{}'
   ```

---

## 3. End-to-End Sound Request Workflow

### Step 1 — Create Sound Request (`POST https://sfx-creator.koda.games/api/v1/sounds`)
Upload your isolated mechanic video as base64 (`.mp4` / `.webm`, 1–5 s) along with `role` and `topology`:

```bash
VIDEO_B64=$(base64 < mechanic_coin_pickup.mp4 | tr -d '\n')
curl -sS -X POST "https://sfx-creator.koda.games/api/v1/sounds" \
  -H "Authorization: Bearer isfx_YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d "{
    \"name\": \"coin_pickup\",
    \"role\": \"feedback\",
    \"topology\": \"one_shot\",
    \"videoBase64\": \"${VIDEO_B64}\",
    \"videoFileName\": \"mechanic_coin_pickup.mp4\",
    \"note\": \"Crisp pleasant muted gold coin pickup, zero harsh 4kHz ring\",
    \"maxWaves\": 3
  }"
```

### Step 2 — Run Autonomous Tournament (`POST https://sfx-creator.koda.games/api/v1/sounds/:id/run?wait=true`)
`?wait=true` holds the request until the multi-wave tournament + confirmation match finishes (or returns `refusalReport`):

```bash
curl -sS -X POST "https://sfx-creator.koda.games/api/v1/sounds/<projectId>/run?wait=true" \
  -H "Authorization: Bearer isfx_YOUR_KEY"
```

*(Alternatively pass `autoStart: true` with `?wait=true` on `POST /api/v1/sounds`, long-poll `GET /api/v1/sounds/<projectId>?wait=25`, or stream SSE `GET /api/v1/sounds/<projectId>/events`.)*

### Step 3 — Inspect Result, Sync to Diggy, Fetch JSON WAVs, or Download ZIP
- Check `status`:
  - `"exported"`: Read `exportManifest` (shipped wave, confidence tier, variations, measured pitch/LUFS, cost).
  - `"refused"`: Read `refusalReport` for concrete wave-by-wave gate failures and missing Vault recording types.
- **Copy directly into `diggyjai/assets_raw/audio/`**:
  ```bash
  curl -sS -X POST "https://sfx-creator.koda.games/api/v1/sounds/<projectId>/sync" \
    -H "Authorization: Bearer isfx_YOUR_KEY"
  ```
- **Fetch manifest + base64 WAVs in one JSON call**:
  ```bash
  curl -sS "https://sfx-creator.koda.games/api/v1/sounds/<projectId>/files" \
    -H "Authorization: Bearer isfx_YOUR_KEY"
  ```
- **Or download staged ZIP (`out/<name>/*.wav` + `manifest.json`)**:
  ```bash
  curl -sS -o coin_pickup_sfx.zip "https://sfx-creator.koda.games/api/v1/sounds/<projectId>/export.zip" \
    -H "Authorization: Bearer isfx_YOUR_KEY"
  ```

---

## 4. Field Reference for `POST /api/v1/sounds`

| Field | Type | Default | Description |
| :--- | :--- | :--- | :--- |
| `name` | `string` | required | Sound identifier (used for staging folder & `<name>_v01.wav` filenames). |
| `role` | `"constant" \| "feedback" \| "event"` | `"feedback"` | Attention & burst fatigue budget (`constant` = softest/shortest, `event` = richer arc). |
| `topology` | `"one_shot" \| "multi_hit" \| "loop_trilogy" \| "ticker"` | `"one_shot"` | Sound architecture. |
| `videoBase64` | `string` | optional | Base64-encoded isolated mechanic video (`.mp4` / `.webm`). |
| `videoFileName` | `string` | optional | Original video filename hint. |
| `triggerTimeMs` | `number` | auto | Optional trigger timestamp inside video (ms). |
| `runtimePitchRangeSemitones` | `[number, number]` | optional | Optional Diggy runtime pitch randomization range (e.g. `[-2, 2]`). |
| `incumbentBase64` | `string` | optional | Optional current `.wav` sound to beat head-to-head. |
| `note` | `string` | optional | Non-visual context (e.g. `"penalty, not reward"`). |
| `maxWaves` | `number` | `3` | Max exploration waves (`1..3`). |
| `autoStart` | `boolean` | `false` | Start tournament immediately on creation. |

---

## 5. Endpoint Summary

- `GET  /api/v1/instructions` — This guide (JSON default; `?format=markdown` or `?format=html`).
- `GET  /api/v1/models` — Connected OAuth accounts, API key status, and discovered model candidates.
- `POST /api/v1/estimate` — Compute Spec 6.8 best/typical/worst call & USD cost range.
- `GET  /api/v1/status` — Vault clip count, folder breakdown, anchors, Phase 0 status, active jobs.
- `POST /api/v1/ingest` — Ingest `audio_bank/` into SQLite Vault (supports `?wait=true`).
- `POST /api/v1/phase0/run` — Run Phase 0 anchor measurement or full qualification suite (supports `?wait=true`).
- `GET  /api/v1/sounds` — List all sound projects.
- `POST /api/v1/sounds` — Create a sound request (optional `videoBase64`, `incumbentBase64`, `autoStart`).
- `GET  /api/v1/sounds/:id` — Get project detail, waves, pool, manifest, or refusal report (supports `?wait=25` / `?wait=true`).
- `GET  /api/v1/sounds/:id/events` — Real-time Server-Sent Events (SSE) stream of tournament updates.
- `POST /api/v1/sounds/:id/run` — Start/attach tournament run (supports `?wait=true`).
- `POST /api/v1/sounds/:id/sync` — Copy staged `.wav` variations into `diggyjai/assets_raw/audio/`.
- `GET  /api/v1/sounds/:id/files` — Fetch export manifest + base64-encoded `.wav` variations as JSON.
- `GET  /api/v1/sounds/:id/export.zip` — Download `.zip` containing `out/<name>/*.wav` + `manifest.json`.
- `DELETE /api/v1/sounds/:id` — Delete a sound project.
