How to Connect Asterisk to SignalWire CLOUD for SIP Connectivity

https://signalwire.com/blogs/product/connect-asterisk-signalwire-cloud-sip-connectivity

Having recently started working with SignalWire, I was very keen to see if I could get Asterisk connected up – as just one of the things that SignalWire can do is act as a SIP trunk, providing inbound and outbound calling for your PBX or hosted PBX infrastructure.

Now it’s true that SignalWire is massively more than just competitively priced SIP trunking, but seeing as I already know Asterisk I thought that this would be a great place for me to start!

The first thing to do is to go to https://signalwire.com and create a SignalWire account. This is very easy and straightforward, and you are even given a $5.00 credit to do things like buy DID numbers. Incidentally, a single US DID costs just 8 cents per month! SIP connectivity is metered per minute/per call leg at just $0.0007, and that includes SRTP and TLS encryption that many providers charge extra for. 

As you create your account, you create a “Space URL”, effectively the subdomain at signalwire.com for your account. I went with duffett.signalwire.com.

Then you can create a project. I simply called my project “Asterisk”. From here on in, you are working within your project.

The next job is to create a SIP endpoint, as this is how your Asterisk will connect with SignalWire. You will notice that you need to give the endpoint a name (I went again with “asterisk”) and that the full SIP URI for it includes the name you chose for your Space URL as well as a multi-character unique ID – mine looked like this:
asterisk@duffett-113115cf718e.sip.signalwire.com

You can leave all of the settings for the SIP endpoint at their defaults, so now you just need to set a (very strong) password.

Next you will add a phone number, so just pick a number that you would like and when you come to configure it you will see that there are a number of settings – the only one you need to change at this point is “handle calls using” – set this to be SIP endpoint. This, in turn, will require you to choose an endpoint, and since you have only created one so far, it will be that one! Mine, you might remember, was called “asterisk”.

That is all your work done at the SignalWire end!

Now let’s configure Asterisk. I am assuming you are using at least version 13.x.x and so I am going to show the PJSIP and dialplan settings.

First, here are the relevant entries for PJSIP:

/etc/asterisk/pjsip.conf

[transport-udp]
type=transport
protocol=udp
bind=0.0.0.0


[signalwire]
type=auth
auth_type=userpass
username=asterisk ; Your username
password=<very-strong-password>

[signalwire]
type=aor
contact=sip: duffett-113115cf718e.sip.signalwire.com

[signalwire]
type=endpoint
transport=transport-udp
outbound_auth=signalwire ; Note that there is only an outbound_auth, as we do not challenge when a call arrives inbound
aors=signalwire
disallow=all
allow=ulaw
from_user=asterisk
from_domain=duffett-113115cf718e.sip.signalwire.com
media_encryption=sdes ; Note that we are using encryption
context=from-signalwire

[signalwire]
type=registration
server_uri=sip: duffett-113115cf718e.sip.signalwire.com
client_uri=sip:asterisk@duffett-113115cf718e.sip.signalwire.com ; Your full SIP URI
outbound_auth=signalwire

[signalwire]
type=identify
endpoint=signalwire
match=duffett-113115cf718e.sip.signalwire.com

Now your SIP connection is made. Asterisk should register with SignalWire using the registration section, which in turn used the information in your auth section to authenticate.

Inbound calls are matched to the SignalWire endpoint using the identify section, and then handled in the from-signalwire context in the dialplan.

Outbound calls are sent out through the SignalWire endpoint to the host which is identified in the aor section.

And now for the entries in the dialplan!

In the from-signalwire context, you will notice that the number 1 priority of the s extension sets a variable called numb to a CUT() of a CUT() of the result of a read of the PJSIP_HEADER function, looking at the To field.

This is because as far as Asterisk is concerned, the call has arrived without any dialed digits (that is why we have to run with the s extension).

The dialed number is actually contained in the To field of the SIP header, and so that first line is just to obtain the dialed number.

/etc/asterisk/extensions.conf

[outbound]
exten => _1NXXNXXXXXX,1,Dial(PJSIP/${EXTEN}@signalwire)

[from-signalwire]
exten => s,1,Set(numb=${CUT(CUT(PJSIP_HEADER(read,To),@,1),:,2)})
same => n,Goto(${numb:1},1)  ; Using ${numb:1} simply strips away the first character or number, in this case a‘+’

exten => 12563840002,1,Playback(tt-monkeys)

A note on outbound dialing:


If you have an endpoint connected to your Asterisk that can get into the outbound context, you should be able to send calls out to US numbers using the pattern shown.

However, when your SignalWire account is first created, it will be in “Trial Mode” with a $5.00 credit, but you cannot send outbound calls until you add some credit (from $5.00 to $50.00) to bring the account out of Trial Mode.
Without doing this, outbound calls will be greeted with a 404 error!

So now you can see how easy it is to get your Asterisk connected to the outside world with SignalWire. You can learn about our disruptive pricing here: https://signalwire.com/disruptive-pricing