DEV Community

Alexey D
Alexey D

Posted on

How to Validate Phone Numbers in Python — Free API with Fraud Risk Scoring

How to Validate Phone Numbers in Python — Free API with Fraud Risk Scoring

Every app that collects phone numbers faces the same problem: users enter junk. Typos, fake numbers, VoIP burners, numbers from the wrong country — you only find out when your SMS campaign bounces or a fraudster slips through.

This tutorial shows how to validate phone numbers in Python and JavaScript using a free API that returns not just validity — but carrier info, line type, and a fraud risk score.

The API: Phone Validator Pro

👉 Phone Validator Pro on RapidAPI

  • 240+ countries
  • Line type detection: MOBILE / LANDLINE / VOIP / TOLL_FREE
  • Carrier identification
  • Timezone info
  • Batch endpoint (up to 50 numbers)

Sample Response

{
  "is_valid": true,
  "international_format": "+1 212-555-2000",
  "e164_format": "+12125552000",
  "country": "United States",
  "carrier": "AT&T",
  "line_type": "MOBILE",
  "timezones": ["America/New_York"]
}
Enter fullscreen mode Exit fullscreen mode

Python Example

import requests

def validate_phone(phone, country_code="US"):
    url = "https://phonevalidatorpro.p.rapidapi.com/validate"
    headers = {
        "X-RapidAPI-Key": "YOUR_KEY",
        "Content-Type": "application/json"
    }
    r = requests.post(url, json={"phone": phone, "country_code": country_code}, headers=headers)
    data = r.json()

    if not data["is_valid"]:
        return {"valid": False}

    return {
        "valid": True,
        "country": data["country"],
        "carrier": data["carrier"],
        "line_type": data["line_type"],
        "e164": data["e164_format"]
    }

# Usage
print(validate_phone("+79161234567", "RU"))
# {"valid": True, "country": "Russia", "carrier": "MTS", "line_type": "MOBILE", "e164": "+79161234567"}

print(validate_phone("+12125552000", "US"))
# {"valid": True, "country": "United States", "carrier": "AT&T", "line_type": "MOBILE", ...}
Enter fullscreen mode Exit fullscreen mode

JavaScript Example

async function validatePhone(phone, countryCode = "US") {
  const res = await fetch("https://phonevalidatorpro.p.rapidapi.com/validate", {
    method: "POST",
    headers: {
      "X-RapidAPI-Key": "YOUR_KEY",
      "Content-Type": "application/json"
    },
    body: JSON.stringify({ phone, country_code: countryCode })
  });

  const data = await res.json();

  if (!data.is_valid) {
    console.log("Invalid number");
    return null;
  }

  console.log(`${data.country} | ${data.carrier} | ${data.line_type}`);
  return data;
}

validatePhone("+447911123456", "GB");
// United Kingdom | EE | MOBILE
Enter fullscreen mode Exit fullscreen mode

Batch Validation

Validate up to 50 numbers in one request:

import requests

numbers = [
    {"phone": "+79161234567", "country_code": "RU"},
    {"phone": "+447911123456", "country_code": "GB"},
    {"phone": "+12125552000", "country_code": "US"}
]

r = requests.post(
    "https://phonevalidatorpro.p.rapidapi.com/batch",
    json=numbers,
    headers={"X-RapidAPI-Key": "YOUR_KEY", "Content-Type": "application/json"}
)

for item in r.json():
    status = "" if item["is_valid"] else ""
    print(f"{status} {item.get('e164_format', item.get('phone'))} | {item.get('line_type', 'N/A')}")
Enter fullscreen mode Exit fullscreen mode

Practical Use Cases

  • Sign-up forms — block invalid numbers before sending OTP
  • CRM cleanup — batch-validate your contact database
  • SMS campaigns — filter out landlines and VoIP
  • Fraud prevention — flag VOIP numbers at registration

Free Tier

Free tier is available — no credit card required.

👉 Subscribe here: Phone Validator Pro on RapidAPI


Also check out my other APIs: Email Validator Pro | Language Detector Pro

Top comments (0)