# SIP Trunks

Source: https://docs.qvoice.net/admin/sip-trunks/

How a reseller or the platform admin sets the SIP trunks an account uses for outbound calls to the
public phone network (PSTN).

Trunks are usually set up once, on the platform or reseller account, and every customer under it uses
them. Create trunks for a single customer only when it needs its own carrier, for example a
customer that brings its own SIP trunk.

## Where trunks are managed

In the Portal: **Configuration** > **Phone System** > **Trunks**. The page is shown to reseller
admins and the platform admin.

- **Add**: create a trunk.
- **Trunks Route**: which trunks this account uses (below).
- **Edit** in the **Actions** column: change a trunk.

## Trunks Route

| Route | Calls go out through |
| --- | --- |
| **parent** | The trunks of the parent account (the reseller or the platform). This is the default. |
| **local** | The trunks created in this account. Create at least one trunk before choosing it. |

Pick the route and click **Save**. When the account uses custom routing, the page shows **Custom
Routing is Enabled**.

## Create a trunk

Click **Add**. A trunk is written in JSON, because a SIP trunk has many options. A minimal trunk:

```
{
  "name": "Carrier A",
  "enabled": true,
  "emergency": false,
  "flags": ["fax"],
  "gateways": [
    {
      "server": "sip.carrier-a.example",
      "realm": "sip.carrier-a.example",
      "username": "",
      "password": "",
      "port": 5060,
      "prefix": "",
      "codecs": ["PCMU", "PCMA"],
      "media": { "fax_option": true },
      "channel_selection": "ascending",
      "endpoint_type": "sip",
      "invite_format": "route",
      "force_port": false,
      "progress_timeout": 20,
      "skype_rr": false,
      "enabled": true
    }
  ],
  "rules": ["^\\+(1\\d{6,})$"],
  "weight_cost": 50
}
```

The fields you always set:

| Field | What it is |
| --- | --- |
| `name` | Name of the trunk |
| `gateways[].server` | IP address or host name of the carrier |
| `gateways[].realm` | Usually the same as `server` |
| `gateways[].username`, `password` | The carrier's credentials, if it uses registration or digest authentication |
| `rules` | Regular expressions matched against the number dialed. A call uses this trunk when a rule matches. The example matches North American numbers. |
| `weight_cost` | Priority between trunks whose rules match: the lower the number, the higher the priority |
| `gateways[].prefix` | Digits added in front of the number before it is sent to the carrier |

## Format the outbound caller ID

Add `formatters` to the trunk to change the caller ID number sent to the carrier.

Rewrite it to a local format. The regular expression matches the caller ID number and keeps what is
in brackets; `prefix` is added in front. Here `+661234567` is sent as `01234567`:

```
"formatters": {
  "outbound_caller_id_number": {
    "prefix": "0",
    "regex": "^\\+?66(\\d{5,})$"
  }
}
```

Always send the same caller ID number:

```
"formatters": {
  "outbound_caller_id_number": {
    "value": "14165554444"
  }
}
```
