Company
Date Published
Author
Greg Baugues
Word count
848
Language
English
Hacker News points
None

Summary

The Twilio Lookup API is a REST API that can verify phone numbers exist, format international phone numbers, determine if a phone number is a cell phone, VOIP, or landline, and discover information about a carrier. The Twilio Ruby gem makes it easy to use the Lookup API in just a few lines of code. To start using the gem, you need to sign up for a free Twilio account, install the gem, and set constants for your credentials. You can then create a Lookup Client and use it to validate phone numbers by looking them up on the Lookup homepage. If the phone number is valid, it will return a JSON object with attributes such as country code, phone number, national format, and URL. The API also provides information about carriers, which can be accessed by adding an additional parameter to the lookup request. However, if the phone number is invalid, the API will return a 404 error, which the Twilio gem throws as an exception. To handle this, you need to catch the exception and check its code to determine if it's a "Not Found" error code (20404) or just raise the exception as normal. The `valid?` function provided in the tutorial demonstrates how to validate phone numbers using the Lookup API.