How to Purchase Numbers in Bulk in cURL

Below is a working bash script example using the SignalWire REST API to purchase numbers in bulk. It generally takes two actions to purchase a single phone number.

  • Get a list of available phone numbers function
sw_bulk_number_available () {
  • Purchase a number from the list function
sw_bulk_number_available () {

Code Breakdown

Let's explain some key parts of each function.

In the available function, for optimal randomization purposes, we get a list from a Region instead of an area code, for local numbers. We're making a curl request to SignalWire with the InRegion attribute, which will give back a JSON response of 50 numbers. Then, we use a linux utility "jq" to parse the JSON response, giving us only the numbers themselves.

declare -a REGION=(AL AK AZ AR CA CO CT DE FL GA

               HI ID IL IN IA KS KY LA ME MD
               MA MI MN MS MO MT NE NV    NJ
               NM NY NC ND OH OK OR PA RI SC
               SD TN TX UT VA    WA WV WI WY)
    RAND=$(( RANDOM % (47 - 0 + 1 ) + 0 ))
    RANDOM_REGION=${REGION[${RAND:-1}]}
    PHONE_LIST=$(curl --silent https://$SW_SPACE_URL/api/laml/2010-04-01/Accounts/$SW_PROJ_KEY/AvailablePhoneNumbers/US/Local.json \
         -X GET \
         -d "InRegion=$RANDOM_REGION" \
         -u "$SW_PROJ_KEY:$SW_TOKEN" \
        | jq --raw-output '.available_phone_numbers[].phone_number' | head -n ${2:-1})

We do a similar curl request for TollFree numbers. Instead of specifying InRegion= as above, we're specifying AreaCode=.

declare -a AREACODE=( 833 844 855 866 877 888 )
    RAND=$(( RANDOM % 6 ))
    RANDOM_AREACODE=${AREACODE[${RAND:-4}]}
    PHONE_LIST=$(curl --silent https://$SW_SPACE_URL/api/laml/2010-04-01/Accounts/$SW_PROJ_KEY/AvailablePhoneNumbers/US/TollFree.json \
         -X GET \
         -d "AreaCode=$RANDOM_AREACODE" \
         -u "$SW_PROJ_KEY:$SW_TOKEN" \
        | jq --raw-output '.available_phone_numbers[].phone_number' | head -n ${2:-1})

​​​​​​In the purchase function, because SignalWire returns a list of 50 numbers at a time, we need to refresh our list when it is exhausted, hence the sub-loop, until total numbers have been reached. Numbers are purchased individually in the inner-loop's curl request, and the list is refreshed with a new area code in the outer-loop's callback to sw_bulk_number_available $TYPE $ATOTAL.

while [ $t -lt $TOTAL ]; do
    sw_bulk_number_available $TYPE $ATOTAL
    while read -r PHONE; do
        if  [ $ACTION != dry ]; then
        printf "$p: puchased $PHONE\n"
        curl https://$SW_SPACE_URL/api/laml/2010-04-01/Accounts/$SW_PROJ_KEY/IncomingPhoneNumbers.json \
             -X POST \
             --data-urlencode "PhoneNumber=$PHONE" \
             -u "$SW_PROJ_KEY:$SW_TOKEN"
        else
        printf "$p: $PHONE from ${RANDOM_REGION:-$RANDOM_AREACODE} is available\n"
        fi
        p=$[$p+1]
    done < <(printf '%s\n' "$PHONE_LIST")
    t=$[$t+$ATOTAL]
    done

This script can be used to purchase any total of numbers up to the bounds of your account permissions. Please use the script with care. Alterations and abuse may cost you considerably. SignalWire will not be liable for costs incurred by misuse of this script.

By default, if you blindly blast your way through the defaults with the Enter key, the script will do a "dry" run, only listing the available numbers, and only 1 number, from 1 area code at that. This gives you the chance to tinker with the nuances of the script when raising quantities. You'll have to type "purchase" to actually make a purchase. See notes inside scripts for usage.


Did this page help you?