Skip to main content

DevStack

Automating phone number ordering using simple APIs

Riley James Riley James
Riley James
Share
Image for a blog about automating phone number ordering with APIs

In my first four weeks of interning on the product team at Bandwidth, I had the privilege of working on some exciting projects! 

A customer reached out to Mike, their dedicated Bandwidth account manager, with a need to order a phone number in all 317 area codes that Bandwidth has coverage. Previously, the customer had completed this task using the Bandwidth Dashboard interface. It was easy, but they knew it would take several hours to replicate — and that’s despite the fact that Bandwidth offers one of the fastest interface-driven ordering experiences in the market today! They asked Bandwidth if there might be a way to expedite the process — which would in turn, save valuable time. With this inquiry, we thought: why not automate the whole process of ordering a phone number from every area code? 

The opportunity to work directly with a customer and launch a real product feature was so exciting; and it was especially fun for me, given my background as a Computer Science major. Enter Bandwidth’s phone number automation APIs

On the product team at Bandwidth, I have learned that empathy is the gateway to the best solutions, so it is important to take time to understand our customers’ specific problems. I spoke with the customer to better understand the scope of their request, and determined that we could build a simple app using Bandwidth’s APIs that the customer—and other customers—could easily reuse in the future. I worked with fellow interns on the software development team, Bryan and Rimaz, who wrote the script, to ensure that the requirements were met while minimizing the time and effort required from the customer. 

Bryan best explained how the app works:

“The app was written in Javascript using Node.js. We used Javascript because of its ability to execute tasks without blocking each other. The code takes a file with a list of area codes and leverages Bandwidth’s Phone Number SDKs to make requests to the Bandwidth API. The API handles these requests to order phone numbers, and any responses sent back to the application are reported accordingly.”


const orderPhone = async (code) => {
 data = {
   siteId: process.env.BANDWIDTH_SITE_ID,
   AreaCodeSearchAndOrderType: {
     areaCode: code,
     quantity: 1
   }
 }
 try {
   await numbers.Order.createAsync(data);
   console.log(`${code} order successfully placed.`)
 } catch {
   console.error(`${code} order failed: something went wrong while ordering. Please try again`);
 }
}

The script takes the user’s login credentials, account, and site IDs as variables and interacts with the API through the SDKs. The API checks that for each area code, Bandwidth has a corresponding phone number available for the customer to order. An error message is provided for area codes that do not have a corresponding number available. We initially only wanted requests to be made on available phone numbers, but Bandwidth’s dashboard is designed to include a list of failed orders from the API, so the customer can easily track if they are missing a number from a specific area code and the reason the order was not placed.

const main = async () => {
 for await (areaCode of allAreaCodes) {
   await orderPhone(areaCode);
 }
}

In the original script, orderPhone would wait one second before making the next recursive call in order to remain in accordance with Bandwidth’s rate limits. In the current version, orderPhone waits until it receives a response from the API to make the next call, which speeds up the program while still remaining in accordance with Bandwidth’s rate limits. There is no limit on the number of requests made to the API, but there is a limit on the rate at which requests are made. So a process that manually may take a few hours, can be automatically completed in around 2 minutes — depending upon the speed of the internet! After the user runs the script, one phone number for every area code in the US (that is available) is listed in the account’s orders on the Bandwidth Dashboard, and any failed orders can be viewed in the sub-account’s “alerts” section. 

My mentor, Josh, taught me that making an app is really cool, but making an app that can help a customer is even cooler! I have previously only written code in line with a set assignment, so it was a really cool experience for me to determine requirements and discuss code written by Bryan and Rimaz, from the perspective of the customer. Once the script was functional and the ReadMe was comprehensive, we met with Dan Tolbert who helped us align the app with Bandwidth standards and rate limits, so that it could be added to the Bandwidth GitHub repository and made reusable. 

Mike said that the customer wrote back saying, “Thank you so much!” and that the app worked really well. Wahoo!

Phone Numbers

Learn more

Check out the full GitHub repo at the link below. Have questions about how Bandwidth's APIs can help you solve specific business needs? Get in touch and talk to our experts about what problems you're trying to solve and find out how we can help.

Visit GitHub for all the code Talk to an expert