How to Verify Phone Numbers in Ruby with the Lookup API

Twilio Lookup is a REST API that can:

  • Verify that a phone number exists
  • Format any international phone number into its local standard
  • Determine if a phone number is a cell phone, VOIP or landline
  • Discover information about a phone number’s carrier

In this tutorial we’ll use the Twilio Ruby gem to get phone number data from the Lookup API in just a few lines of code.

To start, sign up for free Twilio account.

try-twilio

Getting Started

Install the Twilio gem:

Grab your Account SID and Auth Token from the Twilio dashboard:

twilio-credentials

In your code:

  • require the gem
  • set constants for our credentials (you’ll want to use environment variables in a real app)
  • create a Lookup Client

Armed with a REST client, you’re ready to lookup some numbers.

Valid Phone Numbers

Let’s start by looking up a valid phone number.

If we punch the number my dog uses to send selfies into the Lookup homepage, we’d see that a successful Lookup returns a JSON object:

lookup-number

Here’s how you do that lookup in Ruby:

If the phone number is valid, the gem converts the JSON attributes into methods on the response object:

This lookup is free, but only has information about the number itself. If you’d like to get information about the carrier, it’ll cost you half a penny ($0.005). This is especially useful for determining if a phone number is capable of receiving an SMS/MMS.

To perform a carrier lookup, simply add an additional parameter:

Then you can access carrier data like this:

Try it with your own phone number and see what happens.

Invalid Phone Numbers

Before we talk about invalid phone numbers, let’s talk about bit about REST APIs.

Imagine you have a RESTful web app that serves as an online bookstore. The unique id for each book is a sluggified version of its title. For instance, to view the details of Bob Martin’s The Clean Coder (great book!), we would do:

Now, what happens if we try to GET the details of a book that doesn’t exist? To the best of my knowledge no one’s written a book called The Dirty Coder (though someone certainly should). So if we try this:

We’ll get a 404 because the book’s not found, right?

Twilio’s Lookup is a REST API. Conceptually, you can think of it as an online phonebook with with phone numbers serving as the unique ID. When a phone number is not found, we get a 404.

We can see this as well on the Lookup homepage:

invalid-phone-lookup

When the Twilio gem encounters a 404, it will throw an exception. Thus, whenever we’re using the Lookup API to validate phone numbers, we need to catch the exception and check to see if the error matches Twilio’s “Not Found” error code: 20404.

To write a function that validates a phone number, we need to:

  • Initiate a lookup client
  • Look up the phone number and try to access an attribute on it
  • If that works successfully, return true
  • If it raises an exception, return false if the error is a 20404 (otherwise just raise the exception as normal)

Try it out with a real and fake number and see what happens :

What we learned

Let’s recap:

  1. Twilio Lookup is a REST API phone book. Lookup doesn’t serve to answer the question “Does this number exist?”. It serves to give data about valid phone numbers and returns a 404 when a phone number is not found — just as a REST API should.
  2. The Twilio gem throws an exception on 404s. If you can successfully retrieve information about a phone number, you know the phone number is valid. If Ruby throws an exception when you try to lookup a phone number, chances are that the phone number is invalid (but check the error code just to be sure).

We’d love to hear what you do with the Lookup API. The most obvious use cases for Lookup are phone number validation, phone number formatting, and determining if a phone number can receive text messages. That said, like most of our products, the coolest use of this API is probably going to be something that a developer like you dreams up that we never even considered.

We can’t wait to see what you come up with.

If you enjoyed this post, you may want to follow me on twitter. If you have any questions, drop me an email at gb@twilio.com.

  • ad16

    Getting an undefined method `code’ when using method rewritten verbatim. Any clues?