harry0703/MoneyPrinterTurbo · error · ElevenLabsAuthenticationError

ElevenLabs API key was rejected (401): {_safe_response_error

Error message

ElevenLabs API key was rejected (401): {_safe_response_error(response)}

What it means

ElevenLabsAuthenticationError raised by test_connection() when the GET to the /v1/user/subscription endpoint returns HTTP 401. The key was present but the server rejected it. The message embeds _safe_response_error(response), which reads at most 500 bytes of the error body so the reason (invalid key, revoked key) is visible without dumping large or sensitive payloads into logs.

Source

Thrown at app/services/elevenlabs_music.py:123

    使用不消耗音乐生成额度的订阅接口检查 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)}"
                )
            try:
                payload = response.json()
            except ValueError as exc:
                raise ElevenLabsMusicError(
                    "ElevenLabs returned an invalid subscription response"
                ) from exc
    except requests.RequestException as exc:
        raise ElevenLabsMusicError(
            f"failed to connect to ElevenLabs: {exc}"

View on GitHub (pinned to 1f9f19c202)

Solutions

  1. Regenerate the key on the ElevenLabs dashboard and update config.elevenlabs.api_key / ELEVENLABS_API_KEY.
  2. Verify the key with a direct curl: curl -H 'xi-api-key: <key>' https://api.elevenlabs.io/v1/user/subscription — a 200 confirms the key, a 401 confirms it is dead.
  3. Ensure no stray quotes/whitespace were pasted along with the key.
Defensive patterns

Strategy: try-catch

Validate before calling

import re

def looks_like_elevenlabs_key(key: str) -> bool:
    return bool(key) and bool(re.fullmatch(r'[A-Za-z0-9_-]{20,}', key.strip()))

Try / catch

try:
    test_connection()
except ElevenLabsAuthenticationError as e:
    # covers both 'is required' and 'rejected (401)'
    invalidate_saved_key(); prompt_reentry()

Prevention

When it happens

Trigger: Calling test_connection() with a malformed, revoked, or deleted API key; copying the key with whitespace or a missing 'xi-' prefix; a key from the wrong environment/account.

Common situations: Key rotated on the ElevenLabs dashboard but not updated in config; typos from manual copy-paste; keys revoked after leaving a team workspace.

Related errors


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