Identifying Phone Number Carriers in Python with Twilio Lookup
Time to read: 2 minutes
Trolls and bad actors sometimes use phone numbers from free online providers to create fake profiles for making spam calls. Twilio's Lookup API helps you identify the carrier behind the phone number to learn which users have real mobile numbers.
Setting up
To lookup a phone number you will need:
- Python 3 installed (do this first if you haven't already)
- A free Twilio account
- The Twilio Python module
To install the Twilio Python module, navigate to the directory where you want this code to live and make sure you create a virtual environment. Once you have a virtual environment activated, run the following command to install the dependencies:
After this you should be good to write some code!
Looking up a carrier with Twilio
You can query the Twilio Lookup API for information about a phone number. There are a few data packages the API can request including line-type-intelligence
, sim-swap
, and caller-name
. This example focuses on line type intelligence.
All you need to do a carrier lookup in Python is the following code:
Save that to a file called lookup.py
.
Before running this code, make sure to grab your Account SID and Auth Token from your Twilio Console, and save them as environment variables named TWILIO_ACCOUNT_SID
and TWILIO_AUTH_TOKEN
, which the Twilio Python library will use when you run your code.
After setting these environment variables, run your code with your virtual environment activated using the following command:
You can see that the carrier name for our example number, 1 (855) 747-7626, is Twilio. Similar information is returned for mobile and landline carriers such as AT&T or Level 3 Communications.
Try running the request again with your own phone number and see what comes back! Carriers rebrand themselves constantly and that the names used for carriers likely will change over time, so keep this in mind.
Are the carriers returned accurate?
Have you ever gotten a call from a weirdly invalid number or even from your own phone number? You might get a call from a weird 9-digit invalid number -- that can't be right! Given how current telephony systems are set up, anyone can spoof a call or a text from any number, even if they don’t own that number.
Twilio doesn't allow customers to use numbers that they don't own. This practice, unfortunately, doesn’t prevent others from spoofing Twilio serviced numbers. This isn't cool so we're working with other industry leaders to address spoofing. Although we’re optimistic these efforts will have a big impact, we don’t expect them to start paying off until 2020.
After looking up carrier information, what's next?
We know we're not the only ones who are curious who's calling, so we built a public Twilio bot that uses the Lookup API to let you know a number's carrier.
Text +1 (855) 747-7626* with a phone number to check if it is from Twilio. That’s +1 (855) 747-ROBO
*standard text messaging rates may apply
If you're getting spam calls or texts from a Twilio number, this text hotline will help you report it. Learn more about this and what we're doing to stop spam on our platform.
Here are some other things you can build with the Lookup API:
- Validate Phone Numbers
- Identify line type (mobile vs. landline vs. voip)
- Identify robocalls and spam with the Nomorobo Add-on
- Build your own caller ID with caller name Lookup
And as always, Feel free to reach out and share your experiences or ask any questions.
- Email: sagnew@twilio.com
- Twitter: @Sagnewshreds
- Github: Sagnew
- Twitch (streaming live code): Sagnewshreds
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.