give me sample livekit telephone code
# ask-ai
b
give me sample livekit telephone code
t
Thanks for reaching out, Pavan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hi there! I wanted to share a helpful sample code snippet for integrating telephony (SIP) with LiveKit using Node.js. I hope this makes things a bit easier for you:
Copy code
import { SipClient } from 'livekit-server-sdk';

const sipClient = new SipClient(LIVEKIT_URL, LIVEKIT_API_KEY, LIVEKIT_API_SECRET);

// Create or retrieve an inbound trunk
const trunkName = 'inbound-trunk';
const trunks = await sipClient.listSipInboundTrunk();
let trunk = trunks.find(t => t.name === trunkName);
if (!trunk) {
  trunk = await sipClient.createSipInboundTrunk(trunkName, [TWILIO_PHONE_NUMBER], {
    auth_username: TWILIO_SIP_USERNAME,
    auth_password: TWILIO_SIP_PASSWORD,
  });
}

// Create or retrieve a dispatch rule
const dispatchRuleName = 'inbound-dispatch-rule';
const dispatchRules = await sipClient.listSipDispatchRule();
let dispatchRule = dispatchRules.find(r => r.name === dispatchRuleName);
if (!dispatchRule) {
  dispatchRule = await sipClient.createSipDispatchRule({
    type: 'individual',
    roomPrefix: 'call',
  }, {
    name: dispatchRuleName,
    trunkIds: [trunk.sipTrunkId],
  });
}
This snippet helps you set up an inbound trunk and a dispatch rule programmatically. If you prefer, you can also achieve similar setups using Python or JSON configuration. Please feel free to reach out if you have any questions or need a hand with anything else—I’m here to help! Sources: src/setup-livekit.ts | Creating SIP Inbound Trunks and Dispatch Rules with Python SDK | SIP inbound trunk | LiveKit Docs | complex-agents/ivr-agent/README.md