Search code examples
pythonresttwiliotwilio-api

twilio sms webhook api


I'm would like to programmatically change the webhook for a given number (and possibly enumerate available numbers) or messaging service, but I can't find the API documentation. From Our Request to your Webhook URL:

You can configure the URLs and HTTP Methods Twilio uses to make its requests via your account portal in the Twilio Console or using the REST API.


Solution

  • The keyword you're looking for is the incoming-phone-number resource.

    In Python, you can use the following script to update its sms_url prop:

    import os
    from twilio.rest import Client
    
    
    # Find your Account SID and Auth Token at twilio.com/console
    # and set the environment variables. See http://twil.io/secure
    account_sid = os.environ['TWILIO_ACCOUNT_SID']
    auth_token = os.environ['TWILIO_AUTH_TOKEN']
    client = Client(account_sid, auth_token)
    
    incoming_phone_number = client \
        .incoming_phone_numbers('PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
        .update(sms_url='https://www.your-new-sms-url.com/example')
    
    print(incoming_phone_number.friendly_name)