Tried quick start but keeps giving me 401 error ho...
# ask-ai
c
Tried quick start but keeps giving me 401 error how do i make sure the keys are working properly
t
Thanks for reaching out, Jason. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm here to help you with that 401 error, which usually points to a hiccup with your API key or token authentication. Let’s walk through some steps together to make sure your keys are all set up correctly: 1. Verify API Key and Secret: Double-check that your
LIVEKIT_API_KEY
and
LIVEKIT_API_SECRET
are correctly configured in your environment variables or config files. If you’re in development mode, you can use the placeholder values:
devkey
for the API Key and
secret
for the API Secret. 2. Check Key Initialization: Make sure your application is properly loading these environment variables when it runs. 3. Validate Token Generation: Try generating a valid token using the LiveKit CLI with your API key and secret. Here’s an example command:
Copy code
lk token create \
    --api-key <your-api-key> --api-secret <your-api-secret> \
    --join --room my-first-room --identity user1 \
    --valid-for 24h
4. Test with Example App: You can also test your generated token by using the LiveKit example app at example.livekit.io to see if the connection works smoothly. 5. Review Logs: Take a look at your application logs for any unauthorized errors — they can give you clues if there’s something off with your API credentials. If you’re still running into trouble, please check that your server is running in development mode if you’re using those placeholder credentials, or confirm your production keys are correctly set up and haven’t been revoked. Feel free to reach out if you need more guidance—I’m here to support you! Sources: Mini tutorial: setting up a LiveKit Cloud project | README.md | examples/nextjs/.env.example | livekit-api/src/lib.rs | examples/src/frontdesk/calendar_api.test.ts
c
2025-10-31 013043,592 - ERROR asyncio - Task exception was never retrieved future: <Task finished name='Task-37' coro=<ConnectionPool.prewarm.<locals>._prewarm_impl() done, defined at /Users/jasonliuym/Clyra_interviewer/ai-code-interviewer/Livekit_testing/livekit-voice-agent/.venv/lib/python3.11/site-packages/livekit/agents/utils/connection_pool.py:165> exception=WSServerHandshakeError(RequestInfo(url=URL('wss://agent-gateway.livekit.cloud/v1/tts'), method='GET', headers=<CIMultiDictProxy('Host': 'agent-gateway.livekit.cloud', 'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpbmZlcmVuY2UiOnsicGVyZm9ybSI6dHJ1ZX0sInN1YiI6ImFnZW50IiwiaXNzIjoiQVBJV2Y0UE00eE1zU2VwIiwibmJmIjoxNzYxODkyMjQzLCJleHAiOjE3NjE4OTI4NDN9.E6foy37Itk756SIMp0Yj-pJr88S22EMS3SUNARTLiB4', 'Upgrade': 'websocket', 'Connection': 'Upgrade', 'Sec-WebSocket-Version': '13', 'Sec-WebSocket-Key': 'VkPQ/RyqvcNpd8Wm888sYw==', 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'User-Agent': 'Python/3.11 aiohttp/3.13.2')>, real_url=URL('wss://agent-gateway.livekit.cloud/v1/tts')), (), status=401, message='Invalid response status', headers=<CIMultiDictProxy('Content-Length': '54', 'Content-Type': 'text/plain; charset=utf-8', 'Date': 'Fri, 31 Oct 2025 063043 GMT', 'Vary': 'Origin')>)> Traceback (most recent call last): File "/Users/jasonliuym/Clyra_interviewer/ai-code-interviewer/Livekit_testing/livekit-voice-agent/.venv/lib/python3.11/site-packages/livekit/agents/utils/connection_pool.py", line 168, in _prewarm_impl conn = await self._connect(timeout=self._connect_timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/jasonliuym/Clyra_interviewer/ai-code-interviewer/Livekit_testing/livekit-voice-agent/.venv/lib/python3.11/site-packages/livekit/agents/utils/connection_pool.py", line 62, in _connect connection = await self._connect_cb(timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/jasonliuym/Clyra_interviewer/ai-code-interviewer/Livekit_testing/livekit-voice-agent/.venv/lib/python3.11/site-packages/livekit/agents/inference/tts.py", line 290, in _connect_ws ws = await asyncio.wait_for( ^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/jasonliuym/.pyenv/versions/3.11.10/lib/python3.11/asyncio/tasks.py", line 489, in wait_for return fut.result() ^^^^^^^^^^^^ File "/Users/jasonliuym/Clyra_interviewer/ai-code-interviewer/Livekit_testing/livekit-voice-agent/.venv/lib/python3.11/site-packages/aiohttp/client.py", line 1494, in send return self._coro.send(arg) ^^^^^^^^^^^^^^^^^^^^ File "/Users/jasonliuym/Clyra_interviewer/ai-code-interviewer/Livekit_testing/livekit-voice-agent/.venv/lib/python3.11/site-packages/aiohttp/client.py", line 1093, in _ws_connect raise WSServerHandshakeError( aiohttp.client_exceptions.WSServerHandshakeError: 401, message='Invalid response status', url='wss://agent-gateway.livekit.cloud/v1/tts' 2025-10-31 013043,597 - ERROR livekit.agents - AgentSession is closing due to unrecoverable error Traceback (most recent call last): File "/Users/jasonliuym/Clyra_interviewer/ai-code-interviewer/Livekit_testing/livekit-voice-agent/.venv/lib/python3.11/site-packages/livekit/agents/stt/stt.py", line 289, in _main_task
t
It looks like you're encountering a WSServerHandshakeError 401, which usually means the WebSocket handshake was rejected because the authentication credentials are missing or invalid. Let’s work through this together to get it sorted out: 1. Double-check your credentials: Please make sure your API key and secret are correctly entered. You can provide these either as parameters to the TTS constructor or set them as environment variables (
LIVEKIT_API_KEY
and
LIVEKIT_API_SECRET
). 2. Understand where credentials come from: The SDK looks for credentials in this order: ◦ Parameters passed directly to the TTS constructor ◦ Environment variables
LIVEKIT_INFERENCE_API_KEY
and
LIVEKIT_INFERENCE_API_SECRET
◦ If those aren’t found, it falls back to
LIVEKIT_API_KEY
and
LIVEKIT_API_SECRET
1. Check your Access Token: Make sure your token includes an inference grant (
perform: true
) when it’s generated, since this is essential for TTS operations. 2. Verify your deployment environment: Confirm that your deployment environment has the right
LIVEKIT_API_KEY
,
LIVEKIT_API_SECRET
, and
LIVEKIT_URL
variables properly set. If you’ve confirmed all of the above and the problem is still there, it might be worth checking your network connection or seeing if a proxy or firewall could be blocking the WebSocket connection. Please feel free to reach out if you need any more help—we’re here to support you! Sources: Core Platform Architecture & Authentication | livekit/livekit_composite | D... | livekit-plugins/livekit-plugins-sarvam/livekit/plugins/sarvam/tts.py | agents/src/inference/tts.ts | Server-Side SDKs & Authentication | livekit/livekit_composite | DeepWiki
c
◦ Environment variables
LIVEKIT_INFERENCE_API_KEY
and
LIVEKIT_INFERENCE_API_SECRET
What are these
t
Hi there! Just to help you understand better, the
LIVEKIT_INFERENCE_API_KEY
and
LIVEKIT_INFERENCE_API_SECRET
are special environment variables that help you connect securely with LiveKit's Inference services, like speech-to-text (STT), text-to-speech (TTS), and large language model (LLM) APIs. When you use the LiveKit Agents SDKs, these variables are automatically picked up to set up the inference clients—unless you’ve provided specific API key and secret details directly in your code. If these aren’t set, no worries! The system will use
LIVEKIT_API_KEY
and
LIVEKIT_API_SECRET
instead. This setup really helps you manage your credentials separately for inference services, giving you more control and flexibility with how you handle access and keys. If you have any questions or need a hand with this, feel free to ask—I’m here to help! Sources: livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/llm.py | agents/src/inference/stt.ts | Secrets management | LiveKit Docs
c
what ai key do i need
t
Hi there! When you're using LiveKit's inference services like Text-to-Speech (TTS) and Speech-to-Text (STT), you'll need your LiveKit API key and secret. These credentials are your gateway to accessing LiveKit’s unified inference platform, which offers top-notch models without the hassle of juggling multiple provider accounts. You have a couple of easy options to provide these credentials: • You can pass them directly as parameters when you initialize the service • Or, if you prefer, set them as environment variables:
LIVEKIT_API_KEY
and
LIVEKIT_API_SECRET
Also, if you’re using third-party services through LiveKit plugins like Cartesia, Deepgram, or Rime, you might need additional API keys from those providers. But don’t worry—for LiveKit’s own native inference services, just your LiveKit API key and secret will do the trick. Sources: Adrian Cowham - LiveKit Blog | Tom Shapland - LiveKit Blog | livekit-plugins/livekit-plugins-neuphonic/livekit/plugins/neuphonic/tts.py | Rime TTS plugin guide | LiveKit Docs | LiveKit Pricing
c
do i need the llm key?
t
Hi there! You actually don’t need a separate LLM-specific API key for LiveKit’s inference services. LiveKit simplifies things by using just one API key and secret pair (
LIVEKIT_API_KEY
and
LIVEKIT_API_SECRET
) to give you access to all its inference services, including LLM, STT, and TTS models. Behind the scenes, LiveKit takes care of managing the credentials for the underlying LLM providers like OpenAI and Google. So, all you need to do is provide your LiveKit API key and secret, which you can set as environment variables or include directly in your client configuration. Now, if you’re using plugins to connect directly to third-party LLM providers such as Azure OpenAI or AWS Bedrock, then you will need provider-specific API keys. Just keep in mind, this is separate from LiveKit’s managed inference service. Sources: livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/llm.py | agents/src/inference/llm.ts | Kimi LLM | LiveKit Docs | Qwen LLM | LiveKit Docs | livekit-api/src/lib.rs
c
can i use my own 3rd party keys
t
Absolutely, you can use your own third-party API keys with LiveKit if you'd like to connect directly with providers like OpenAI, Deepgram, or Cartesia. That said, for most situations, this step is optional because LiveKit’s unified inference gateway lets you access these services easily using just your LiveKit API key and secret. If you decide to use third-party keys, here’s a simple way to do it: • Set the provider’s API key as an environment variable (for example,
OPENAI_API_KEY
or
DEEPGRAM_API_KEY
). • Use the provider-specific plugins in your code, like
openai.LLM()
or
deepgram.STT()
, which will automatically pick up these keys from your environment. • Or, if you prefer, you can pass the API key directly when creating the plugin. Keep in mind, third-party keys are only necessary if you want to bypass LiveKit’s gateway or access certain features or models not available through it. For most of your needs, your LiveKit credentials will do the job just fine. Sources: Tom Shapland - LiveKit Blog | .env.example | .env.example | .env.example | AI Service Plugin System | livekit/livekit_composite | DeepWiki
c
how to do it
t
I'm happy to guide you through using your own third-party API keys with LiveKit! Here’s a simple way to get started: 1. Set the API Key as an Environment Variable: First, add your provider's API key to your environment variables. For example, you can do this in a
.env
file:
Copy code
OPENAI_API_KEY=your-openai-key
DEEPGRAM_API_KEY=your-deepgram-key
2. Use Provider-Specific Plugins: Next, in your code, use the right plugin for your provider. Here’s an example in JavaScript:
Copy code
import { openai, deepgram } from '@livekit/agents-plugin';

const session = new voice.AgentSession({
  stt: new deepgram.STT(), // This will use your DEEPGRAM_API_KEY
  llm: new openai.LLM()    // This will use your OPENAI_API_KEY
});
3. Pass the API Key Directly (Optional): If you’d rather, you can also pass the API key directly when creating the plugin instance:
const llm = new openai.LLM({ apiKey: 'your-openai-key' });
With these steps, the plugins will seamlessly use the API keys you provide to connect to the third-party services. If you have any questions or need further help, feel free to ask—I’m here to support you!