how do i get the sipTrunkPhoneNumber and then do s...
# ask-ai
l
how do i get the sipTrunkPhoneNumber and then do session.start({....}) ?
t
Thanks for reaching out, Istiaque. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're exploring how to use a SIP trunk phone number (
sipTrunkPhoneNumber
) with LiveKit! To get started, you'll first want to create a SIP trunk. You can do this using methods like
createSipInboundTrunk
or
createSipOutboundTrunk
, where you'll define the phone number during the trunk creation process. After your trunk is set up, you can easily initiate a SIP call using the
createSipParticipant
method. This connects a SIP caller to a LiveKit room, and if you don’t specify a
fromNumber
, it will automatically use the trunk's associated phone number. Regarding the
session.start
method you mentioned, it’s not part of LiveKit’s documented SIP APIs. If this comes from a custom setup or a third-party integration, could you share a bit more about where you saw it? That way, I can better assist you and make sure we find the right solution together. Sources: packages/sip--make-a-call/src/make-call.ts | packages/livekit-server-sdk/src/SipClient.ts | lib/livekit/sip_service_client.rb | agent.py | SIP participant | LiveKit Docs
l
const client = new RoomServiceClient( process.env.LIVEKIT_URL!, process.env.LIVEKIT_API_KEY, process.env.LIVEKIT_API_SECRET ); export default defineAgent({ prewarm: async (proc: JobProcess) => { proc.userData.vad = await silero.VAD.load(); }, entry: async (ctx: JobContext) => { // const agentPhoneNumber = "+12348134415"; const vad = ctx.proc.userData.vad! as silero.VAD; const assistant = new voice.Agent({ instructions: "You are a helpful voice AI assistant.", }); const session = new voice.AgentSession({ vad, stt: "assemblyai/universal-streaming:en", llm: "openai/gpt-4.1-mini", tts: "cartesia/sonic-3:9626c31c-bec5-4cca-baa8-f8ba9e84c8bc", turnDetection: new livekit.turnDetector.MultilingualModel(), }); await session.start({ agent: assistant, room: ctx.room, inputOptions: { noiseCancellation: BackgroundVoiceCancellation(), }, }); const participants = await client.listParticipants(ctx.room.name!); const agentPhoneNumber = participants[1]?.attributes["sipTrunkPhoneNumber"]; console.log("agentPhoneNumber", agentPhoneNumber); await ctx.connect(); const handle = session.generateReply({ instructions: "Greet the user and offer your assistance.", }); }, }); cli.runApp(new WorkerOptions({ agent: fileURLToPath(import.meta.url) })); there's several sip numebrs assigned to this ai agent. what i am doiing is depending on the sip number, supabase fetches the data and feeds the ai agent. how do i do it here?
t
I’m happy to help you manage multiple SIP numbers assigned to your AI agent and fetch data dynamically based on the SIP number! You can use the
sip.trunkPhoneNumber
attribute, which holds the phone number linked to the SIP trunk. This attribute is accessible from the participant's attributes once the call is connected. Here’s a simple way to get started: 1. Make sure each SIP number is connected to an inbound SIP trunk. 2. Use the
sip.trunkPhoneNumber
attribute to identify which number was dialed. For example:
Copy code
const participants = await client.listParticipants(ctx.room.name!);
const agentPhoneNumber = participants[1]?.attributes["sipTrunkPhoneNumber"];
console.log("Dialed Number:", agentPhoneNumber);
1. Then, based on the dialed number, you can fetch the relevant data from Supabase or another database. For example:
Copy code
if (agentPhoneNumber === "+1234567890") {
  const data = await supabase.from('table_name').select('*').eq('phone_number', agentPhoneNumber);
  // Use the fetched data to configure the AI agent
}
Additionally, you can set up dispatch rules to assign custom attributes to each SIP number, helping you tailor your agent’s behavior even more dynamically. If you’d like, I’m here to guide you through setting up dispatch rules or help with any specific logic for fetching and using data based on the SIP number. Just let me know! Sources: SIP participant | LiveKit Docs | pkg/service/ioservice_sip_test.go | SIP dispatch rule | LiveKit Docs | sip-refer/README.mdoc
l
fetch the dispatch rules then
t
To help you fetch dispatch rules for SIP numbers in LiveKit, you can use the
ListSIPDispatchRule
API. This handy API lets you retrieve all SIP dispatch rules, and you can even filter them by specific criteria like rule IDs or trunk IDs to suit your needs. Here’s a friendly example in Python to get you started:
Copy code
from livekit.api import LiveKitAPI
from livekit.protocol.sip import ListSIPDispatchRuleRequest

# Initialize the LiveKit API client
lk_api = LiveKitAPI(
    url=<LIVEKIT_URL>,
    api_key=<LIVEKIT_API_KEY>,
    api_secret=<LIVEKIT_API_SECRET>,
)

# Fetch all SIP dispatch rules
response = await lk_api.sip.list_dispatch_rule(ListSIPDispatchRuleRequest())
for rule in response.items:
    print(rule)
And if Ruby is more your style, here’s how you can do it:
Copy code
require 'livekit'

# Initialize the SIP service client
client = LiveKit::SIPServiceClient.new(
  <LIVEKIT_URL>,
  api_key: <LIVEKIT_API_KEY>,
  api_secret: <LIVEKIT_API_SECRET>
)

# Fetch all SIP dispatch rules
rules = client.list_sip_dispatch_rule
puts rules
If you want to narrow down the rules to specific inbound numbers, just use the
inbound_numbers
field in your request. This way, you’ll get only the rules that accept calls made to those numbers. If you’d like, I’m here to help you implement this in your specific setup or answer any questions you might have. Just let me know! Sources: SIP dispatch rule | LiveKit Docs | protobufs/livekit_phone_number.proto | livekit-api/src/services/sip.rs | livekit-api/livekit/api/sip_service.py