# Explainer voice server

A Cloudflare Worker that lets the Explainer narrate with ElevenLabs voices
without putting your ElevenLabs key in the page. The page sends each line of
narration to the worker; the worker adds the key, asks ElevenLabs for the
audio plus per-character timings, and sends that back.

It only answers browser requests from `https://steveinflow.github.io` (and
`localhost` for testing), caps each request at 600 characters, rate-limits
each IP to 60 lines a minute, and caches identical lines for 30 days so
repeated openers and stall lines are only paid for once.

## Set up (about 10 minutes)

1. **Make an ElevenLabs API key.** In ElevenLabs, open Developers → API Keys
   and create a key. Give it access to Text to Speech and read access to
   Voices only, and set a monthly credit limit on the key: that limit is the
   hard cap on what the worker can ever spend. A narrated explainer is about
   1,500–1,800 characters.

2. **Deploy the worker.** You need Node.js and a free Cloudflare account.

   ```sh
   cd projects/explainer/worker
   npx wrangler@latest login
   npx wrangler@latest secret put ELEVENLABS_API_KEY   # paste the key when asked
   npx wrangler@latest deploy
   ```

   The deploy prints the worker's address, something like
   `https://explainer-voice.<your-subdomain>.workers.dev`.

3. **Point the Explainer at it.** Open the Explainer, open Settings, set
   Narrator to ElevenLabs and paste the worker address into Voice server.
   The voice list fills in from your ElevenLabs account; pick one.

## Options (in `wrangler.toml`)

- `ALLOWED_ORIGINS`: add another site, comma-separated, if you host the page elsewhere.
- `MODEL_ID`: `eleven_flash_v2_5` is the fastest and cheapest.
  `eleven_multilingual_v2` sounds richer and costs about twice as much per character.
- `DEFAULT_VOICE_ID`: the voice used when the page doesn't name one.
- `[[ratelimits]]`: the per-IP limit. Needs Wrangler 4.36 or later; delete the
  block if your account doesn't support it and the worker runs without it.

Redeploy with `npx wrangler@latest deploy` after changing anything.
