How can I get a token to use? Is the same as API k...
# ask-ai
s
How can I get a token to use? Is the same as API key?
t
Thanks for reaching out, Raymond. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hi there! I’d love to help clarify the difference between tokens and API keys in LiveKit—it can definitely be a bit confusing at first. • API keys are your static credentials that help authenticate your backend server when creating tokens. It’s really important to keep these safe and never expose them in your frontend code. • Access tokens are temporary JWTs made for each user. They carry details like the user’s identity, the room they can join, their permissions, and when the token expires. When it comes to generating tokens, here are a couple of friendly ways to do it: 1. You can use the LiveKit CLI during development, like this:
Copy code
lk token create \
    --api-key devkey --api-secret secret \
    --join --room my-first-room --identity user1 \
    --valid-for 24h
2. For production, I recommend using a Server SDK to create a backend endpoint that generates tokens securely. We have great examples for Node.js, Python, Go, and more to get you started. Remember, always generate tokens on your server—not in frontend code—to keep things secure. If you’d like, I’m here to provide more details or examples for any specific SDK you’re working with. Just let me know! Sources: rover/auth.py | Web and mobile frontends | LiveKit Docs | README.md | Generating tokens | LiveKit Docs | web/app/api/connection-details/route.ts
s
i'm using the react sdk
t
When you're working with the LiveKit React SDK, you'll want to provide a token to the
LiveKitRoom
component so it can connect to a room smoothly. Here's a simple way to do that: 1. Pass the
token
and
serverUrl
props to the
LiveKitRoom
component. For example:
Copy code
<LiveKitRoom 
  serverUrl="wss://your-livekit-server.com" 
  token="your-access-token" 
  connect={true}
>
  {/* Child components */}
</LiveKitRoom>
2. You'll need to generate the token on your backend using your LiveKit API key and secret. If you're just getting started, the LiveKit CLI or token server can be really handy for development. 3. If you'd like, you can manage tokens in React using context or hooks. For instance, a custom hook like
useToken
can help you fetch a token dynamically from your backend before connecting. Feel free to reach out if you'd like a hand with generating tokens or integrating them into your React app—I’m here to help! Sources: examples/nextjs/pages/processors.tsx | React quickstart | LiveKit Docs | Next.js 12 quickstart | LiveKit Docs | src/components/token-context.tsx | README.md
s
so it doesn't matter of im using cloud? anyways how does the cli work
t
Thanks for reaching out! Could you please let me know if you're planning to use the LiveKit CLI to generate tokens during development, for production, or specifically with LiveKit Cloud? That way, I can guide you with the best and most helpful instructions.
s
for development, and livekit cloud
t
I'm happy to help you generate tokens using the LiveKit CLI for both development and LiveKit Cloud environments. Let's walk through it together: For Development (Self-Hosted LiveKit Server) If you're working with a local LiveKit server during development, you can use the default API key and secret like this:
Copy code
API Key: devkey
API Secret: secret
Then, simply run this command to create your token:
Copy code
lk token create \
    --api-key devkey --api-secret secret \
    --join --room my-first-room --identity user1 \
    --valid-for 24h
For LiveKit Cloud First, you'll want to authenticate and connect your project by running:
Copy code
lk cloud auth
Once you're authenticated, you can generate a token like this:
Copy code
lk token create \
    --join --room my-first-room --identity user1 \
    --valid-for 24h
If you haven’t set up a default project yet, no worries! Just include your LiveKit Cloud URL along with your API credentials like this:
Copy code
lk token create \
    --url wss://your-project.livekit.cloud \
    --api-key <api-key> --api-secret <api-secret> \
    --join --room my-first-room --identity user1 \
    --valid-for 24h
If any part of this feels unclear or if you want me to guide you through a specific step, please don’t hesitate to ask. I’m here to help! Sources: cmd/server/commands.go | README.md | swift-frontend/VisionDemo/Services/TokenService.swift | VoiceAgent/Auth/TokenService.swift | lib/services/token_service.dart
s
gimme an example of the token
t
Hi there! A LiveKit token is a JWT-based access token that carries important details like the participant's identity, the room name, permissions, and when the token expires. Here’s an example of what an encoded token looks like:
Copy code
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoibmFtZSIsInZpZGVvIjp7InJvb21Kb2luIjp0cnVlLCJyb29tIjoibXktcm9vbSIsImNhblB1Ymxpc2giOnRydWV9LCJzdWIiOiJpZGVudGl0eSIsImlzcyI6Im15YXBpa2V5IiwibmJmIjoxNzQxODY5Njc5LCJleHAiOjE3NDE4OTEyNzl9.9_eOcZ1RNaRXxwdU36LTYoxsHtv_IhfhLk-kTd8MDY4
This token lets the participant join a room called "my-room" and publish media there. If you'd like, I’m here to help you create a token tailored to your specific needs—just let me know! Sources: Authentication | LiveKit Docs | examples/send_bytes/README.md | complex-agents/note-taking-assistant/note-taker-frontend/app/api/connection...
s
what about the serverUrl for the sdk, how do I get that
t
The
serverUrl
for the LiveKit SDK really depends on how you’ve set things up, so let’s walk through it together: • For LiveKit Cloud: You’ll want to use
wss://<your-project>.livekit.cloud
. Just swap out
<your-project>
with your actual project name. • For a self-hosted LiveKit Server: Use
wss://your-domain.com
if you’re in production, or
ws://localhost:7880
if you’re working locally on your machine. A handy tip is to keep the
serverUrl
in an environment variable like
LIVEKIT_URL
, or you can fetch it from your backend along with the access token. If you’d like, I’m here to help you set this up—just let me know! Sources: docs/interfaces/LiveKitRoomProps.html | README.md | frontend/lib/server-utils.ts | .env.example | examples/nextjs/.env.example
s
what happens when I do this
Copy code
<LiveKitRoom token="<livekit-token>" serverUrl="<url-to-livekit-server>" connect={true}>
  ...
</LiveKitRoom>
gimme example of the token and serverurl, im using cloud project also, would this work just as it is after i passed the variables?
t
The
LiveKitRoom
component will work perfectly as long as you provide a valid
token
and
serverUrl
, and have
connect
set to
true
(which is the default). Let me share a simple example for a LiveKit Cloud project to help you along: Example serverUrl:
wss://myproject.livekit.cloud
(just replace
myproject
with your actual project name from the LiveKit Cloud dashboard). Example token: This is a JWT-style access token generated on the server side. For instance:
Copy code
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoibmFtZSIsInZpZGVvIjp7InJvb21Kb2luIjp0cnVlLCJyb29tIjoibXktcm9vbSIsImNhblB1Ymxpc2giOnRydWV9LCJzdWIiOiJpZGVudGl0eSIsImlzcyI6Im15YXBpa2V5IiwibmJmIjoxNzQxODY5Njc5LCJleHAiOjE3NDE4OTEyNzl9.9_eOcZ1RNaRXxwdU36LTYoxsHtv_IhfhLk-kTd8MDY4
Example usage:
Copy code
<LiveKitRoom 
  token="your-access-token" 
  serverUrl="wss://your-project.livekit.cloud" 
  connect={true}
>
  <VideoConference />
</LiveKitRoom>
If you’d like, I’m here to help you with generating the token or setting up the serverUrl—just let me know! Sources: src/components/LiveKitRoom.tsx | examples/nextjs/pages/processors.tsx | Connecting to LiveKit | LiveKit Docs | Mini tutorial: setting up a LiveKit Cloud project | App.tsx