This project will show you how to look up a phone number to get different types of information including:
- Validation and Formatting. Detects invalid phone numbers and parses numbers into the standardized E.164 format
- Carrier. Detects line type (i.e. mobile, landline, voip) and carrier.
- Caller name. Subscriber info, US only.
More details in the Lookup API documentation.
For a production use case we recommend adding phone verification, which is a best practice for collecting phone numbers from your users in order to make sure they have control of the number.
The best way to use the Function templates is through the Twilio CLI as described below. If you'd like to use the template without the Twilio CLI, check out our usage docs.
This project requires some environment variables to be set. To keep your tokens and secrets secure, make sure to not commit the .env
file in git. When setting up the project with twilio serverless:init ...
the Twilio CLI will create a .gitignore
file that excludes .env
from the version history.
In your .env
file, set the following values:
Variable | Meaning | Required |
---|---|---|
ACCOUNT_SID |
Find in the console | Yes |
AUTH_TOKEN |
Find in the console | Yes |
lookup.js
expects the following parameters:
Parameter | Description | Required |
---|---|---|
phone |
Phone number in E.164 format | Yes |
types |
Optional. Can provide 'carrier' and 'caller-name' for additional details, defaults to formatting lookup. | No |
- Install the Twilio CLI
- Install the serverless toolkit
twilio plugins:install @twilio-labs/plugin-serverless
- Initiate a new project
twilio serverless:init lookup-sample --template=lookup && cd lookup-sample
- Add your environment variables to
.env
:
Make sure variables are populated in your .env
file. See Environment variables.
- Start the server :
npm start
- Open the web page at https://localhost:3000/index.html and enter your phone number to test
ℹ️ Check the developer console and terminal for any errors, make sure you've set your environment variables.
Deploy your functions and assets with either of the following commands. Note: you must run these commands from inside your project folder. More details in the docs.
With the Twilio CLI:
twilio serverless:deploy