Introduction
A dispatch rule controls how callers are added as SIP participants in rooms. When an inbound call reaches your SIP trunking provider and is connected to LiveKit, the SIP service authenticates the inbound trunk (if applicable) and looks for a matching dispatch rule. It then uses the rule to dispatch SIP participants to rooms.
The dispatch rule can also include room configuration options that specify which agents to dispatch to the room. Agent dispatch is a separate feature that handles how agents are dispatched to rooms. To learn more, see Agent dispatch.
To create a dispatch rule with the SIP service, use the CreateSIPDispatchRule
API. It returns a SIPDispatchRuleInfo
object that describes the dispatch rule.
By default, a dispatch rule matches all your trunks and makes a caller's phone number visible to others in the room. You can modify these defaults using dispatch rule options. For a full list of available options, see the CreateSIPDispatchRule
API reference.
To learn more about, see the following:
- SIP overview: General concepts and features.
- SIP API: API endpoints and types.
Caller dispatch rule (individual)
An SIPDispatchRuleIndividual
rule creates a new room for each caller. The name of the created room is the phone number of the caller plus a random suffix. You can optionally add a specific prefix to the room name by using the roomPrefix
option.
The following examples dispatch callers into individual rooms prefixed with call-
, and dispatches an agent named inbound-agent
to newly created rooms:
{"dispatch_rule":{"rule": {"dispatchRuleIndividual": {"roomPrefix": "call-"}},"name": "My dispatch rule","roomConfig": {"agents": [{"agentName": "inbound-agent","metadata": "job dispatch metadata"}]}}}
When you omit the trunk_ids
field, the dispatch rule matches calls from all inbound trunks.
Direct dispatch rule
A direct dispatch rule places all callers into a specified room. You can optionally protect room access by adding a pin in the pin
field:
In the following examples, all calls are immediately connected to room open-room
on LiveKit.
Create a file named
dispatch-rule.json
and add the following:{"dispatch_rule":{"rule": {"dispatchRuleDirect": {"roomName": "open-room"}},"name": "My dispatch rule"}}Create the dispatch rule using
lk
:lk sip dispatch create dispatch-rule.json
Pin-protected room
Add a pin
to a room to require callers to enter a pin to connect to a room in LiveKit. The following example requires callers to enter 12345#
on the phone to enter safe-room
:
{"dispatch_rule":{"trunk_ids": [],"rule": {"dispatchRuleDirect": {"roomName": "safe-room","pin": "12345"}},"name": "My dispatch rule"}}
Callee dispatch rule
This creates a dispatch rule that puts callers into rooms based on the called number. The name of the room is the called phone number plus an optional prefix (if roomPrefix
is set). You can optionally add a random suffix for each caller by setting randomize
to true, making a separate room per caller.
{"dispatch_rule":{"rule": {"dispatchRuleCallee": {"roomPrefix": "number-","randomize": false}},"name": "My dispatch rule"}}
Setting custom attributes on inbound SIP participants
LiveKit participants have an attributes
field that stores key-value pairs. You can add custom attributes for SIP participants in the dispatch rule. These attributes are inherited by all SIP participants created by the dispatch rule.
To learn more, see SIP participant attributes.
The following examples add two attributes to SIP participants created by this dispatch rule:
{"dispatch_rule":{"attributes": {"<key_name1>": "<value1>","<key_name2>": "<value2>"},"rule": {"dispatchRuleIndividual": {"roomPrefix": "call-"}},"name": "My dispatch rule"}}
Setting custom metadata on inbound SIP participants
LiveKit participants have a metadata
field that can store arbitrary data for your application (typically JSON). It can also be set on SIP participants created by a dispatch rule. Specifically, metadata
set on a dispatch rule will be inherited by all SIP participants created by it.
The following examples add the metadata, {"is_internal": true}
, to all SIP participants created from an inbound call by this dispatch rule:
{"dispatch_rule": {"metadata": "{\"is_internal\": true}","rule": {"dispatchRuleIndividual": {"roomPrefix": "call-"}},"name": "My dispatch rule"}}
Update dispatch rule
Use the UpdateSIPDispatchRule
API to update specific fields of a dispatch rule or replace a dispatch rule with a new one.
Update specific fields of a dispatch rule
The UpdateSIPDispatchRuleFields
API allows you to update specific fields of a dispatch rule without affecting other fields.
Create a file named dispatch-rule.json
with the following content:
{"name": "My updated dispatch rule","rule": {"dispatchRuleCallee": {"roomPrefix": "number-","randomize": false,"pin": "1234"}}}
Update the dispatch rule using lk
. You can update the trunks
parameter to a comma-separated string of trunks IDs if the rule matches specific trunks.
lk sip dispatch update --id <dispatch-rule-id> \--trunks "[]" \dispatch-rule.json
Replace dispatch rule
The UpdateSIPDispatchRule
API allows you to replace an existing dispatch rule with a new one using the same dispatch rule ID.
The instructions for replacing a dispatch rule are the same as for updating a dispatch rule.
List dispatch rules
Use the ListSIPDispatchRule
API to list all dispatch rules.
lk sip dispatch list