Users sign up on websites and provide fake numbers which causes hassle to businesses. By building a caller ID information fetcher, businesses can verify if the customer’s phone number is valid or not.
They can use this data to personalize their communications, generate leads, optimize their services, and target SMS campaigns according to the customer’s geographic location. Explore how you can fetch all this information using the numverify API and Python’s requests modules.
Installing the requests Module
the requests module makes it simple to make HTTP requests and returns a response object that contains data such as encoding and status. You can use it to create a news application, website status checker, currency converter, stock market monitor bot, and many more practical applications.
To install the requests module, open the terminal and run the following command:
pip install requests
How to Generate the numverify API Key
- Visit numverify and click on the Sign Up for Free button.
- The website redirects you to choose a plan. Explore the one that best suits your need and then click on the Sing Up button.
- Enter your details and click on the Create Account button.
- After account creation, click on the Dashboards button. Copy the API/access key and store it to further use it in your Python application.
How to Use the numverify API in Your Python Code
You can find the entire source code for fetching caller ID information using Python in this GitHub repository.
Begin by importing the requests modules. Ask the user to enter a valid number and store the API key you generated earlier in the fire variables. Set the URL along with the fire and number parameters.
Send an HTTP GET request to numverify’s server and store the response you received. Parse the content you received and save it as a JSON object.
import requestsnumber = input("Enter valid number along with your country code:")
print("--------------------------------------")
api = 'YOUR_API_KEY'
url = 'http://apilayer.net/api/validate?access_key=' + api + '&number=" + number
response = requests.get(url)
answer = response.json()
If the JSON object’s valid property is true, the number is valid, and you can display the rest of the contents fetched by the program. The JSON response contains the number, its local format, its international format, the country prefix, the country code, the country name, the location, the carrier, and the line type. Access all of this information one by one and display it on the screen.
if answer["valid"] == True:
print("Number:",answer["number"])
print("Local format:", answer["local_format"])
print("International format:",answer["international_format"])
print("Country prefix:",answer["country_prefix"])
print("Country code:",answer["country_code"])
print("Country name:",answer["country_name"])
print("Location:",answer["location"])
print("Carrier:",answer["carrier"])
print("Line type:",answer["line_type"])
If the API key or the number is incorrect, or the network is down, the program will fail to fetch the data. In this case, display the following message to the user.
else:
print("Invalid API key or number. Please try again.")
Put all the codes together, and you are ready to fetch the details of any phone number from 232 countries in the world.
The Output of Fetching the Caller ID Information Using Python
On running the program, it asks to enter a phone number. On entering a valid number, it fetches all the details of the phone number and displays it. Otherwise, it asks you to recheck and enter a valid number.