I’m running a self-hosted agent in a Docker contai...
# ask-ai
g
I’m running a self-hosted agent in a Docker container and I want to send a POST request to create a session from an external system (like a PBX). Could you clarify: 1. Which endpoint should I target for a self-hosted agent running in Docker?
t
Thanks for reaching out, victor. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks for reaching out! To make sure I fully understand and can help you best, could you please clarify your goal a bit more? Are you looking for: 1. The internal HTTP or WebSocket endpoint exposed by the agent container for session initialization? 2. The LiveKit Server API used to trigger agent jobs? 3. A custom endpoint within your agent logic? Your answer will really help us find the right approach for your setup. Looking forward to your reply! Sources: README.md | livekit-plugins/livekit-plugins-simli/livekit/plugins/simli/avatar.py | plugins/anam/src/api.ts | Self-hosting the Ingress Service | LiveKit docs
g
Hi! To clarify my goal: • I have a self-hosted agent running in Docker. • I want to send an HTTP POST to an existing session to provide metadata, specifically the phone number from my mailing list. • I’m not trying to create a new session, nor use WebSockets or the Cloud API. • Could you clarify which endpoint and JSON fields I should use to update or pass this data to the agent session?
t
I'm glad you're reaching out about sending metadata like phone numbers to an existing session in a self-hosted LiveKit agent! The best way to do this is by including your data in the job metadata when you dispatch the job. Let me walk you through how to do this: Dispatch with Job Metadata You can use the
metadata
field in your dispatch request to send structured information such as phone numbers. Here's a simple example:
Copy code
lk dispatch create \
  --new-room \
  --agent-name your-agent \
  --metadata '{"phone_number": "+1234567890", "user_id": "user123", "user_name": "John Doe"}'
Just so you know, the metadata field expects a string, and using JSON is a great way to structure your data. Access Metadata in the Agent Inside your agent’s
entrypoint
function, you can parse this metadata and use it like this:
Copy code
import json

async def entrypoint(ctx: JobContext):
    # Parse job metadata
    dial_info = json.loads(ctx.job.metadata)
    phone_number = dial_info["phone_number"]
    user_id = dial_info.get("user_id")
    user_name = dial_info.get("user_name")

    # Use the data in agent logic
This way, your agent will have access to the phone number and any other metadata throughout the session. Alternative: Update Room Metadata If you find yourself needing to send or update data after the session has already started, you can use the
UpdateRoomMetadata
API like this:
Copy code
const roomService = new RoomServiceClient(LIVEKIT_URL, API_KEY, API_SECRET);
await roomService.updateRoomMetadata("my-room", '{"status": "call_connected"}');
Just a quick note: this updates the room’s metadata, not the agent’s job metadata. Important Notes • Keep in mind that job metadata can’t be changed once the session starts, so it’s best to include all the important info at dispatch time. • If you need to make updates dynamically during the session, using room or participant metadata updates is the way to go. If anything isn’t clear or you want to explore this further, please don’t hesitate to ask—I’m here to help! Sources: livekit/livekit_connector_whatsapp.pb.go | Job lifecycle | LiveKit Docs | Agent dispatch | LiveKit Docs | README.md | agent-console/app/api/room/update-metadata/route.ts
g
I’d like to clarify one more thing: • My agent is self-hosted in Docker, and I want to send phone number metadata from an external system (like a PBX) to a session. • Could you please clarify which HTTP endpoint exposed by the self-hosted agent I should use for this? • Is there a specific URL/path for updating job metadata or room metadata from an external POST, or should I use the LiveKit API endpoints directly?
t
Hi there! I wanted to share some helpful info about LiveKit agents, including those self-hosted in Docker. They don’t have built-in HTTP endpoints for external POST requests to update job or room metadata directly. Instead, the best way to update metadata is through the LiveKit server's RoomService API. Room Metadata Updates via LiveKit Server You can easily update room metadata by using the LiveKit server's
UpdateRoomMetadata
API. This involves calling the RoomService API directly. Here’s a simple example:
Copy code
const roomService = new RoomServiceClient(LIVEKIT_URL, API_KEY, API_SECRET);
await roomService.updateRoomMetadata("my-room", '{"status": "call_connected"}');
This method works great for updating room-wide metadata once a session is underway. Custom HTTP Endpoints in the Agent If you want to send metadata (like phone numbers) from another system to the agent, you can build a custom HTTP server inside your agent code. For instance: • Use a web framework such as FastAPI (Python) or Express (Node.js) to create your own endpoints. • Handle incoming POST requests to update the agent’s internal state or call the LiveKit server API to update room metadata. This way, you can tailor the solution to fit your specific needs and workflow. Job Metadata Limitations One important thing to keep in mind: job metadata can’t be changed once a session starts and must be set when dispatching. If you need to update data dynamically during a session, using room metadata updates or participant-level metadata is the way to go. Please feel free to reach out if you’d like more detailed help on setting up custom endpoints or working with the LiveKit API—I’m here to help! Sources: Job lifecycle | LiveKit Docs | Agent dispatch | LiveKit Docs | livekit/livekit_room.twirp.go | agent-console/lib/types.ts | agent/game_host.py
g
how can i configure the post method to acess my self-hosted ai agent on livekit?
t
Let's walk through how to configure a POST method to access your self-hosted AI agent from LiveKit together. Here’s a step-by-step guide to help you set it up smoothly: 1. Expose an HTTP Endpoint on Your AI Agent Your AI agent should have an HTTP endpoint ready to accept POST requests. The payload you send might look like this:
Copy code
{
  "livekit_url": "https://your-livekit-server.com",
  "livekit_token": "your-agent-jwt-token",
  "room_name": "your-room-name"
}
Feel free to add extra fields such as
image
,
agent_id
, or
model
if your setup requires them. 2. Generate a LiveKit JWT Token Within your LiveKit agent, you’ll want to generate a room join token using the
AccessToken
class like this:
Copy code
livekit_token = (
    api.AccessToken(api_key=livekit_api_key, api_secret=livekit_api_secret)
    .with_kind("agent")
    .with_identity("my-ai-agent")
    .with_grants(api.VideoGrants(room_join=True, room=ctx.room.name))
    .to_jwt()
)
This token is essential for authenticating your AI agent with LiveKit, so make sure to include it in your POST payload. 3. Send the POST Request To send the request, you can use an HTTP client like
aiohttp
or
httpx
. Here’s a simple example:
Copy code
async with httpx.AsyncClient() as client:
    response = await client.post(
        <YOUR_AI_AGENT_ENDPOINT>,
        json={
            "livekit_url": livekit_url,
            "livekit_token": livekit_token,
            "room_name": room_name
        }
    )
    response.raise_for_status()
4. Add Retry Logic Sometimes requests might fail temporarily, so adding retry logic can really help improve reliability. Here’s a basic pattern you can follow:
Copy code
for attempt in range(max_retries):
    try:
        response = await client.post(...)
        break
    except Exception:
        await asyncio.sleep(retry_interval)
5. Use Environment Variables For your sensitive data like API keys and URLs, it’s best to store them in environment variables. This keeps your setup secure and flexible. Important Notes: • LiveKit agents don’t come with built-in HTTP endpoints for external POST requests, so you’ll need to add this functionality within your AI agent yourself. • Keep in mind that job metadata can’t be changed after dispatch, so for any dynamic updates, consider using room or participant metadata instead. If you have any questions or want to dive deeper into any part of this, just let me know—I’m here to help! Sources: livekit-plugins/livekit-plugins-simli/livekit/plugins/simli/avatar.py | Deploying to custom environments | LiveKit Docs
g
which endpoints i can use to send a post method to my self hosted agent?
t
Hi there! I wanted to share some helpful info about how LiveKit agents work with POST requests. LiveKit agents don’t have built-in HTTP endpoints to receive POST requests directly. Instead, they’re designed to interact within LiveKit rooms and respond to real-time events or data sent through the LiveKit ecosystem. Here are some friendly options for sending data to an agent: • Use the LiveKit RoomService API: You can send data to the room where the agent is active using the
sendData
method. For example, the
RoomServiceClient
lets you send a message to the room, and the agent can pick it up and process it. • Custom HTTP Server: If you want to send POST requests directly to the agent, you could add a custom HTTP server inside the agent’s code (like with FastAPI or Express). This way, your agent can handle incoming requests and take action. • External API Integration: Agents can also send POST requests to external APIs. You might have seen this in plugins like Anam, Bithuman, and Simli, where the agent acts as an HTTP client instead of a server. If your goal is to trigger an action on the agent from an external system, the best way is to send data into the LiveKit room using the
RoomServiceClient
, or to set up a custom HTTP server within the agent. Please feel free to reach out if you’d like some guidance on either of these approaches—I’m here to help! Sources: plugins/anam/src/api.ts | livekit-plugins/livekit-plugins-simli/livekit/plugins/simli/avatar.py | agent-console/app/api/room/send-data/route.ts | Egress API | LiveKit Docs | SIP APIs | LiveKit Docs
g
• Use the LiveKit RoomService API: You can send data to the room where the agent is active using the
sendData
method. For example, the
RoomServiceClient
lets you send a message to the room, and the agent can pick it up and process it. tell me more about it, the room need to be the same every call? or no
t
The
RoomServiceClient.sendData
method in LiveKit is a wonderful tool that lets you send data messages to participants in a specific room. When you use
sendData
, you’ll just need to specify the room name in the
room
parameter each time. Room Flexibility: • The room doesn’t have to be the same every time you call it. You can easily send data to different rooms by just changing the room name in each call. • This gives you great flexibility to reach multiple rooms dynamically, depending on what you need. If you’d like a hand with implementing this or managing multiple rooms, please don’t hesitate to ask—I’m here to help! Sources: packages/livekit-server-sdk/src/RoomServiceClient.ts | src/main/kotlin/io/livekit/server/RoomServiceClient.kt | livekit-api/src/services/room.rs
g
on production it's not the perfect scenario, bc every differente call i'll have will generate a different room
t
In production, managing dynamically generated rooms for each call is something many encounter, and I’m here to help you navigate it smoothly. Let’s walk through some best practices that can make this process easier and more efficient for you: 1. Room Creation • LiveKit is designed to be smart—it can automatically create a room when the first participant joins using a valid token. This means you don’t always have to worry about creating rooms explicitly, which helps reduce delays. • If you’d rather have more control, you can use the
createRoom
API method. This lets you set important parameters like
empty_timeout
and
max_participants
to fit your needs. 2. Room Cleanup • It’s a good idea to set an appropriate
empty_timeout
so rooms close automatically once everyone leaves, keeping things tidy without extra effort. • And if you ever need to manually clean up, the
deleteRoom
API is there for you. 3. Room Metadata • Don’t forget, you can store application-specific details like call info in the room metadata. This data stays in sync for all participants and can be updated as things change. 4. Scaling and Multi-Node Setup • For larger setups, configuring Redis helps with distributed room management and balances the load automatically across nodes. • Also, keep in mind that each room lives on a single node, so planning room sizes accordingly will keep everything running smoothly. 5. Monitoring and Observability • Keeping an eye on metrics like the number of active rooms, participants per room, and how long rooms last can give you great insights. • Setting up alerts for unusual activity, such as rapid room creation or rooms staying open too long, can help you catch issues early. If you’d like, I’m here to dive deeper into any of these points or help tailor advice specifically for your setup. Just let me know! Sources: Core Platform Architecture & Authentication | livekit/livekit_composite | D... | pkg/service/roommanager.go | Distributed Setup | LiveKit docs | livekit-api/livekit/api/room_service.py | README.md
g
if i prefer pass to a n8n with livekit key, secret and url and after it pass to my agent, it's possible?
t
Absolutely, you can use n8n to pass LiveKit API keys, secrets, and URLs to your agent! The exact approach really depends on your deployment environment, so let’s walk through it together: 1. LiveKit Cloud Deployments • With LiveKit Cloud, the
LIVEKIT_URL
,
LIVEKIT_API_KEY
, and
LIVEKIT_API_SECRET
are automatically provided as environment variables to your agents at runtime. These credentials are managed by LiveKit itself, so they can’t be manually set or overridden through n8n. 2. Custom/Self-Hosted Deployments • If you’re working with custom environments like Docker, Kubernetes, or Fly.io, you’ll need to provide these credentials manually as environment variables. Here’s how n8n can help make this smoother: ◦ Orchestrating deployment workflows that set environment variables (for example, with
fly secrets set
). ◦ Managing the injection of environment variables during container deployment. ◦ Coordinating CI/CD pipelines to securely pass along your credentials. ◦ Automating credential rotation to keep your setup secure across environments. Security Best Practices • It’s super important to never hardcode credentials in your source code or config files. • Always use environment variables for sensitive information. • And keep your secrets out of version control by using proper secrets management tools. If you’d like, I’m here to help you with implementing this setup or answer any specific questions about your deployment platform. Just let me know! Sources: Server-Side SDKs & Authentication | livekit/livekit_composite | DeepWiki | fly.io/README.md | Secrets management | LiveKit Docs | CLI Tools & Token Management | livekit/livekit_composite | DeepWiki | Community & Support | livekit/livekit_composite | DeepWiki