Twilio Lookup is a simple REST API with a ton of utility. You can use Lookup to check whether a number exists, format international numbers to local standards, determine whether a phone is a landline or can receive text messages, and even discover information about the carrier associated with that phone number.

In this post, we’re going to learn how to deal with valid and invalid numbers using the Twilio Python library. This code could work in any context whether you use it to look up customer numbers in your production Django app or just have a basic script you want to run to check numbers in a local database. This should also work regardless of whether you are using Python 2 or 3.

Before getting started you will need to have Python installed as well as pip, which should come with most versions of Python. You will also need to sign up for free Twilio account.

Getting started

First off you should run a virtualenv in order to cleanly install python libraries with pip. If you are unfamiliar with pip and virtualenv you can check out this guide.

Head over to your terminal and install the Twilio Python module: