why is my livekit server returning 401 unauthorize...
# ask-ai
f
why is my livekit server returning 401 unauthorized when asking for token? I have provided the right api key and urls. And sometimes it works
t
Thanks for reaching out, venu. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To get to the bottom of this together, could you share a bit more about your setup? For example: • Are you using a particular SDK or calling the API directly? • What environment are you working in (like local, staging, or production)? • Have you noticed any specific situations when the 401 error pops up? • If you’re comfortable, could you share the exact error message or logs (just make sure to leave out any sensitive info)?
f
python sdk the code is in prod
t
Hey there! The 401 Unauthorized error you’re seeing in the LiveKit Python SDK usually pops up because of some authentication hiccups. Let’s walk through the most common causes and how you can fix them: • Invalid or missing access token: Make sure your token is generated correctly, signed with your API secret, and includes all the necessary details like identity and room name. • Incorrect API credentials: Double-check that your
LIVEKIT_API_KEY
and
LIVEKIT_API_SECRET
are set up right in your environment or passed in properly. • Missing or invalid video grants: Your token needs to have video grants with
room_join
set to true and the
room
field filled out. • Token expiration: Tokens expire by default after 6 hours, so make sure yours hasn’t timed out. Give these a quick look in your production setup, and hopefully, that’ll clear things up. If you’re still stuck, just let me know—I’m here to help! Sources: replay/cloud_replay.twirp.go | protocol/unity_proto/UnityAPI.twirp.go | Authentication | LiveKit Docs | livekit-api/livekit/api/access_token.py
f
how to set expiration time of api keys
t
Hey there! In LiveKit, you actually set the expiration time for access tokens (not API keys) when you generate the token. Here’s a simple way to do it using the Python SDK:
Copy code
from livekit.api.access_token import AccessToken
import datetime

token = AccessToken(api_key, api_secret)
token.with_ttl(datetime.timedelta(hours=1))  # This sets the token to expire in 1 hour
token.with_identity("user")
token.with_grants(VideoGrants(room_join=True, room="my-room"))
jwt_token = token.to_jwt()
The
with_ttl
method lets you specify how long the token should last using a
datetime.timedelta
object. If you don’t set a TTL, tokens will expire after 6 hours by default. If you have any questions or want me to walk you through it, just let me know—I’m here to help! Sources: Authentication | LiveKit Docs | packages/livekit-server-sdk/README.md
f
what is the default time limit
t
Hey there! Just so you know, the default expiration time for access tokens in LiveKit is 6 hours. If you don’t set a TTL (time-to-live) when you create the token, it’ll automatically expire after that time. If you want to tweak this or have any other questions, feel free to ask—I’m here to help!
f
my session with agent lasts for 10 mins only
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!