PJSIP Endpoint Templates and SIP Trunk
PJSIP Endpoint Templates and SIP Trunk
Define a base template once, then create endpoints by inheriting from it. This eliminates repetition and ensures consistent settings across all phones. Includes transport definitions and SIPSession Initiation Protocol, the standard signaling protocol used to set up, manage, and tear down VoIP calls between Asterisk and phones or carriers. trunkA connection between Asterisk and another PBX or an ITSP/carrier, used to send and receive external calls. configuration.
Requirements
res_pjsip.soand related modules loaded- TLS certificates if using encrypted transport
- SIP trunk account credentials from your provider
Transports (pjsip.conf)
[transport-udp]
type = transport
protocol = udp
bind = 0.0.0.0:5060
[transport-tcp]
type = transport
protocol = tcp
bind = 0.0.0.0:5060
[transport-tls]
type = transport
protocol = tls
bind = 0.0.0.0:5061
cert_file = /etc/asterisk/keys/asterisk.crt
priv_key_file = /etc/asterisk/keys/asterisk.key
method = tlsv1_2
SIP Trunk (pjsip.conf)
[trunk-auth]
type = auth
auth_type = userpass
username = your_trunk_username
password = your_trunk_password
[trunk-registration]
type = registration
outbound_auth = trunk-auth
server_uri = sip:sip.provider.com
client_uri = sip:your_username@sip.provider.com
[trunk-endpoint]
type = endpoint
context = from-trunk
allow = !all,ulaw,alaw
outbound_auth = trunk-auth
aors = trunk-aor
direct_media = no
send_pai = yes
send_rpid = yes
trust_id_outbound = yes
from_domain = sip.provider.com
[trunk-aor]
type = aor
contact = sip:sip.provider.com
qualify_frequency = 60
qualify_timeout = 3.0
[trunk-identify]
type = identify
endpoint = trunk-endpoint
match = sip.provider.com
Endpoint Template (pjsip.conf)
[endpoint-internal](!)
type = endpoint
context = internal
disallow = all
allow = ulaw,alaw,g722
direct_media = no
trust_id_outbound = yes
dtmf_mode = rfc4733
rewrite_contact = yes
rtp_symmetric = yes
force_rport = yes
rtp_keepalive = 15
device_state_busy_at = 1
named_call_group = office
named_pickup_group = office
media_encryption = sdes
[aor-internal](!)
type = aor
max_contacts = 5
qualify_frequency = 30
qualify_timeout = 3.0
remove_existing = no
Using Templates for Extensions
; Extension 1001 - inherits all settings from endpoint-internal
[1001](endpoint-internal)
auth = 1001-auth
aors = 1001
callerid = Alice Smith <1001>
[1001](aor-internal)
[1001-auth]
type = auth
auth_type = userpass
username = 1001
password = strong_random_password_here
How it works
- Template syntax
(!): The exclamation mark in[endpoint-internal](!)marks this section as a template. It won't create an actual endpointIn PJSIP configuration, the SIP entity Asterisk communicates with: a phone, trunk, or other user agent. Defined by an endpoint section in pjsip.conf.. it just defines defaults that other sections inherit. - Inheritance
(template-name):[1001](endpoint-internal)creates endpoint1001with all the settings from the template, plus any overrides specified in the[1001]section. - Trunk components: A SIP trunk needs 5 objects: auth (credentials), registration (keeps trunk registered), endpoint (call routing), AOR (address of record / contact), and identify (matches inbound packets to the trunk endpoint).
!allcodecA coder-decoder that compresses audio for transmission, such as ulaw, alaw, GSM, or Opus. Endpoints negotiate which codec a call uses. syntax:allow = !all,ulaw,alaw,g722first disables all codecs, then enables specific ones in preference order. G.722 provides wideband (HD) audio for internal calls.- NAT traversal:
rewrite_contact = yes,rtp_symmetric = yes, andforce_rport = yeshandle phones behind NAT.rtp_keepalive = 15sends periodic RTPReal-time Transport Protocol. Carries the actual audio (media) of a VoIP call after SIP signaling has set it up. packets to keep NAT pinholes open. - Call groups:
named_call_groupandnamed_pickup_groupenable directed and group call pickup (*8and**XXXXpatterns).
Tips
- Generate strong random passwords for each endpoint:
openssl rand -base64 16. - Set
device_state_busy_at = 1so BLF lights show busy when the phone is on a call. - Use
max_contacts = 5in the AOR template so the same extensionA dialplan entry that matches a dialed number or pattern within a context and triggers a sequence of prioritized steps. can register from multiple devices (desk phone, softphone, mobile app). - Verify trunk status with
pjsip show registration trunk-registrationon the CLI. - For TLS, generate certificates with Let's Encrypt or a self-signed CA. Most desk phones support TLS but may need the CA certificate imported.
- For MWI (voicemailAsterisk's built-in voice messaging system (app_voicemail), configured in voicemail.conf with mailboxes per user. waiting indicator), most phones use solicited MWI: the phone sends a SUBSCRIBE and
res_pjsip_mwihandles it automatically with no extra endpoint config. If your phones use unsolicited MWI instead, addmailboxes = 1001@defaultto each endpoint section.
User Notes
Know a tip or gotcha for this topic? Share it below and help others.
Contribute a note
Share a tip, gotcha, or practical example. Keep it under 2000 characters. No questions (use the Asterisk community forums for support). Wrap code in backticks.