Identifying Phone Number Line Types with Twilio Lookup and Node.js

January 06, 2023
Written by
Sam Agnew
Twilion

Copy of Generic Blog Header 2 (3).png

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:

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:

npm init --yes

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:

npm install twilio@3.84.0 --save

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-intelligencesim-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:

// Set environment variables for your Account Sid and Auth Token!
// These can be found at twilio.com/console
const client = require('twilio')();

client.lookups.v2
  .phoneNumbers("+18557477626")
  .fetch({ fields: 'line_type_intelligence' })
  .then((pn) => {
    console.log(pn.lineTypeIntelligence); // All of the carrier info.
    console.log(pn.lineTypeIntelligence.type); // Just the number's line type.
  });

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:

node lookup.js

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.