alexrbrown
alexrbrown

Reputation: 31

Twilio - Need to dynamically set webhook from application after creating a new number

I have built an application that assigns a customer a twilio number that they will be able to send text messages to. I am able to create the number dynamically but i now need to set the webhook inside the code for the incoming texts so that twilio knows how to respond. Right now i am only aware of a way to do it through the console which wont work for what i need. Any help would be greatly appreciated. Thanks!

Upvotes: 2

Views: 1623

Answers (2)

philnash
philnash

Reputation: 73065

Twilio developer evangelist here.

Thanks to Alex for the answer, that's spot on. I just wanted to add a bit of code as I noticed the question was tagged Node.js.

Here is how to do the API calls with the Node.js helper library.

Update an existing incoming phone number:

var accountSid = 'YOUR_ACCOUNT_NUMBER';
var authToken = 'YOUR_AUTH_TOKEN';
var client = require('twilio')(accountSid, authToken);

client.incomingPhoneNumbers("PHONE_NUMBER_SID").update({
    smsUrl: "http://demo.twilio.com/docs/sms.xml"
}, function(err, number) {
    if (err) { console.error(err); return }
    console.log(number.voiceUrl);
});

When buying the number:

client.incomingPhoneNumbers.create({
    friendlyName: "My Company Line",
    smsUrl: "http://demo.twilio.com/docs/voice.xml",
    phoneNumber: "PHONE_NUMBER_TO_PURCHASE"
}, function(err, number) {
    if (err) { console.error(err); return }
    console.log(number.sid);
});

Upvotes: 5

Alex Baban
Alex Baban

Reputation: 11732

It can be done using Optional Parameters

when you update an incoming phone number:

https://www.twilio.com/docs/api/rest/incoming-phone-numbers?code-sample=code-update-an-incomingphonenumber&code-language=curl&code-sdk-version=json

$ curl -XPOST https://api.twilio.com/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/IncomingPhoneNumbers/PN2a0747eba6abf96b7e3c3ff0b4530f6e.json \
    -d "VoiceUrl=http://demo.twilio.com/docs/voice.xml" \
    -d "SmsUrl=http://demo.twilio.com/docs/sms.xml" \
    -u 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token'

or when you create a new incoming phone number:

https://www.twilio.com/docs/api/rest/incoming-phone-numbers?code-sample=code-create-a-new-incomingphonenumber&code-language=curl&code-sdk-version=json

$ curl -XPOST https://api.twilio.com/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/IncomingPhoneNumbers.json \
    -d "FriendlyName=My%20Company%20Line" \
    -d "SmsUrl=http://demo.twilio.com/docs/sms.xml" \
    -d "PhoneNumber=%2B15105647903" \
    -d "SmsMethod=GET" \
    -u 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token'

Upvotes: 2

Related Questions