Identifying Phone Number Line Types with Twilio Lookup and Node.js
Time to read: 2 minutes
While the Twilio Messaging API won't charge you for attempting to send an SMS to a landline number, identifying the line type of a phone number can be useful for other reasons. A lot of businesses won't let users sign up with VoIP numbers, for example.
Let's walk through how to identify a phone number's line type with Twilio Lookup and Node.js.
Setting up
For this post you will need:
- Node.js and npm installed (do this first if you haven't already)
- A free Twilio account
- The Twilio Node library
To install the Twilio npm module, navigate to the directory where you want this code to live and navigate to the directory where you want this code to live and run the following command in your terminal to create a package for this project:
The --yes
argument just runs through all of the prompts that you would otherwise have to fill out or skip. Now that we have a package.json
for our app, let’s install the Twilio module with the following shell command:
After this you should be good to write some code!
Looking up the line type with Twilio
You can query the Twilio Lookup API for information about a phone number. There are several types of requests the API can perform including line-type-intelligence
, sim-swap
, and caller-name
. This example focuses on line type intelligence. With the information we get back from this lookup, we can figure out if the phone number is landline, mobile, voip, or another type..
All you need to do a line type lookup in JavaScript is the following code:
Save that to a file called lookup.js
.
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 Node library will use when you run your code.
After setting these environment variables, run your code using the following command:
The possible values for line type include mobile
, landline
, or fixedVoip
, nonFixedVoip
, tollFree
, voicemail
, and more. You can see that the line type for our example number, 1 (855) 747-7626, is nonFixedVoip
because it is a Twilio phone number.
Try running the code again with your own phone number and see what comes back!
What's Next
You can add this code to your Express app, or maybe you want to prompt customers to add SMS (or voice!) based 2FA, clean up your database, or segment your users. You may also be interested in validating phone numbers with Node.js or identifying unknown numbers using a name lookup.
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.