This is a Python file that runs two Number APIs, one to search for available numbers and the second is to purchase the available list of numbers. The console will print out questions that specify what parameters are intended by the user.
To quickly run this code, check out our recipe below and replace the variables with your own!
Number Release Policy
Numbers that have been purchased in your account, will not be able to be released for 14 days.
Be sure to only purchase the numbers that are needed. You can always come back for more!
The goal is to order as many numbers that are needed, in the most simplistic way possible. These numbers can be customized to the user's preferences via parameters in our API requests.
These queries are then entered into our SignalWire Compatible APIs to search for available local numbers and search for available Toll-Free numbers. Once the correct amount and type of numbers are found, we will then use the create an incoming phone number API to purchase and place these numbers into a space.
You must have the SignalWire Python SDK installed. You can install that HERE
account - 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.
token - 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.
space - 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.
To Begin, we must Connect to SignalWire's APIs through our authentication methods. Make sure to change up the
space variables to those that are in your SignalWire dashboard.
As an added precaution, we have added a filter that will read if any of these variables were not changed. Rather than wasting your time filling in all of the parameters and later finding out these variables were not changed, we saved you the frustration.
program activation variable in the code snippet below. This is used to prevent the program from continuing to run. We will look at this in more detail when we get to the while loop that kicks off our number search.
# Install SignalWire Rest Client from signalwire.rest import Client as signalwire_client # Input User Authentication into Client account="AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE" token="PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" space = "example.signalwire.com" client = signalwire_client(account, token , signalwire_space_url = space) program_activation=0 if space == "example.signalwire.com" or account == "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE" or token == "PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX": program_activation = 1 print("ERROR: Please change the SW_SPACE, SW_PROJ_KEY, and SW_TOKEN variables with your actual info and re-run the program\n")
This code contains multiple while loops in order to prevent the user from entering values into the query that would prevent an error. For example, if the prompt is asking you to enter ( tf/ local) any entry besides 'tf' or 'local' will cause the user to be faced with the same prompt until they enter the query properly.
The first query that the user is prompted for is to specify whether they would like to search through Toll-Free or 10 Digit Long Code (aka Local) phone numbers.
program_activation variable shows up again in order to indicate this first prompt will only be executed if all three of the authorization variables are changed.
while program_activation==0: print('Yo OG, Please type in local for Local numbers or tf for Toll Free (tf / local)') local_or_tf =input()
In order to get a better understanding of how these queries are used, lets take a look at what would happen if the user selected 'local' in their first prompt. They ultimately have two choices of characterization, to customize the region or to customize any special words or numbers to be included.
In filtering by the region, the user can search by a state, a specific area code, or a random area code if they do not have a preference. This entry is then saved and will be used later for entering into our search for available local number API.
Next, we ask the user if they would like to have any special words or numbers included. This entry is then taken and if the user entered 'Y' then the requested parameter is then saved to be added as a filter to our available local number API.
# Begin 10 DLC Number Lookup state_select = 0 local_retry_search =0 local_number_tree=0 retry_loop = 0 contains = 0 if local_or_tf == 'local': while local_number_tree==0: while local_retry_search ==0: while state_select== 0: # Prompt user to choose if they would like to search by a specific state, area code, or if they would like a random area code print('Please enter State to search by State, Area to search by 3 digit area code, or R for random area code? (State/Area/R)') state_or_area = str(input()) if state_or_area == 'R': state_input = '' area_input = '' local_number_tree = 1 state_select = 1 if state_or_area == 'State': print('Please choose a State to purchase from: \n AL AK AZ AR CA CO CT DE FL GA \n HI ID IL IN IA KS KY LA ME MD \n MA MI MN MS MO MT NE NV NJ NM \n NY NC ND OH OK OR PA RI SC SD \n TN TX UT VA WA WV WI WY') state_input = str(input()) area_input='' local_number_tree = 1 state_select = 1 if state_or_area == 'Area': print('Please enter in the 3 digit area code:') area_input = str(input()) state_input='' local_number_tree = 1 state_select = 1 # Prompt user to decide if they would like to include a specific letters or numbers in their search while contains==0: print('Would you like the number to contain specific numbers or a phrase? (Y/N)') contain_Y_or_N = str(input()) if contain_Y_or_N == 'Y': print('Please enter the number or word combination to be included') contain_phrase = str(input()) contains = 1 if contain_Y_or_N == 'N': contain_phrase = '' contains = 1
It is now time to search for what numbers are available that match this criteria. From our code above, we can see that the variables 'state_input' and 'area_code' will never both be containing a full string at the same time. If one contains a user's input, then the other does not. However, the 'contains' variable will include whatever special word or set of numbers are that should be included.
# Search for available numbers using SignalWire's avaialble number API with the parameters defined through previous prompts available_phone_numbers = client.available_phone_numbers("US") \ .local \ .list(in_region=state_input, area_code=area_input , contains=contain_phrase )
After we have searched for these numbers, we must count up how many numbers survive through the search parameters that we chose. Taking the length of the array of available numbers is the simplest way to count up how many options are presented. This entire array is also printed to give the buyer an image of what numbers will be purchased.
Sometimes our search doesn't return the amount of numbers that we would have liked. Not a problem, will the built in retry loop, you will be able to keep searching and searching until you find the numbers that are right for you.
# List all available numbers that meet the criteria of the users for record in available_phone_numbers: avail_num.append((record.phone_number)) print(record.phone_number) print('You have ' +str(len(avail_num)) + ' Numbers that fit this criteria \n') # After user sees how many numbers are available, offer to retry the available number search print('\n Would you like to retry your search? (Y/N)') retry_y_or_n= input() if retry_y_or_n == 'Y': program_activation=0 local_number_tree = 0 local_retry_search = 0 retry_loop=1 state_select = 0 contains = 0 if retry_y_or_n == 'N': program_activation=1 local_number_tree=1 local_retry_search = 1 retry_loop = 1 state_select = 1
With great power, comes great responsibility.
As a preemptive measure to prevent any unwanted purchases, this API will limit the user to only pulling 100 numbers at a time. This means that you will need to execute this code multiple times if you wish to purchase more than 100 at a given time.
Although there may be plenty of numbers available to purchase, if you do not need all of the numbers available, then you may specify the quantity desired. The available number array will then correct to the requested quantity.
If you are just tinkering around with the code and do not want to actually purchase phone numbers, then you can choose to declare this as a dry test run. If the user chooses a test run, they will be given a message that all was successful, but the numbers have not been purchased.
# Prompt user to see how many of the available numbers that the user would like to purchase print('How many numbers would you like to purchase?') num_requested= int(input()) # Create new list of numbers that the user will purchase, given number of numbers user wants to purchase avail_num = avail_num[:num_requested] print('The list of numbers you will be purchasing are' + str(avail_num)) # Give user the option to purchase the numbers or run a dry test run input_test_or_buy =0 test_or_buy_loop = 0 while test_or_buy_loop==0: if input_test_or_buy ==0: print('Was this a Test or would you like to Buy?(Test/Buy)') Test_or_Buy = input() input_test_or_buy =1 # Create a prompt for the user to see when selecting test if Test_or_Buy == 'Test': print('HOORAY! It worked! Re-Run me when youre ready.') test_or_buy_loop = 1
Now, for the moment we have all been waiting for. Purchasing the numbers.
Notice that this will only execute if the user types in 'Buy' when asked in the previous prompt. All numbers that were presented to the user and approved will be looped through the create an incoming phone number request.
# Purchase the list of numbers using SignalWire's available number API if Test_or_Buy == 'Buy': for program_activation in avail_num: incoming_phone_number = client.incoming_phone_numbers \ .create( phone_number= program_activation ) print('Congratulations! You are the proud owner of phone number(s)'+ str(avail_num)) input_test_or_buy =1 test_or_buy_loop = 1
Each one of these numbers will be added directly to your space. Give it a moment to sync up and you will see them appear in your SignalWIre dashboard!
If you would like to test this example out, you can create a SignalWire account and space HERE.
For more information on purchasing numbers through SignalWire's platform, navigate to our resource on incoming phone numbers HERE
Please feel free to reach out to us on our Community Slack or create a Support ticket if you need guidance!
Updated about 2 months ago