This content originally appeared on Twilio Blog and was authored by Sam Agnew
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:
pip install twilio==7.16.0
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:
from twilio.rest import Client
# Set environment variables for your Account Sid and Auth Token!
# These can be found at twilio.com/console
client = Client()
phone_number = client.lookups \
.v2 \
.phone_numbers('+18557477626') \
.fetch(fields='line_type_intelligence')
print(phone_number.line_type_intelligence) # All of the carrier info.
print(phone_number.line_type_intelligence['carrier_name']) # Just the carrier name.
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:
python lookup.py
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
This content originally appeared on Twilio Blog and was authored by Sam Agnew
Sam Agnew | Sciencx (2019-12-06T20:45:44+00:00) Identifying Phone Number Carriers in Python with Twilio Lookup. Retrieved from https://www.scien.cx/2019/12/06/identifying-phone-number-carriers-in-python-with-twilio-lookup/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.