token = api.AccessToken(os.getenv('LIVEKIT_API_KEY...
# ask-ai
l
token = api.AccessToken(os.getenv('LIVEKIT_API_KEY'), os.getenv('LIVEKIT_API_SECRET')) \ .with_identity("identity") \ .with_name("name") \ .with_metadata(metadata=json.dumps(metadata))\ .with_grants(api.VideoGrants( room_join=True, room=room_name)) modify token creating funtion with publishing and listening other tracks in a room
t
Thanks for reaching out, Rajadurai. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — you're asking to modify your token creation so the participant can both publish their own tracks and subscribe (listen) to others in a room. I see your original Python snippet; below is a corrected and complete example that adds those permissions.
Copy code
import os
import json
from livekit import api  # ensure this import matches your SDK

metadata = {"example": "value"}  # replace with your metadata dict
room_name = "my-room"            # replace with your room name

token = api.AccessToken(
    os.getenv("LIVEKIT_API_KEY"),
    os.getenv("LIVEKIT_API_SECRET")
).with_identity("identity") \
 .with_name("name") \
 .with_metadata(metadata=json.dumps(metadata)) \
 .with_grants(api.VideoGrants(
     room_join=True,
     room=room_name,
     can_publish=True,     # allow publishing local audio/video/data tracks
     can_subscribe=True    # allow subscribing to remote tracks
 ))
Key points: • Use
can_publish=True
to allow the participant to publish their own audio/video/data tracks. • Use
can_subscribe=True
to allow the participant to receive/play tracks published by others. • Replace
identity
,
name
,
metadata
, and
room_name
with your actual values. • Ensure the `livekit`/`api` import matches the LiveKit server SDK you have installed. If you'd like, I can: • Show an example that grants only publish for specific track types (audio/video) if your SDK supports that. • Provide a short server endpoint example that returns this token to a client. Which of those would help you next? Sources: Agent dispatch | LiveKit Documentation | server-sdk-go | livekit/livekit_composite | DeepWiki
l
In my room initialy a agent and participant-1 is connected. agent replied to participant-1 properly. when new participant (participat-2) connect to the room using this token. I want to stop the agent for speaking but lisenes to the tracks. how to do that
t
Thanks — I’m responding to your goal: when participant-2 joins, stop the agent from speaking (prevent publishing) but let the agent still listen (subscribe) to other tracks. You can do this without disconnecting the agent or changing its token by updating the agent’s live permissions with the server-side UpdateParticipant API. Quick plan: • Detect when participant-2 joins (webhook or server-side event). • Call UpdateParticipant for the agent and set publish to false while keeping subscribe true. Before I give a concrete code sample, which server SDK/language are you using on your server to call LiveKit admin APIs? (Node.js, Python, Go, other?) If you want, I can also: • Provide a webhook example to detect new joins, or • Show a client-side pattern (agent listens for participant-connected events and requests the server to update permissions). Tell me your preferred language and whether you want the webhook or SDK-triggered approach and I’ll send the exact code. Sources: server-sdk-js | livekit/livekit_composite | DeepWiki | server-sdk-kotlin | livekit/livekit_composite | DeepWiki | server-sdk-ruby | livekit/livekit_composite | DeepWiki | Server-side | livekit/livekit_composite | DeepWiki
l
I'm using python framework yes give me the updatepartipant
t
Thanks — I’m responding to your request for a Python example that uses UpdateParticipant to stop the agent from speaking (prevent publishing) while still allowing them to listen (subscribe). Below is a concise, runnable example using the LiveKit Python server SDK. It shows: • initializing the API client, • updating the agent’s permissions to disable publishing but keep subscribing enabled, • and closing the client. Replace the placeholders with your LiveKit host, API key/secret, room name, and agent identity.
Copy code
import asyncio
from livekit import api

async def disable_agent_publishing(
    host: str,
    api_key: str,
    api_secret: str,
    room_name: str,
    agent_identity: str
):
    # Initialize LiveKit API client
    lk = api.LiveKitAPI(
        url=host,
        api_key=api_key,
        api_secret=api_secret
    )

    # Build the update request to disable publishing but allow subscribing
    update_req = api.UpdateParticipantRequest(
        room=room_name,
        identity=agent_identity,
        permissions=api.ParticipantPermission(
            can_subscribe=True,      # keep listening to others
            can_publish=False,       # stop publishing (speaking)
            can_publish_audio=False, # explicitly disable audio publish
            can_publish_video=False, # explicitly disable video publish
            can_publish_data=False   # optional: disable data messages if desired
        )
    )

    # Send update
    await lk.room.update_participant(update_req)

    # Close client
    await lk.aclose()

# Example usage
if __name__ == "__main__":
    HOST = "<https://your-livekit-instance.com>"    # e.g., <https://cloud.livekit.io> or your self-hosted URL
    API_KEY = "YOUR_API_KEY"
    API_SECRET = "YOUR_API_SECRET"
    ROOM = "your-room-name"
    AGENT_ID = "agent-identity"

    asyncio.run(disable_agent_publishing(HOST, API_KEY, API_SECRET, ROOM, AGENT_ID))
Notes and tips: • This change applies immediately and does not require the agent to reconnect. The agent remains in the room and can continue subscribing to remote tracks. • If you only want to disable audio (so the agent can still publish data or video), set
can_publish_audio=False
and leave other publish flags as needed. • Trigger this function when you detect participant-2 joining — via LiveKit webhooks (participant_joined) or server-side room events. • To re-enable speaking later, call the same API with
can_publish=True
(and appropriate
can_publish_audio/video
flags). If you want, I can: • Provide an example webhook handler that calls this when participant-2 joins, or • Show the re-enable (unmute) flow and a minimal client-side notification for the agent. Which would you prefer? Sources: server-sdk-kotlin | livekit/livekit_composite | DeepWiki