Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

SIP Header Manipulation


The expected format of phone numbers in SIP INVITE requests can sometimes differ between on-premise equipment and providers. For example, some private branch exchanges (PBXs) might send numbers in a format other than E.164, the format that Twilio requires. In order to address this issue, Twilio provides a feature called SIP Header Manipulation.

SIP Header Manipulation allows you to automatically modify the user fields in a SIP INVITE for Elastic SIP Trunking termination and/or origination calls. (For Elastic SIP Trunking customers, user fields often contain phone numbers. This page will refer to phone numbers and user fields interchangeably.)

This feature is accessible in the Twilio Console under Elastic SIP Trunking > Settings > SIP Header Manipulation(link takes you to an external page).

The following section will describe the main components of Manipulation Policies, the set of rules that describe if, how, and when a SIP header should be modified.

If you prefer, you can jump down to the Create a Manipulation Policy section to view the steps for implementing this feature.


Manipulation Policies

manipulation-policies page anchor

In order to manipulate phone numbers within SIP INVITEs, you will need to create a Manipulation Policy and assign it to one or more of your SIP Trunks.

Manipulation Policies consist of:

  • A Friendly Name: a human-readable name for the Manipulation Policy
  • A Policy SID: a unique identifier for the Manipulation Policy
  • Request Rule(s): rules that define whether and how SIP INVITEs will be changed

You can check that your Manipulation Policy is working as expected by running a test on it in the Console.

The maximum number of Manipulation Policies per account is 1000. For each Trunk, you can assign one Manipulation Policy for termination and one for origination.

Rules

rules page anchor

Request Rules contain the logic that determines if and how an INVITE's headers should be manipulated.

  • One or more Actions that define how a SIP INVITE will be manipulated (maximum 20 per Request Rule)
  • Zero or more Conditions that determine whether or not the Action should be executed (maximum 20 per Request Rule)

Actions

actions page anchor

An Action specifies which phone numbers (user fields) you want to change and how you want to change them.

An Action is associated with a Request Rule, and a Request Rule can contain up to 20 Actions.

What phone numbers to change

When creating an Action, you will choose the phone numbers you want to modify when the Action executes.

Phone numbers (the user fields in SIP URIs) tend to repeat across several SIP headers within a SIP message. This means that if you need to change a phone number/user in one SIP header, it's likely that it needs to change elsewhere. To minimize the work you need to do to manipulate phone numbers/users in SIP INVITEs, Twilio has grouped related URI user fields together into To Number fields and From Number fields.

To Number fields represent the user who will receive the INVITE:

From Number fields represent the user who initiated the INVITE:

When an Action is executed on To Number fields, the To header's user field will be manipulated first, then copied into the Request-URI's user field.

When an Action is executed on From Number fields the From header's user field will be manipulated first, then copied into the P-Asserted-Identity, Remote-Party-ID, and Contact headers' user fields.

How to change the phone numbers

After choosing which phone numbers to change, you will specify how the phone numbers should change by selecting an action (a text modification) and a value to be used with the text modification.

You can choose from the following text modification actions:

  • Add prefix
  • Add suffix
  • Remove prefix
  • Remove suffix
  • Remove number of prefix
  • Remove number of suffix
  • Replace with

The Action's value is the value you want to use with the chosen action. Values are case-sensitive.

(warning)

Warning

There are restricted and reserved characters within each SIP field. Keep this in mind when defining values in your Actions.

For SIP user fields, only the characters below are allowed. You can refer to RFC 3261(link takes you to an external page) for guidelines on what characters should be escaped for the user fields. Be sure to escape other special characters.

Allowed characters for SIP users
A-Z | a-z | 0-9 | "-" | "." | "_" | "~" | "!" | "$" | "&" | "'" | "(" | ")" | "*" | "+" | "," | ";" | "=" | "?" | "/"
And escaped versions of special characters

Action Execution:

  • If a Request Rule does not contain Conditions, the Action(s) will be executed on every SIP INVITE.
  • If a Request Rule does contain Conditions, the Conditions determine whether or not the Action(s) will be executed.
  • Actions (if executed) are executed in the order they appear. The order can be modified.

You can conditionally execute the Action(s) in a given Request Rule by defining a group of Conditions and an execution type that determines how to evaluate the Conditions.

The execution type can be:

  • If any of these conditions are met (this is the default)
    • If any conditions in the list is true, the Action(s) will be executed.
    • If all of the conditions are false, the Action(s) will not be executed.
  • If all of these conditions are met
    • All of the conditions must be true in order for the Action(s) to be executed.
    • If any of the conditions is found to be false, the Action(s) will not be executed.
  • If none of these conditions are met
    • If all of the conditions are false, the Action(s) will be executed.
    • If any of the conditions are true, the Action(s) will not be executed.

Each Condition consists of:

  1. A SIP header field to be evaluated
  2. A logical operator
  3. A value to be used when evaluating the SIP header field

The evaluation of Conditions is case-sensitive.

The order of the Conditions (as they appear in the Console) is the order in which they will be evaluated. You can, however, reorder or delete them.

SIP header fields

The SIP header fields that can be evaluated are listed below. Click on a header field or go to the SIP INVITE Glossary page to learn more.

Logical operators

The logical operators that can be used when creating a Condition are:

  • Equals
  • Does not equal
  • Starts with
  • Does not start with
  • Contains
  • Does not contain
  • Ends with
  • Does not end with
(warning)

Warning

Keep in mind that Twilio will evaluate the SIP header exactly as it is presented and will not encode or decode special characters when comparing the SIP header to the value you have set in your Condition.

For example, if the SIP header contains the example%40company.com as the To user, and the value in your Condition contains example@company.com, Twilio will not equate these strings.

You can refer to RFC 3261(link takes you to an external page) for guidelines on what characters should be escaped within each field type of the SIP header. Below is a summary of the characters allowed within each SIP field.

SIP field typeAllowed characters
userA-Z | a-z | 0-9 | "-" | "." | "_" | "~" | "!" | "$" | "&" | "'" | "(" | ")" | "*" | "+" | "," | ";" | "=" | "?" | "/" And escaped versions of other special characters.
hostA-Z | a-z | 0-9 | "-" | "." | ":" | "[" | "]"
URIA-Z | a-z | 0-9 | "-" | "." | "_" | "~" | "!" | "$" | "&" | "'" | "(" | ")" | "*" | "+" | "," | ";" | "=" | ":" | "[" | "]" | "?" | "#" | "/" | "@" | "%" | "`" And escaped versions of other special characters.
display nameAscii characters with decimal values of 0 to 253.

Create a Manipulation Policy

create-a-manipulation-policy page anchor

This section will walk through creating an example Manipulation Policy in the Console.

  1. Log in to your Twilio Console(link takes you to an external page) . In the left-hand navigation pane, click on Elastic SIP Trunking > Settings > SIP Header Manipulations .
  2. On the "SIP header manipulations" page, click on the blue Create a policy button (in the top right corner).

    Header Manipulation - Click Create a Policy button.Rate this page:

    Need some help?

    Terms of service

    Copyright © 2024 Twilio Inc.