Build a carrier block list with Twilio Lookup
Time to read: 3 minutes
Bad actors can target phone verification or two-factor authentication flows to artificially inflate SMS traffic. Certain carriers, knowingly or not, allow this to happen and the fraudsters make money off of this inflated traffic. This could lead to higher than expected Twilio traffic for you.
Our Verify API for sending one-time passcodes includes comprehensive fraud mitigation mechanisms. However, no provider-side solution can guarantee 100% effectiveness against sophisticated attackers, which is why we encourage customer participation in preventing attacks.
Fortunately, Twilio's Lookup API can detect carriers and we can use this to filter out known bad actors before sending an SMS verification.
Here's a quick snippet of an example block list:
The rest of the post will cover building this solution with the Twilio Lookup API.
How to define a carrier in code: MCC and MNC
The mobile country code (MCC) and mobile network code (MNC) combination is globally unique and is used to identify carriers worldwide. Note: MCC and MNC are not available for landline numbers.
This data is returned in a carrier Lookup response when available:
Prerequisites for building a carrier detection list
To code along with this post you'll need to:
Step 1: Sign up for or sign into your Twilio account
Step 2: Install Node JS https://nodejs.org/en/download
Step 3: Install Twilio CLI (learn more)
Step 4: Install the Serverless CLI plugin
Step 5: Login to the CLI with your Twilio account credentials
Download the Lookup starter template
Using the CLI, clone the international telephone input template from the Twilio code exchange:
Open up the .env file and make sure your Twilio Account Credentials (ACCOUNT_SID
and AUTH_TOKEN
) have been populated, you can find your credentials in the Twilio Console. You can start the app by running:
Open http://localhost:3000/index.html in a browser; you should be able to input your phone number and see it in E.164 format:
The application already does a really handy thing by accepting international formatted numbers and converting them to the standard E.164 format. The Lookup API then validates whether the number is valid or not. If you try inputting just 12345
or any other invalid phone number you'll get an error.
Add a block list to your sign-up form
To add a block list, open up the functions/lookup.js
file and add the following line after const client = …
:
This list is using mobile country codes (MCC) and mobile network codes (MNC) to identify carriers, which the Lookup API also uses. Our list is fictional, but you can determine MCC-MNC combinations with this site. In order to segment your verification by different MCC+MNCs, start tracking these details so you have more insights around which carriers you can trust more or less.
If you look at the rest of the function, you might notice that we're not actually using the response from the Lookup API. To make use of the response and grab the carrier that's returned, replace the 5 line block of code that starts with response.setStatusCode(200);
with:
Finally, head over to assets/index.html
and update line 73 to report the function's error message:
Make sure the project is running with twilio serverless:start
, head over to http://localhost:3000/index.html and test it out! Then try adding your mobile number MCC-MNC combination to the block list to see the error message "Carrier not allowed."
Alternatives to blocking carriers
Blocking an entire carrier does involve the risk of blocking legitimate traffic, so you could use the Lookup API response to detect riskier numbers and add protections like reCAPTCHAs to prevent bots.
You can also block or allow verifications from certain countries with a similar method described in this blog post.
You could also use the WhatsApp channel instead of SMS to send verification codes (coming soon to the Verify API).
What to do if you suspect fraud on your account
You can email fraud@twilio.com if you are facing messaging abuse on the Authy or Verify APIs. Please include the following details in your message:
Other uses for the Lookup API
You can also use the Lookup API to allow or detect certain countries, another way to prevent fraud and decrease the surface area of your application.
Lookup can also be used to detect line type to determine mobile vs. landline vs. VoIP numbers.
Of course, once you've implemented Lookup you'll want to Verify phone numbers using Twilio's Verify API. Here are some more resources for account security that you might enjoy:
- Lookup API documentation
- Best practices for phone number validation during new user enrollment
- Best practices for managing retry logic with SMS 2FA
- Send an SMS OTP in 5 minutes
I can't wait to see what you build and secure with Twilio!
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.