How to Confirm Phone Number Ownership with Lookup Identity Match in Python
Time to read:
There's a lot you can learn from a phone number. Lookup Identity Match uses phone numbers as an identity to provide a zero-knowledge match against authoritative data sources like mobile carrier records. This allows you to build real-time confirmation that you're interacting with a real person linked to the phone number provided.
Use Identity Match to prevent fake accounts and improve trust with legitimate, unique users. Customers can use Identity Match at sign up, login, and even during account changes or checkout. Best of all, Identity Match happens seamlessly in the background, with lower costs and friction than alternatives like credit bureau and document verification used to satisfy Anti-Money Laundering (AML) rules.
This blog post will show you how to use Twilio Lookup Identity Match to confirm phone number ownership and reduce sign up fraud.
Here's a sneak peak at what the Identity Match request looks like. Learn more about accepted parameters and coverage in the documentation.
Prerequisites
To code along with this post you'll need:
- A Twilio account. Sign up or sign in.
- Submit this form for Identity Match approval (Outside the US and Brazil only; takes 2-4 weeks)
- Python 3.14 ( download)
- pip
Build the app
This post builds a simple Flask application with a phone number input form as a starting point, but you could use your own sign up form instead. The base form relies on the excellent intl-tel-input plugin for collecting phone numbers in E.164 format.
You can find the source code on GitHub.
Step 1: Set up the base project
Create a new project directory:
Create a requirements.txt file:
Create and activate a virtual environment:
Install the dependencies:
Create a .env file and add your Twilio Account SID and Auth Token; you can find both on the front page of your Twilio Console:
Create app.py with the following:
Create a static directory and add static/index.html:
Also download static/styles.css from the completed project on GitHub and save it to your static directory.
Test out the project by running flask run, you should be able to open http://localhost:5000 and see a phone number input form.
Identity match needs information about the phone number owner to match against. Parameters can be any combination of name, address, national ID or date of birth.
This example is going to use first and last name, so you need to start by making sure your form has fields to collect those separately:
Open static/index.html file and replace everything inside the <form> tags with the following:
This adds a first name and last name input in addition to the phone number input. Further down in index.html in the script section, add the following code under const data = … to extract the name input values.
Then, head over to app.py file to make use of the data and call the Lookup Identity Match API. The existing code already calls the basic Lookup API for formatting and validation, so you need to add the additional data parameters to tell the API to use the Identity Match package.
Add first_name and last_name to the form data extraction after the phone variable:
Then, update the .fetch() call to pass the Identity Match fields:
Step 3: Interpret the results of a Lookup Identity Match response
After calling the Lookup API, you need to decide what to do with the information in the response. Here's what the identity match portion of a sample response looks like:
Possible responses include exact_match, no_match, and no_data_available. Name and address fields also support partial matches. Learn more about match scores in the documentation. The API will respond null if you don't provide an input to match on.
This application is only looking at name matches and will consider the match a success if it's an exact match or high partial match (e.g. Kelly instead of Kelley – happens all the time).
Back in app.py, replace success = result.valid and the if not success block with the following:
Then replace return jsonify({"success": True}) with:
Finally, make two small changes to display the response message in the index.html file. Replace the inside of the if (json.success) { block with:
And since you're no longer only checking the phone number, update the error message line (error.innerHTML) in the else block a few lines below:
Run the application
Navigate back to your terminal, and restart the application with flask run (enter CTRL + C first if it's still running). Now head back over to http://localhost:5000 and test it out!!
Next steps for identity verification
You can extend your application with other Lookup packages like Line Type Intelligence, SIM swap detection and more. Check out our documentation to learn more about what's possible with the Lookup API.
Another way to prevent invalid phone numbers is to do phone verification - we also have an API for that! I can't wait to see what you build and secure.
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.