Dev48
Language
  • About
  • Services
  • Industries
  • Technologies
  • Articles
  • Contacts
Book a call
    Home/Articles/Avoid sending sms to landlines with the lookup v2 api and go
Dev48
info@dev48.org+375 29 320-40-96WhatsApp@isky_by

© 2026 · All rights reserved.

Avoid Sending SMS to Landlines with the Lookup v2 API and Go

Фото: Samuel Angor (Unsplash) — https://unsplash.com/photos/a-close-up-of-a-cell-phone-with-a-keyboard-qbwGUYHiYnI?utm_source=dev48&utm_medium=referral

Source: Twilio•October 7, 2026 at 04:33 AM

Avoid Sending SMS to Landlines with the Lookup v2 API and Go

In this tutorial you'll learn how to filter out landlines in Go using Twilio's Lookup v2 API.

October 07, 2026

Written by

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.

Copy code

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.

Copy code

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.

Copy code

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.

Copy code

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 Twilio RestClient object, 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 calling filterLandLines(), and attempts to send an SMS to each one by calling sendSMS(). 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.

Copy code

You should see output similar to the following printed to the terminal.

Copy code

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.

← All articles

What this article says

More in Telecom & Networks

All →
How to reply to an email: 8+ tips for responding to emails
Twilio

How to reply to an email: 8+ tips for responding to emails

SpaceX’s subsidiary Cursor tells lawmakers it's considering phasing out Chinese AI modelsПресса
SpaceX

SpaceX’s subsidiary Cursor tells lawmakers it's considering phasing out Chinese AI models

How To Import Data From Your CRM Or Another External Source Into Conversation Memory
Twilio

How To Import Data From Your CRM Or Another External Source Into Conversation Memory

What is an SMTP server? How Simple Mail Transfer Protocol works
Twilio

What is an SMTP server? How Simple Mail Transfer Protocol works

Bitcoin suffered identical sell-off pattern ahead of Anthropic, SpaceX IPOs — what happens next?Пресса
SpaceX

Bitcoin suffered identical sell-off pattern ahead of Anthropic, SpaceX IPOs — what happens next?

LiveKit Turn Detector v1.1: end-of-turn detection that reads and hears
LiveKit

LiveKit Turn Detector v1.1: end-of-turn detection that reads and hears

More from Twilio

How to reply to an email: 8+ tips for responding to emails
Twilio

How to reply to an email: 8+ tips for responding to emails

How To Import Data From Your CRM Or Another External Source Into Conversation Memory
Twilio

How To Import Data From Your CRM Or Another External Source Into Conversation Memory

What is an SMTP server? How Simple Mail Transfer Protocol works
Twilio

What is an SMTP server? How Simple Mail Transfer Protocol works

Extending Twilio Verify Beyond Carrier Channels: Passkeys, TOTP, and Push Without the Plumbing
Twilio

Extending Twilio Verify Beyond Carrier Channels: Passkeys, TOTP, and Push Without the Plumbing

The absolute best time to send email campaigns in 2026
Twilio

The absolute best time to send email campaigns in 2026

Deliver Greater Precision, Context, and Efficiency with Enhancements to Twilio Segment Journeys
Twilio

Deliver Greater Precision, Context, and Efficiency with Enhancements to Twilio Segment Journeys