how to config meeting room context via SIP?
# ask-ai
c
how to config meeting room context via SIP?
t
Thanks for reaching out, Meng. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to help you set up the meeting room context via SIP in LiveKit! You can do this using the
CreateSIPParticipant
API for outbound calls or by setting up SIP dispatch rules for inbound calls. Outbound SIP Calls: • You can specify the room context by using the
room_name
,
room_config
, or
room_preset
parameters. • Adding participant metadata and attributes is a great way to provide extra context. • Don't forget, custom SIP headers can also be used to pass context, and these can be mapped to participant attributes. Inbound SIP Calls: • Setting up SIP dispatch rules allows you to define the room context and route calls to the right rooms. • For more precise control, you can use the
room_config.agents
field to explicitly dispatch agents. If you'd like, I can share detailed examples or walk you through any part of this—just let me know! Sources: SIP APIs | LiveKit docs | protobufs/livekit_sip.proto | Agent dispatch | LiveKit Docs | test/lktest-sip-outbound/README.md
c
is it possible for inbound sip calls to set context by calling an api with the caller’s phone number
t
Thanks for reaching out! I wanted to share some helpful info about inbound SIP calls in LiveKit. While these calls can’t directly invoke an external API during the SIP dispatch phase to set context using the caller’s phone number, the great news is that the caller’s phone number is automatically captured as a participant attribute (
sip.phoneNumber
) once the call connects to a LiveKit room. You can definitely use this attribute to programmatically call an external API after the call connects. For example, an AI agent or backend service can grab the phone number and pull in additional context from a CRM or database. Here’s a simple flow to guide you: 1. Detect when the SIP participant joins the room. 2. Extract the
sip.phoneNumber
attribute. 3. Call your external API using this number. 4. Use the returned context to enhance the session (like updating attributes or customizing agent behavior). Also, if your SIP provider supports custom headers, you can pass pre-call data (such as a customer ID) through SIP headers and map them to participant attributes using the
headers_to_attributes
configuration. This can be really handy! Please feel free to ask if you'd like me to walk you through more details or provide examples—I’m here to help! Sources: SIP participant | LiveKit docs
c
show me the sample code for your simple flow
t
I'd love to share a helpful example with you! Here’s a sample code snippet that shows how you can use the caller’s phone number to fetch context from an external API right after the SIP participant joins a LiveKit room:
Copy code
const livekit = require('livekit-server-sdk');

// Initialize LiveKit Room
const room = new livekit.Room();

// Listen for participant connection events
room.on('participant_connected', async (participant) => {
  if (participant.kind === 'SIP') {
    // Extract the caller's phone number from participant attributes
    const phoneNumber = participant.attributes['sip.phoneNumber'];

    // Call an external API to fetch context using the phone number
    const context = await fetchUserContextFromAPI(phoneNumber);

    // Apply the returned context (e.g., update participant metadata)
    participant.metadata = JSON.stringify(context);

    console.log(`Context for ${phoneNumber}:`, context);
  }
});

// Example function to call an external API
async function fetchUserContextFromAPI(phoneNumber) {
  const response = await fetch(`${process.env.BASE_API_URL}/user-context?phone=${phoneNumber}`);
  if (!response.ok) {
    throw new Error('Failed to fetch user context');
  }
  return response.json();
}

// Start the room (replace with your actual room connection logic)
room.connect('ws://your-livekit-server', 'your-api-key', 'your-api-secret');
This example walks you through: • Listening for the
participant_connected
event. • Extracting the
sip.phoneNumber
attribute. • Calling an external API to fetch user context. • Applying the fetched context to the participant’s metadata. Just a quick note: be sure to replace
process.env.BASE_API_URL
with your actual API base URL. If anything isn’t clear or if you'd like me to provide more examples, please don’t hesitate to ask—I’m here to help!
c
so I can fetch more details about the caller, before agent join?
t
Absolutely, you can definitely fetch more details about the caller before the agent joins the conversation. Once the SIP participant connects to the room, their phone number becomes available as the
sip.phoneNumber
attribute. You can use this to reach out to an external API and gather extra context, like customer details, preferences, or history. To make sure this happens before the agent steps in, here’s a simple approach: 1. Set up your backend or AI agent to listen for the
participant_connected
event. 2. Use the phone number from the
sip.phoneNumber
attribute to fetch the caller’s details. 3. Process and store the information you get (for example, update participant metadata or get it ready for the agent). 4. Only let the agent join the room once all this context is prepared. This way, your agent will have all the important information about the caller before they start the interaction. If you’d like, I’m here to help you set up this flow—just let me know!
c
can I get the phone number that caller is calling?
t
Absolutely! LiveKit can definitely provide the phone number that the caller is dialing in an inbound SIP call. You’ll find this information within the SIP participant's attributes, specifically under the
sip.phoneNumber
key. Here’s how you can access it: • Make sure the
HidePhoneNumber
option is turned off in your SIP dispatch rule. • Once the call connects to the room, you can retrieve the
sip.phoneNumber
attribute from the participant's metadata. The phone number will be in E.164 format, which means it includes the country code and the full number without any special characters or spaces. If you have any questions or need a hand with this, just let me know—I’m here to help! Sources: SIP participant | LiveKit docs | protobufs/rpc/sip.proto
c
I mean I need both caller’s phone number, and the number that is calling for the inbound call
t
When it comes to inbound SIP calls, LiveKit is here to help by providing both the caller's phone number and the number being called: • Caller’s Phone Number: You can find this as the
sip.phoneNumber
attribute, which is taken from the SIP INVITE request. • Number Being Called: This shows up in the
call_to
field, representing the number the caller is dialing. Both of these values come from the SIP signaling information that LiveKit processes. Just make sure your SIP trunk configuration includes the relevant numbers in the
numbers
field of your inbound trunk setup, so calls to those numbers are accepted smoothly. If you'd like, I’m happy to provide more details or examples—just let me know! Sources: protobufs/livekit_sip.proto
c
I want the agent to work with multiple numbers, so when a user makes a call - 1. get the user’s phone number to fetch user details 2. get the number user is calling, to get context about the company details 3. after that, agent join is that possible? if so. show me the sample code
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
c
during a call, can the agent forward the call to another number? e.g. human pop in
t
Absolutely! An agent can forward a SIP call to another number during a call in LiveKit, and I’m here to guide you through how it works. You can use the
TransferSIPParticipant
API, which supports cold transfers. Here’s a simple breakdown: • Cold Transfer: The agent transfers the call to another phone number or SIP URI, and the current session ends once the transfer is complete. • The key parameters you’ll need include: ◦ `participant_identity`: This is the identity of the participant you want to transfer. ◦ `room_name`: The name of the room where the participant is currently. ◦ `transfer_to`: The destination phone number or SIP URI (for example,
tel:+15105550100
or
sip:user@domain.com
). ◦
play_dialtone
(optional): You can choose whether to play a dial tone during the transfer. If you’re looking to do a warm transfer, where the agent first connects with the transferee to provide some context, you can use
CreateSIPParticipant
to call the transferee and then
MoveParticipant
to bring them into the original call. Also, just a quick reminder to make sure your SIP trunk is set up to allow call transfers. If you’d like, I’m more than happy to share more details or walk you through some examples—just let me know! Sources: SIP APIs | LiveKit docs | README.md | agent.py | Agent-assisted warm transfer | LiveKit docs | examples/warm-transfer/README.md