{"record":{"id":"560cc0bc04c63cdc","repo":"harry0703/MoneyPrinterTurbo","slug":"elevenlabs-api-key-was-rejected-401-safe-resp","errorCode":null,"errorMessage":"ElevenLabs API key was rejected (401): {_safe_response_error(response)}","messagePattern":"ElevenLabs API key was rejected \\(401\\): (.+?)","errorType":"exception","errorClass":"ElevenLabsAuthenticationError","httpStatus":401,"severity":"error","filePath":"app/services/elevenlabs_music.py","lineNumber":123,"sourceCode":"    使用不消耗音乐生成额度的订阅接口检查 API Key 和账号套餐。\n\n    该接口只能确认 Key 可访问订阅信息以及账号不是免费套餐，不能证明当前 Key\n    一定拥有 Music endpoint 权限。ElevenLabs 允许按 endpoint、额度和 IP 限制\n    Key，因此 UI 成功提示必须保留这一边界，实际权限仍由生成请求最终确认。\n    响应中的账单和用量详情不会写入日志，避免记录账号隐私。\n    \"\"\"\n    api_key = get_api_key()\n    if not api_key:\n        raise ElevenLabsAuthenticationError(\"ElevenLabs API key is required\")\n    try:\n        with requests.get(\n            f\"{_base_url()}{SUBSCRIPTION_PATH}\",\n            headers={\"xi-api-key\": api_key},\n            timeout=(15, 30),\n            stream=True,\n        ) as response:\n            if response.status_code == 401:\n                raise ElevenLabsAuthenticationError(\n                    \"ElevenLabs API key was rejected (401): \"\n                    f\"{_safe_response_error(response)}\"\n                )\n            if not response.ok:\n                raise ElevenLabsMusicError(\n                    \"ElevenLabs account check failed \"\n                    f\"({response.status_code}): \"\n                    f\"{_safe_response_error(response)}\"\n                )\n            try:\n                payload = response.json()\n            except ValueError as exc:\n                raise ElevenLabsMusicError(\n                    \"ElevenLabs returned an invalid subscription response\"\n                ) from exc\n    except requests.RequestException as exc:\n        raise ElevenLabsMusicError(\n            f\"failed to connect to ElevenLabs: {exc}\"","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/harry0703/MoneyPrinterTurbo/blob/1f9f19c2021a68d04df228f33e9099a0c947f6f8/app/services/elevenlabs_music.py#L105-L141","documentation":"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.","triggerScenarios":"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.","commonSituations":"Key rotated on the ElevenLabs dashboard but not updated in config; typos from manual copy-paste; keys revoked after leaving a team workspace.","solutions":["Regenerate the key on the ElevenLabs dashboard and update config.elevenlabs.api_key / ELEVENLABS_API_KEY.","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.","Ensure no stray quotes/whitespace were pasted along with the key."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"import re\n\ndef looks_like_elevenlabs_key(key: str) -> bool:\n    return bool(key) and bool(re.fullmatch(r'[A-Za-z0-9_-]{20,}', key.strip()))","typeGuard":null,"tryCatchPattern":"try:\n    test_connection()\nexcept ElevenLabsAuthenticationError as e:\n    # covers both 'is required' and 'rejected (401)'\n    invalidate_saved_key(); prompt_reentry()","preventionTips":["Test the key right after saving it in the UI, not only at generation time.","Copy keys with the clipboard button — manual copy-paste is the top source of malformed keys.","Rotate keys in config as soon as they are regenerated on the dashboard."],"tags":["elevenlabs","authentication","http-401","api-key"],"backgroundTag":null,"analyzedSha":"1f9f19c2021a68d04df228f33e9099a0c947f6f8","analyzedAt":"2026-08-14T19:41:05.568Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}