How to Release Numbers by Area Code

This code snippet will filter all numbers by a certain area code and then return all the numbers that match. Once you review the numbers that it prints out, you can uncomment the delete line and run it again to release the numbers all at once.

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!

What do you need to run this code?

For this code, the SignalWire Python SDK will need to be installed. It can be accessed here: SignalWire Documentation.

What do you need to replace in this code?

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. This can also be found in an easily copyable format within the API tab in your SignalWire space.
You will need to replace the area code with that of the area code that you want to release from your account.

Step by Step Guide Through Code

The code below can be used to delete large volumes of DIDs at one time - the record.phone_number.startswith('+1855') will need to be replaced with the 3 digit area code that the results will be filtered by.

Begin by running this code initially with the line #client.incoming_phone_numbers(record.sid).delete() commented out to see a dry run of what will be deleted.

Once confirmed that numbers returned are to be deleted (this cannot be undone), un-comment the above line and run again.

from import Client as signalwire_client
client = signalwire_client("ProjectId", "Token", signalwire_space_url = '')
incoming_phone_numbers = client.incoming_phone_numbers.list()
print("Total Numbers -- " + str(len(incoming_phone_numbers)))
for record in incoming_phone_numbers:
  if (record.phone_number.startswith('+1855')):
      print("deleting number -- " + record.phone_number)

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?