Validating Phone Numbers Using Twilio Lookup API in Rust
Time to read:
Validating Phone Numbers Using Twilio Lookup API in Rust
Validating phone numbers is important for many applications like sign-ups, messaging, and customer support. It helps to make sure the phone numbers are real and can be contacted.
In this project, we will build a small command-line tool in Rust that checks if a phone number is valid using the Twilio Lookup API. Twilio is a popular service that provides reliable information about phone numbers.
We choose Rust because it is fast, safe, and easy to use for building small command-line apps. By the end, you will have a tool that takes a phone number as input and tells you if it is valid and some extra details like the phone type or country.
Prerequisites
- Basic knowledge of Rust and CLI applications
- A Twilio account (free or paid). Create one if you don't already have one.
- Rust and Cargo
Configure the required environment variables
To avoid hardcoding your Twilio credentials in your Rust code, it’s best to use environment variables. Specifically, you need to store your Account SID and Auth Token. In your project root, create a new file named .env, which is not shared publicly. Then, paste the code below into the new file:
Using the rust-dotenv crate, your Rust app will load these variables at runtime automatically. This way, you can easily change credentials without touching your source code.
Retrieve your Twilio credentials
With your .env file in place, the next step is to retrieve your Twilio credentials. Go to the Twilio Console and sign in to your account. On the console dashboard, locate the Account Info section. There, you’ll see your Account SID and Auth Token. These credentials authenticate your requests to Twilio services, including the Lookup API.
Copy both values and paste them into the .env file you created earlier, replacing the <your_account_sid_here> and <your_auth_token_here> placeholders, respectively.
Set up the Rust project
Now that you have your Twilio credentials, let’s set up the Rust project where we will build the CLI app.
Open your terminal and run the command below to create a new Rust project named "phone_validator" (though, you can choose any name that you prefer) using Cargo:
This creates a new folder named "phone_validator" with the basic Rust project files.
Add the required dependencies
We need a few libraries (called “crates” in Rust) to help us:
- reqwest: To make HTTP requests to Twilio API
- serdeand serde_json: To handle JSON data
- clap: To parse command-line arguments
- dotenv (optional): To load environment variables
To add them, open the Cargo.toml file in the project's top-level directory and add the following in the "dependencies" section:
Create the application logic
The main part of your Rust CLI app is the logic that takes a phone number from the user, sends it to the Twilio Lookup API, and handles the response.
It uses the clap crate to read the phone number input on the command line. Then, it creates an HTTP request with the reqwest crate, including your Twilio credentials for authenticated access. After sending the request to the Twilio Lookup API, it receives a JSON response with details about the phone number. It then uses Serde to turn this JSON into Rust structs, making it easy to get information like whether the number is valid, its country code, and its type (such as mobile or landline).
Open main.rs in the project's top-level directory and paste the code below into the file.
The code above checks if the phone number provided is valid using the Twilio Lookup API. It loads your Twilio credentials from environment variables using rust-dotenv, reads the phone number from the command line, and sends a GET request using reqwest.
The JSON response is parsed into Rust structs with Serde. If the number is valid, it prints details like the national format, country code, and carrier info. It also handles errors such as missing numbers or API issues, based on the response status.
Test that the application works
To test the application, run the command below after replacing <Your Phone Number> with your phone number in E.164 format.
This will trigger a request to the Twilio Lookup API and display the result in the terminal. You can also test with invalid or incomplete numbers to check if error handling works as expected.
The image below shows the result of the output if it's successful, given that the mobile number supplied is valid:
Else, if the mobile number is invalid, the output below shows the result
That is how to validate phone numbers using Twilio's Lookup API in Rust
That is how to validate phone numbers using the Twilio Lookup API in Rust — by securely loading your Twilio credentials, accepting a phone number through a simple CLI, and making an authenticated HTTP request to the API.
With proper error handling and clear output, this app not only helps ensure accurate phone data, but also serves as a great starting point for building more powerful Rust tools that interact with external APIs.
Jesuleye Marvellous Oreoluwa is a software engineer who is passionate about teaching technology and enjoys making music.
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.