1

I can't find how to set up or change the Webhook through API. Is it possible to change it, set it when I am buying a number, or select one Webhook URL for all numbers?

I tried to find this info in the documentation but there was helpful to me

nbo
  • 21
  • 2

2 Answers2

2

Yes, you can do that with the following command from the CLI:

twilio phone-numbers:update <TWILIO_NUMBER> --voice-url https://xxxxxxxx.ngrok.io --sms-url https://xxxxxxxx.ngrok.io

or with Node

const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);

client.incomingPhoneNumbers('PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
  .update({voiceUrl: 'https://www.your-new-voice-url.com/example'})
  .then(incoming_phone_number => console.log(incoming_phone_number.friendlyName));

You can find other snippets in the docs (scroll to "Example 2 Update IncomingPhoneNumber to use a new Voice URL").

PS: If you want to reuse the same configuration for multiple phone numbers, you might want to see whether TwiML Apps can help you too.

IObert
  • 2,118
  • 1
  • 10
  • 17
  • thanks, bro, you are the best) It was really not so obvious to me, that it calls sms_url / voice_url. Thanks a lot! – nbo Jan 13 '23 at 19:39
  • I'm glad I could help :) It would be great if you could go ahead and use the "checkmark" button to accept the answer. – IObert Jan 15 '23 at 15:58
0

You will have to log into your Twilio console.

From the Develop tab, select Phone Numbers, then Manage > Active Numbers.

You can set the default Webhook (and back-up alternate Webhook) by clicking on the desired number and entering it under the respective Phone or (if available) SMS fields. You will likely have to set the Webhook (takes 2 seconds) for each phone number purchased as the default is the Twilio Demo URL (replies back with Hi or something)

The nature of a Webhook should allow any change in functionality to be done externally (on your end) through your Webhook script's functionality and thus dynamically changing the Webhook URL through the API on a case-by-case basis is discouraged and frankly should not be necessary. Someone may correct me if mistaken.

John Smith
  • 490
  • 2
  • 11
  • Yes, you are correct, but I knew that. I wanted to set the web hook through API. Without using Twilio's console. – nbo Jan 11 '23 at 20:34
  • @nbo What purpose would that hold? Perhaps if you could provide one that can't easily be disavowed (proven to have little purpose) Twilio may institute such a function. Until then, if you can request/change a Webhook you can just as easily change it's internal functionality... it's a solution in search of a problem- as far as development goes. Simple class function. request (A) does function (A) request (B) does function (B). Create a private web interface that allows you to switch between any functionality you wish. – John Smith Jan 11 '23 at 23:09