how i can set the ElevenLabs api key in the env fi...
# ask-ai
s
how i can set the ElevenLabs api key in the env file and how can i use it for tts ?
t
Thanks for reaching out, Stefano. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to set your ElevenLabs API key in a
.env
file and use it for TTS. Below are concise, runnable steps for both Python and Node.js. 1) Add the API key to
.env
• Create/edit a
.env
in your project root and add:
Copy code
ELEVEN_API_KEY=your_actual_api_key_here
• Make sure
.env
is in
.gitignore
. 2) Install the ElevenLabs plugin • Python:
Copy code
uv add "livekit-agents[elevenlabs]~=1.2"
• Node.js:
Copy code
pnpm add @livekit/agents-plugin-elevenlabs@1.x
3) Use the key with the plugin (the plugin reads
ELEVEN_API_KEY
from the environment) • Python example:
Copy code
from livekit.agents import AgentSession
  from livekit.plugins import elevenlabs

  session = AgentSession(
      tts=elevenlabs.TTS(
          model="eleven_multilingual_v2",
          voice_id="ODq5zmih8GrVes37Dizd"
      ),
      # other components...
  )
• Node.js example:
Copy code
import * as elevenlabs from '@livekit/agents-plugin-elevenlabs';
  import * as voice from '@livekit/agents'; // adjust import to your setup

  const session = new voice.AgentSession({
      tts: new elevenlabs.TTS({
          voice: { id: "ODq5zmih8GrVes37Dizd" },
          model: "eleven_multilingual_v2"
      }),
      // other components...
  });
Notes: • The plugin will automatically use
ELEVEN_API_KEY
from your environment. •
voice_id
and
model
are optional; defaults exist if you don’t set them. • If you want, I can show how to load
.env
in your runtime (e.g.,
python-dotenv
or
dotenv
for Node) or provide a minimal example that sends text and receives audio. Which would you prefer? Sources: Secrets management | LiveKit docs | ElevenLabs TTS plugin guide | LiveKit Docs
s
are you sure i shouldn't set ELEVENLABS_API_KEY instead ?
t
Thanks — use ELEVEN_API_KEY. LiveKit’s ElevenLabs plugin reads
ELEVEN_API_KEY
from the environment. If you set
ELEVENLABS_API_KEY
instead, the plugin won’t pick it up unless you explicitly pass that value in code. Would you like a quick example of loading
.env
into your runtime? Sources: ElevenLabs TTS plugin guide | LiveKit Docs | Deploying to custom environments | LiveKit Docs
s
show me how to pass it in code
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
s
i get the following error after i did what you said: Error in _tts_inference_task Traceback (most recent call last): File "C:\Dev\python\livekit-voice-agent\.venv\Lib\site-packages\livekit\agents\inference\tts.py", line 472, in _run await asyncio.gather(*tasks) File "C:\Dev\python\livekit-voice-agent\.venv\Lib\site-packages\livekit\agents\inference\tts.py", line 459, in _recv_task raise APIError(f"LiveKit TTS returned error: {msg.data}") livekit.agents._exceptions.APIError: LiveKit TTS returned error: {"type":"error","code":"CONNECTION_ERROR","data":"websocket: close 1008 (policy violation): A voice with voice_id gfKKsLN1k0oYYN9n2dXX does not exist."} (body=None, retryable=True) The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Dev\python\livekit-voice-agent\.venv\Lib\site-packages\livekit\agents\utils\log.py", line 16, in async_fn_logs return await fn(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Dev\python\livekit-voice-agent\.venv\Lib\site-packages\livekit\agents\voice\generation.py", line 288, in _tts_inference_task pushed_duration += await _tts_node_inference(input_segment, pushed_duration) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Dev\python\livekit-voice-agent\.venv\Lib\site-packages\opentelemetry\util\_decorator.py", line 71, in async_wrapper return await func(*args, **kwargs) # type: ignore ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Dev\python\livekit-voice-agent\.venv\Lib\site-packages\livekit\agents\voice\generation.py", line 246, in _tts_node_inference async for audio_frame in tts_node: File "C:\Dev\python\livekit-voice-agent\.venv\Lib\site-packages\livekit\agents\voice\agent.py", line 447, in tts_node async for ev in stream: File "C:\Dev\python\livekit-voice-agent\.venv\Lib\site-packages\livekit\agents\tts\tts.py", line 572, in anext raise exc # noqa: B904 ^^^^^^^^^ File "C:\Dev\python\livekit-voice-agent\.venv\Lib\site-packages\livekit\agents\tts\tts.py", line 362, in _traceable_main_task await self._main_task() File "C:\Dev\python\livekit-voice-agent\.venv\Lib\site-packages\livekit\agents\tts\tts.py", line 396, in _main_task await self._run(output_emitter) File "C:\Dev\python\livekit-voice-agent\.venv\Lib\site-packages\livekit\agents\inference\tts.py", line 487, in _run raise APIConnectionError() from e livekit.agents._exceptions.APIConnectionError: Connection error. (body=None, retryable=True)