Skip to main content

Releasing Numbers from a CSV

Overview

At some point, you may need to release a large quantity of numbers in order to purchase new ones. This can be tedious to delete one by one, so this example shows one way that you can release numbers in bulk with ease.

Full code example: Release Numbers based on CSV
from signalwire.rest import Client as signalwire_client
import csv

client = signalwire_client("ProjectID", "AuthToken",
signalwire_space_url='YOURSPACE.signalwire.com')

incoming_phone_numbers = client.incoming_phone_numbers.list()
print("Total Numbers -- " + str(len(incoming_phone_numbers)))

results = []

with open("Numbers.csv", 'r', encoding='utf-8-sig') as csvfile:
reader = csv.reader(csvfile)

for row in reader:
if "+" not in row[0]:
results.append("+" + row[0])
else:
results.append(row[0])
print(results)

for record in incoming_phone_numbers:
if record.phone_number in results:
print("deleting number -- " + record.phone_number)
client.incoming_phone_numbers(record.sid).delete()

What are we going to do?

This code snippet will read a CSV of numbers that you want to delete, compare it with the full list of numbers within your project, and release all the ones that match the CSV. Please note: the CSV file containing the numbers to be deleted must be in the same folder as the below script.

Python

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 Python SDK 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 yourSignalWire 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.

YOURSPACE - 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.

You will need to replace the name ‘Numbers.csv’ with the name of your CSV file.

Code Walkthrough

Load required libraries

from signalwire.rest import Client as signalwire_client
import csv

Instantiate the SignalWire Client

In order to successfully connect to SignalWire's servers, make sure to replace ProjectID, AutoToken, and YOURSPACE.

client = signalwire_client("ProjectID", "AuthToken", signalwire_space_url='YOURSPACE.signalwire.com')

Get numbers and print them

incoming_phone_numbers = client.incoming_phone_numbers.list()
print("Total Numbers -- " + str(len(incoming_phone_numbers)))

Open CSV file and add numbers to results array

Please Note

The CSV file containing the numbers to be deleted must be in the same folder as the script. Otherwise, you will need to specify the proper path to your CSV when opening the file.

results = []

with open("Numbers.csv", '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, and release each of them if it exists in the Results array

for record in incoming_phone_numbers:
if record.phone_number in results:
print("deleting number -- " + record.phone_number)
client.incoming_phone_numbers(record.sid).delete()

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!

Sign Up Here

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

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