#I'm trying to switch my connection to my agent through react sdk from websockets to webrtc, but get
1 messages · Page 1 of 1 (latest)
AI-generated Answer for pepito_12781
Likely cause: the SDK is not receiving a valid agent identifier when starting the session. In your console you can see the client trying to open a WebSocket with agent_id=undefined, which happens when neither a valid conversationToken nor an agentId is provided to startSession.
What to check and fix
- If your agent is private, your backend must return the raw WebRTC conversation token string
- Server (Node/Express):
- Call the WebRTC token endpoint with your agent_id and XI API key.
- Return body.token as plain text (not JSON).
Example (from docs):
app.get('/conversation-token', yourAuthMiddleware, async (req, res) => {
const r = await fetch(https://api.elevenlabs.io/v1/convai/conversation/token?agent_id=${process.env.AGENT_ID}, {
headers: { 'xi-api-key': process.env.ELEVENLABS_API_KEY },
});
if (!r.ok) return res.status(500).send('Failed to get conversation token');
const body = await r.json();
res.send(body.token); // IMPORTANT: send just the token string
});
Then on the client:
const response = await fetch('/conversation-token', yourAuthHeaders);
const conversationToken = await response.text(); // expects a raw token string
await Conversation.startSession({ conversationToken, connectionType: 'webrtc' });
If you prefer to return JSON from your server, then parse it on the client (const { token } = await response.json();). Docs: WebRTC example and token endpoint. (elevenlabs.io)
- Ensure you’re calling the WebRTC token endpoint (not the WebSocket signed URL route)
- WebRTC uses GET /v1/convai/conversation/token (requires agent_id).
- WebSocket uses get-signed-url and won’t work for WebRTC. (elevenlabs.io)
- Verify your agent_id is set and valid when minting the token
- The token request requires agent_id. If process.env.AGENT_ID is missing, ElevenLabs can’t issue a usable token for your agent. (elevenlabs.io)
- Token freshness
- conversationToken is short‑lived (10 minutes). Mint it right before starting the session. (elevenlabs.io)
- Quick isolation test (for public agents)
- If your agent is public, skip the token and start directly with:
await conversation.startSession({ agentId: '<your-agent-id>', connectionType: 'webrtc' });
If this works, your client setup is fine and the issue is with your token route. (elevenlabs.io)
Reference docs
- React/JS SDK startSession with conversationToken and WebRTC server/client examples. (elevenlabs.io)
- WebRTC conversation token API (required agent_id). (elevenlabs.io)
- React SDK: startSession requires one of signedUrl, conversationToken, or agentId; public agents can use just agentId. (elevenlabs.io)
- Token validity (10 minutes). (elevenlabs.io)
If you still see agent_id=undefined after these changes, log what your /conversation-token route returns and confirm it’s a non-empty token string (not JSON, not quoted JSON) before passing it to startSession.
it does work with public agents, but not with my private one even if i generate correclty the token, why?