Skip to content

Commit 18e76d2

Browse files
kraenhansenclaude
andcommitted
fix: resolve api_key from env var with runtime assertion
The upcoming SDK regeneration (#761) makes api_key required in the generated base client. This updates the wrapper to resolve api_key from the parameter or ELEVENLABS_API_KEY env var, raising a clear error if neither is set — keeping the public API backwards-compatible. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9f0632b commit 18e76d2

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

src/elevenlabs/client.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ def get_base_url_host(base_url: str) -> str:
1919
return httpx.URL(base_url).host
2020

2121

22+
def _resolve_api_key(api_key: typing.Optional[str]) -> str:
23+
resolved = api_key or os.getenv("ELEVENLABS_API_KEY")
24+
if not resolved:
25+
raise ValueError(
26+
"Please pass in your ElevenLabs API Key or export ELEVENLABS_API_KEY in your environment."
27+
)
28+
return resolved
29+
30+
2231
class ElevenLabs(BaseElevenLabs):
2332
"""
2433
Use this class to access the different functions within the SDK. You can instantiate any number of clients with different configuration that will propogate to these functions.
@@ -47,14 +56,15 @@ def __init__(
4756
*,
4857
base_url: typing.Optional[str] = None,
4958
environment: ElevenLabsEnvironment = ElevenLabsEnvironment.PRODUCTION,
50-
api_key: typing.Optional[str] = os.getenv("ELEVENLABS_API_KEY"),
59+
api_key: typing.Optional[str] = None,
5160
timeout: typing.Optional[float] = 240,
5261
httpx_client: typing.Optional[httpx.Client] = None
5362
):
63+
resolved_api_key = _resolve_api_key(api_key)
5464
super().__init__(
5565
base_url=base_url,
5666
environment=environment,
57-
api_key=api_key,
67+
api_key=resolved_api_key,
5868
timeout=timeout,
5969
httpx_client=httpx_client
6070
)
@@ -93,14 +103,15 @@ def __init__(
93103
*,
94104
base_url: typing.Optional[str] = None,
95105
environment: ElevenLabsEnvironment = ElevenLabsEnvironment.PRODUCTION,
96-
api_key: typing.Optional[str] = os.getenv("ELEVENLABS_API_KEY"),
106+
api_key: typing.Optional[str] = None,
97107
timeout: typing.Optional[float] = 240,
98108
httpx_client: typing.Optional[httpx.AsyncClient] = None
99109
):
110+
resolved_api_key = _resolve_api_key(api_key)
100111
super().__init__(
101112
base_url=base_url,
102113
environment=environment,
103-
api_key=api_key,
114+
api_key=resolved_api_key,
104115
timeout=timeout,
105116
httpx_client=httpx_client
106117
)

0 commit comments

Comments
 (0)