can i use this token generation service within the...
# ask-ai
p
can i use this token generation service within the frontend, which is requesting tokens too? import express from "express"; import cors from "cors"; import { AccessToken } from "livekit-server-sdk"; import dotenv from "dotenv"; dotenv.config(); const app = express(); app.use(cors()); app.use(express.json()); const apiKey = process.env.LIVEKIT_API_KEY; const apiSecret = process.env.LIVEKIT_API_SECRET; if (!apiKey || !apiSecret) { throw new Error("LIVEKIT_API_KEY and LIVEKIT_API_SECRET must be set in environment"); } app.post("/token", async (req, res) => { const { identity, room } = req.body; if (!identity || !room) { return res.status(400).json({ error: "Missing identity or room" }); } const at = new AccessToken(apiKey, apiSecret, { identity }); at.addGrant({ roomJoin: true, room, canPublish: true, canSubscribe: true, }); // toJwt() is async now — await the promise try { const token = await at.toJwt(); res.json({ token }); } catch (err) { console.error("Error generating token:", err); res.status(500).json({ error: "Failed to generate token" }); } }); const port = process.env.PORT || 3000; app.listen(port, () => { console.log(
:white_check_mark: Token server running on port ${port}
); });
t
Thanks for reaching out, Vaneeza. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I totally understand where you're coming from! Just a friendly heads-up: the token generation service you have is best kept away from the frontend since it uses your API key and secret, which are super important to keep safe on your backend server. The best way to handle this is by creating a backend endpoint that generates the tokens. Your frontend can then securely fetch the tokens from there, keeping your sensitive info nicely protected. If you'd like, I’d be more than happy to walk you through setting up that backend service or show you how to connect it with your frontend. Just let me know—I'm here to help! Sources: Server-Side SDKs & Authentication | livekit/livekit_composite | DeepWiki | Core Platform Architecture & Authentication | livekit/livekit_composite | D... | Generating tokens | LiveKit Docs | README.md | README.md
p
can i use it to generate tokens for different web agents?
t
Absolutely! The token generation service is designed to help you create tokens for different web agents. You can easily customize the tokens by including details like the agent name and other specific settings when you generate them. If you'd like, I’m here to help you with the implementation or guide you through configuring tokens for your particular agents. Just let me know, and I’ll be happy to provide some examples or walk you through the process.
p
yes currently any agent can enter any room. i want it to handle different agents within the same livekit project
t
To help you manage different agents within the same LiveKit project and control room access, you can use agent dispatch configurations along with access token permissions. I’m here to guide you through it! Here’s how you can do it step-by-step: 1. When generating your token, use the
room_config
parameter to specify which agents are allowed in a particular room. For example, you might set up agent-specific configurations like this:
Copy code
at.roomConfig = new RoomConfiguration({
  agents: [
    new RoomAgentDispatch({
      agentName: "specific-agent-name",
      metadata: "additional-metadata"
    })
  ]
});
1. Next, control access to the room by assigning the right
VideoGrant
permissions within your access token. Here’s an example:
Copy code
const grant = {
  room: roomName,
  roomJoin: true,
  canPublish: true,
  canSubscribe: true,
};
at.addGrant(grant);
This way, only the agents with the correct setup will be able to join specific rooms. If you’d like, I’m more than happy to help you implement this or discuss any particular needs you have—just let me know! Sources: src/pages/api/token.ts | client/web/src/app/api/connection-details/route.ts | Voice Assistant Applications | livekit/livekit_composite | DeepWiki | Server-Side SDKs & Authentication | livekit/livekit_composite | DeepWiki | test/agent_test.go