Identifying a Phone Number's Line Type with Twilio Lookup and Python
Time to read: 2 minutes
Identifying the line type (i.e. mobile, landline, or VoIP) of a phone number can be useful for many reasons. A lot of businesses won't let users sign up with VoIP numbers, for example.
This post will show you how to identify a phone number's line type with Twilio Lookup and Python.
Set Up
In order to code along with this post you'll want to start with the following:
Create a new file called lookup.py
and add the following code.
Replace the placeholder credential values
Swap the placeholder values for your_account_sid
and your_auth_token
with your personal Twilio credentials. Go to https://www.twilio.com/console and log in. On this page, you’ll find your unique Account SID and Auth Token, which you’ll need any time you send messages through the Twilio Client like this. You can reveal your auth token by clicking on the 'view' link:
Please note: it's okay to hardcode your credentials when getting started, but you should use environment variables to keep them secret before deploying to production. Check out how to set environment variables for more information.
From the command line, run the code with:
You should see output like this:
How to tell if a phone number is mobile or landline or VoIP
This code gave us some useful information, but let's create a reusable function so our application can be used as a handy script. In lookup.py
, add the following function:
The possible values for line type are mobile
, landline
, or voip
. Next, replace the original phone_number = …
and print
statements in lookup.py
with the following:
This includes a bit of error handling to make sure we provide a phone number when we run the script. Now our program can take a phone number as an argument and return its line type!
Try running it again with a new phone number, maybe your personal cell phone or the landline of your favorite pizza delivery shop. Don't forget to put the number in E.164 format.
Ta da! Now we have a handy script we can use to determine the line type of a phone number.
What's Next
Maybe you want to prompt customers to add SMS (or voice!) based 2FA, clean up your database, or segment your users. With this simple code, you have a lot more information about what type of phone number is in your system.
You may also be interested in:
- Build Simple SMS Phone Verification with Twilio Verify and Python
- Identify Unknown Phone Numbers with Python 3, AWS Lambda, Twilio Lookup and SMS
Have you built something that takes advantage of Twilio Lookup? Let me know in the comments or on Twitter @kelleyrobinson.
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.