Validate Phone Number in Python

Cloudmersive
2 min readFeb 1, 2022

--

Number not in service? It’s nice to know if that’s the case before you make the call. Using the Cloudmersive Validate API, you can make sure a phone number is valid & get additional information about it including national & international formatting, country code & name, and more. The specific iteration you can use for this is called /validate/phonenumber/basic — below, we’ll run through how you can connect in Python.

To begin connecting to this API, start by running the below Python SDK installation command:

pip install cloudmersive-validate-api-client

Next you can start off the function with the below snippet:

from __future__ import print_function
import time
import cloudmersive_validate_api_client
from cloudmersive_validate_api_client.rest import ApiException
from pprint import pprint

After that, you’ll need to authorize your API key using the below code:

# Configure API key authorization: Apikey
configuration = cloudmersive_validate_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'

If you don’t already have a Cloudmersive API key, you can get one easily by creating a free account on the Cloudmersive website.

For the final step, copy & paste the remaining code for the callback function:

# create an instance of the API class
api_instance = cloudmersive_validate_api_client.PhoneNumberApi(cloudmersive_validate_api_client.ApiClient(configuration))
value = cloudmersive_validate_api_client.PhoneNumberValidateRequest() # PhoneNumberValidateRequest | Phone number to validate in a PhoneNumberValidateRequest object. Try a phone number such as \"1.800.463.3339\", and either leave DefaultCountryCode blank or use \"US\".
try:
# Validate phone number (basic)
api_response = api_instance.phone_number_syntax_only(value)
pprint(api_response)
except ApiException as e:
print("Exception when calling PhoneNumberApi->phone_number_syntax_only: %s\n" % e)

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet