Avoid Sending SMS to Landlines with the Lookup v2 API and Go
Time to read:
Avoid Sending SMS to Landlines with the Lookup v2 API and Go
SMS is one of the most effective ways of staying in touch with your customers. Why? Because:
- SMS delivery isn't reliant on an internet connection
- SMS are delivered almost instantly
- SMS works on every mobile phone
- SMS has an open rate of 98% (according to Forbes)
But, what if you have an application where users have provided landlines as well as mobile/cell numbers? How do you filter out landlines so that you don't receive intermittent errors and avoid paying for failed SMS.
Well, this tutorial will show you how to do so in Go using Twilio's Lookup v2 API.
Prerequisites
To follow along with this tutorial, you'll need the following:
- A Twilio account (whether free or paid). Create one now if you don't have one already.
- A Twilio phone number that can send SMS
- Go 1.23 or above
- A phone number that can receive SMS and a landline number (preferrably, one that belongs to you or a friend)
- Your preferred text editor or IDE for writing Go
Build the Go application
The first thing to do is to create an empty Go project with module support. To do that, run the following commands wherever you store your go projects.
Add the required libraries
Next, the project will need a few dependencies, these are:
- Twilio's Go Helper Library: This simplifies interacting with Twilio's APIs
- GoDotEnv: This loads environment variables from .env files
To install them, run the following command.
Set the required environment variables
You need to set two environment variables which are required to make authenticated requests to Twilio's Lookup v2 API. To do that, in the project's top-level directory, create a new file named .env and paste the configuration below into the new file.
Then, open the Twilio Console in your browser of choice, and copy the Account SID, Auth Token from the Workbench at the bottom of the page.
Now, set the copied values as the values of TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN respectively, in .env.
Then, under Quick actions click Phone numbers, and copy an E.164-formatted phone number from the Phone Number column. Paste the phone number as the value of , and TWILIO_PHONE_NUMBER in .env.
Write the code
Now, it's time to write the code. Just one file. Create a new file named main.go in the project's top-level directory and in that file paste the code below. After that, replace <A MOBILE PHONE NUMBER> and <A LAND LINE NUMBER> with a mobile phone number and a land line, respectively.
The code above defines three functions:
main(): is the application's entrypoint. It starts by loading the environment variables registered in .env, before initialising a new TwilioRestClientobject, and an array of phone numbers to validate using Twilio's Lookup v2 API. Following that, the code iterates over the list of phone numbers returned from callingfilterLandLines(), and attempts to send an SMS to each one by callingsendSMS(). The user is notified whether the SMS is sent successfully or not through text printed to the terminal.filterLandLines(): uses a custom iterator to filter landlines from an array of phone numbers, returning an iterator of strings. As the iterator iterates over the array, it attempts to fetch the details of each phone number from Twilo's Lookup v2 API including Line Type Intelligence information. This information is necessary as it contains the phone number's type. If the phone number type is not set to "mobile", the number is not included in the returned iterator.sendSMS(): is a small utility function that sends an SMS to the recipient with the message provided.
Test that the code works as expected
With the code written, let's see it in action, by running the following command in a new terminal tab or session.
You should see output similar to the following printed to the terminal.
That's how to avoid sending SMS to landlines in Go with Twilio's Lookup v2 API
Thanks to the power of Twilio's Lookup v2 API and Go's standard library, you don't need to write much code at all to filter landlines from a list of phone numbers. Make sure you use this functionality to avoid being charged for failed SMS, and create more robust Go applications that integrate with Twilio.
Matthew Setter is the PHP, Go, and Rust editor on the Twilio Voices team, and a developer in each of these languages. He’s also the author of Mezzio Essentials and Deploy with Docker Compose. You can find him at msetter[at]twilio.com, and on LinkedIn and GitHub.
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.