How to Update Webhooks in Bulk

Use Python to Update Webhooks on Bulk Amount of Numbers

To quickly run this code, check out our recipe below and replace the variables with your own!

Or, read our step by step walkthrough to learn more about this snippet!

Why do you need it?

If you have a lot of phone numbers, it could be tedious to change the webhook for message or call handling for all of them one by one. You can use this script (or one like it in another SDK) in order to change the webhook for all of the numbers at one time!

What does it do?

This code will list all of the numbers on your account and compare them to the list of numbers whose webhooks you want to update. For all the numbers that also exist in your CSV, the webhook will be updated sequentially.

What do you need to run the code?

You must have the SignalWire Python SDK installed. You can install that here.

Change the following variables

ProjectID- Your project ID is an alphanumeric string that tells the SignalWire SDK where to find your project. You can find this in an easily copyable format by going to your
SignalWire Portal and clicking the API tab on the left hand side.

AuthToken - Your Auth Token is an alphanumeric string that helps to authenticate your HTTP requests to SignalWire. You can create this (if you haven’t already) or copy this in an easily copyable format by going to your SignalWire Portal and clicking the API tab. If you have not created an API token, press the blue new button. If you have, click show and copy the string.

SpaceURL - Your space URL is the domain of your space, i.e. example.signalwire.com. This can also be found in an easily copyable format within the API tab in your SignalWire space.

CSV File - You will need to replace this with the actual path to your CSV file on your account. The easiest way is usually to keep it in the same file as the script.

Webhook - You will need to replace the webhook with the path to your webhook so that it can be updated correctly.

The code

from signalwire.rest import Client as signalwire_client
import csv
import requests
from requests.auth import HTTPBasicAuth

# define your variables here so they don't need to be hardcoded
SpaceURL = 'YourSpace.signalwire.com'
ProjectID = "Some-Alphanumeric-String"
AuthToken = 'Some-Alphanumeric-String'
WebhookPath = 'https://example.com/message_handler'
PathToCSV = 'Path-To-CSV-On-Your-Computer'

# Replace project ID, auth token, and space URL
client = signalwire_client(ProjectID, AuthToken, signalwire_space_url=SpaceURL)

# List and print all numbers on account
incoming_phone_numbers = client.incoming_phone_numbers.list()
print("Total Numbers -- " + str(len(incoming_phone_numbers)))

# create empty array to store numbers that need webhooks updated
results = []

# Open CSV file with numbers whose webhooks you want to update, replace with file path and file
with open(PathToCSV, 'r', encoding='utf-8-sig') as csvfile:
   reader = csv.reader(csvfile)
   # Change to E164 format if it's not already in that format
   for row in reader:
       if "+" not in row[0]:
           results.append("+" + row[0])
       else:
           results.append(row[0])
print(results)

# Loop through all account numbers, if number exists in Results array, print to console and update webhook
for record in incoming_phone_numbers:
   if record.phone_number in results:
       print("update number -- " + record.phone_number)
       response = requests.put('https://+SpaceURL+/api/relay/rest/phone_numbers/' + record.sid,
                               params={'call_handler': 'laml_webhooks',
                                       'message_request_url': WebhookPath}
                               , auth=HTTPBasicAuth(ProjectID, AuthToken))
       print(response)

Comments

This script uses both the RestAPI and Relay Rest API found in the documentation below in order to query all numbers in your project, compare with a CSV containing the numbers you want to update, and then update all webhooks for those numbers with your intended webhook.

Getting started guide

If you are looking for more information about using SignalWire, refer to our Getting Started guide.

Please feel free to reach out to us on our Community Slack or create a Support ticket if you need guidance!

Start a free trial

If you would like to test this example out, you can create a SignalWire account and space here.


Did this page help you?