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

Work with webhooks


One of the most convenient features of the CLI is the ability to quickly direct Twilio webhooks to a new handler. Both Incoming Message Webhooks and Voice are supported.


Update webhook handlers

update-webhook-handlers page anchor

For example, you can redirect incoming SMS webhooks to the /handle-sms route of your deployed server with:


_10
twilio phone-numbers:update {PHONE_NUMBER_SID|E164} \
_10
--sms-url http://www.your-server.com/handle-sms

Replace {PHONE_NUMBER_SID|E164} with either the SID of your Twilio number (ex. PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX), or the E.164 formatted version of the phone number itself (ex. "+14155552671"). This will set the primary SMS URL for the given Twilio Phone number, and that endpoint will begin handling incoming messages.

Similarly, you can leverage the --voice-url flag to modify your phone number's incoming call handler:


_10
twilio phone-numbers:update {PHONE_NUMBER_SID|E164} \
_10
--voice-url http://www.your-server.com/handle-call

There are also options for setting the fallback URL and more. Run twilio phone-numbers:update --help for a full list of options.


If you specify a webhook URL that uses the host name localhost or 127.0.0.1, the CLI will automatically create a public proxy for you using ngrok(link takes you to an external page) and set your webhook to direct requests to the new proxy URL. For example:


_10
twilio phone-numbers:update {PHONE_NUMBER_SID|E164} \
_10
--sms-url http://localhost:3000/handle-sms

This is incredibly handy when trying to prototype an application and validate your webhook handler logic without deploying code to a publicly accessible URL.

(warning)

Warning

Please be aware that this will temporarily expose your computer to the internet. You should exit this command when you have completed testing.


In addition to these built-in features, Twilio Labs has released a CLI plugin to further help you develop and test your webhooks: the Webhook plugin for the Twilio CLI(link takes you to an external page).

Read this excellent introduction blog post(link takes you to an external page) to learn more.


Rate this page: