how do I decode the token to know the userid / etc...
# ask-ai
n
how do I decode the token to know the userid / etc on the python agent side
t
Thanks for reaching out, rohit. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're looking to decode a token and extract the user ID or other details on the Python agent side! You can do this easily using the
TokenVerifier
class from the
livekit.api
module. Let me walk you through the steps: 1. Start by initializing
TokenVerifier
with your
LIVEKIT_API_KEY
and
LIVEKIT_API_SECRET
. 2. Next, call
verify(token)
with your JWT string. This will decode the token and give you a
Claims
object. 3. From the
Claims
object, you can then extract helpful information like
identity
,
metadata
,
video
grants, or
attributes
. Here’s a simple example to illustrate:
Copy code
from livekit.api import TokenVerifier

# Initialize the verifier with API key and secret
verifier = TokenVerifier(api_key="your_api_key", api_secret="your_api_secret")

# Decode and verify the token
claims = verifier.verify("your.jwt.token.here")

# Extract user ID and other data
user_id = claims.identity
user_metadata = claims.metadata
video_grants = claims.video
attributes = claims.attributes

print(f"User ID: {user_id}")
print(f"Metadata: {user_metadata}")
print(f"Attributes: {attributes}")
If you ever just want to peek inside the token without verifying it, you can use
jwt.decode(..., verify_signature=False)
. However, I’d recommend against this for anything security-related, since verifying with your
api_secret
is the safest way to make sure the token’s trustworthy. Please don’t hesitate to reach out if you have any questions or need a hand with this—I’m here to help! Sources: livekit-api/tests/test_access_token.py | livekit-agents/livekit/agents/jupyter.py | Job lifecycle | LiveKit Docs | Agent dispatch | LiveKit Docs