Thanks — I’m responding to your question about preventing LiveKit from routing inbound and outbound calls when a user’s credits are zero.
Short answer: handle the credit check in your backend and only create the LiveKit SIP objects (dispatch rules for inbound, SIP participants for outbound) when the user has sufficient credits. Trunks are global and don’t know about your credit model, so the safest place to enforce this is before you call LiveKit.
Recommended approaches
• Inbound calls
◦ Only create a SIP dispatch rule (the rule that routes PSTN calls into rooms/agents) for users who have >0 credits.
◦ If a user’s credits drop to zero, remove or disable their dispatch rule so new incoming PSTN calls no longer get routed to them.
◦ Optionally return a recorded message or IVR response outside LiveKit explaining credits are insufficient (handled by your SIP provider or an IVR front end) rather than letting calls reach LiveKit.
• Outbound calls
◦ Before creating a SIP participant or issuing an outbound dial through LiveKit, check the user’s credit balance.
◦ If credits <= 0, reject the call attempt and return an error to the client.
◦ If allowed, deduct expected cost (or reserve credits) before dialing; adjust final billing later using webhooks with actual call duration.
Practical implementation notes
1. Centralized credit-check middleware
◦ Implement a single service that authorizes all call-related operations. All endpoints that create dispatch rules or SIP participants call this service first.
1. For inbound: create/remove dispatch rules
◦ Create dispatch rules only when credits are available.
◦ Remove/disable rules immediately when credits reach zero to stop future inbound routing.
1. For outbound: pre-check and reserve
◦ Check credits synchronously before initiating the outbound SIP participant.
◦ Optionally reserve/deduct a minimum cost to avoid mid-call cutoffs; reconcile with actual cost from webhooks after the call.
1. Use LiveKit webhooks
◦ Track call starts/stops and durations via webhooks to finalize credit usage and adjust balances after the call ends.
Questions to tailor the recommendation
• Do you currently create dispatch rules dynamically per user, or do you have a static routing layer?
• For outbound calls, do you prefer reserving credits (to avoid interruption) or deducting post-call based on usage?
• Are you using any external SIP provider/IVR in front of LiveKit that could play a “no credits” message?
If you share which model you already use (dynamic dispatch vs static routing, and whether you want pre-reservation or post-billing), I can provide example code snippets or a flow diagram for your backend logic.
Sources:
Agents telephony integration | LiveKit Documentation |
Outbound Calling Script | LiveKit Documentation