Identifying a Phone Number's Line Type with Twilio Lookup and Python

February 19, 2019
Written by

clueless 1995 cell phone

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:

  1. Create a Twilio account
  2. Install Python 3
  3. Install the twilio-python helper library

Create a new file called and add the following code.

# Download the helper library from
from import Client

# Your Account Sid and Auth Token from
account_sid = 'your_account_sid'
auth_token = 'your_auth_token'
client = Client(account_sid, auth_token)

phone_number = client.lookups.phone_numbers('+15108675310').fetch(type='carrier')


Replace the placeholder credential values

Swap the placeholder values for your_account_sid and your_auth_token with your personal Twilio credentials. Go to 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:

Reveal your Auth Token in the Twilio Console

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:

    'mobile_country_code': '310',
    'mobile_network_code': '120',
    'name': 'Sprint Spectrum, L.P.',
    'type': 'mobile',
    'error_code': None

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, add the following function:

def line_type(phone_number):
       phone_number (string): phone number in e.164 format

       string: line type associated with the phone number
               possible values are mobile, landline, or voip
   phone_number_type = client \
       .lookups \
       .phone_numbers(phone_number) \
       .fetch(type='carrier') \
       .carrier \
   return phone_number_type

The possible values for line type are mobile, landline, or voip. Next, replace the original phone_number = … and print statements in with the following:

import sys
if len(sys.argv) < 2:
   Please provide a phone number in E.164 format like this:
   $ python +15108675310

input_number = sys.argv[1]

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.

python +12127363100
// landline

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:

Have you built something that takes advantage of Twilio Lookup? Let me know in the comments or on Twitter @kelleyrobinson.