harry0703/MoneyPrinterTurbo · error · ElevenLabsAuthenticationError

ElevenLabs API key is required

Error message

ElevenLabs API key is required

What it means

ElevenLabsAuthenticationError raised by test_connection() when get_api_key() returns empty. The key is read from config.elevenlabs['api_key'] first, with the ELEVENLABS_API_KEY environment variable as fallback; both were blank/unset. Music generation shares one key with ElevenLabs TTS, so no separate music key exists to configure.

Source

Thrown at app/services/elevenlabs_music.py:114

        )
    else:
        body = str(body_bytes)
    body = body.strip().replace("\n", " ")[:MAX_ERROR_BODY_BYTES]
    return body or response.reason or "request failed"


def test_connection() -> dict[str, Any]:
    """
    使用不消耗音乐生成额度的订阅接口检查 API Key 和账号套餐。

    该接口只能确认 Key 可访问订阅信息以及账号不是免费套餐,不能证明当前 Key
    一定拥有 Music endpoint 权限。ElevenLabs 允许按 endpoint、额度和 IP 限制
    Key,因此 UI 成功提示必须保留这一边界,实际权限仍由生成请求最终确认。
    响应中的账单和用量详情不会写入日志,避免记录账号隐私。
    """
    api_key = get_api_key()
    if not api_key:
        raise ElevenLabsAuthenticationError("ElevenLabs API key is required")
    try:
        with requests.get(
            f"{_base_url()}{SUBSCRIPTION_PATH}",
            headers={"xi-api-key": api_key},
            timeout=(15, 30),
            stream=True,
        ) as response:
            if response.status_code == 401:
                raise ElevenLabsAuthenticationError(
                    "ElevenLabs API key was rejected (401): "
                    f"{_safe_response_error(response)}"
                )
            if not response.ok:
                raise ElevenLabsMusicError(
                    "ElevenLabs account check failed "
                    f"({response.status_code}): "
                    f"{_safe_response_error(response)}"
                )

View on GitHub (pinned to 1f9f19c202)

Solutions

  1. Set elevenlabs.api_key in the app config (same key as TTS) or export ELEVENLABS_API_KEY in the process environment.
  2. For Docker deployments, confirm the variable reaches the container (docker exec env | grep ELEVENLABS) and is not only in the host shell.
  3. Check with is_enabled() before offering the ElevenLabs music option in the UI.

Example fix

# before: no key configured
test_connection()  # -> ElevenLabsAuthenticationError

# after
export ELEVENLABS_API_KEY='xi-...'
test_connection()
Defensive patterns

Strategy: validation

Validate before calling

from app.services import elevenlabs_music

if not elevenlabs_music.is_enabled():
    raise ConfigError('set elevenlabs.api_key or ELEVENLABS_API_KEY before testing')

Try / catch

try:
    elevenlabs_music.test_connection()
except ElevenLabsAuthenticationError as e:
    if 'is required' in str(e):
        prompt_user_for_api_key()  # then retry

Prevention

When it happens

Trigger: Calling test_connection() (or any path calling it, e.g. validate_generation_access) with neither the elevenlabs.api_key config entry nor the ELEVENLABS_API_KEY env var set to a non-empty value.

Common situations: Fresh install without config.toml entry; env var set in the dev shell but not in the Docker container/service unit; key accidentally saved with surrounding quotes making it a blank string after strip().

Related errors


AI-assisted analysis of harry0703/MoneyPrinterTurbo@1f9f19c202 (2026-08-14). Data as JSON: /api/errors/4b1307cd86872902. Report an issue: GitHub.