Skip to main content

client.phone_number_validation

Phone Number Validation

Validates a phone number and returns its country, its national and international formats, and the network it was originally assigned to.

Requires an API Key licensed for phone validation.

Every query decrements your lookup balance, including a number that fails to parse and a number reported as invalid.

Endpoint​

GET /phone_numbers

See the API reference for this endpoint.

Signature​

def phone_number_validation(query:, current_carrier: nil, tags: nil)

Arguments​

NameTypeRequiredDescription
keywordskeyword argumentsyesOne per query parameter, named as the API names them. A keyword left out or nil is not sent.

Query Parameters​

Pass these as keyword arguments.

NameTypeRequiredDescription
queryStringyesSpecifies the phone number to validate. Phone number must include a country code in an acceptable format. For instance, UK phone numbers should be prefixed with +44, 44 or 0044.
current_carrierStringnoWhen set to true, the API retrieves and populates the current network of the phone number.
tagsString, Array<String>noTags

Response​

AddressZen::Responses::PhoneNumberResponse. Read fields with readers named as the API names them, such as response.result, or with response["result"]. to_h returns the decoded body.

Errors​

An HTTP error raises AddressZen::ApiError with the HTTP status, the API error code and the decoded body. A transport failure raises AddressZen::TransportError. See error handling.

Example​

phone-number-validation.rbruby
require "addresszen"

client = AddressZen::Client.new(api_key: "ak_test")

response = client.phone_number_validation(query: "+1 202 555 0123")

result = response.result

if result.is_a?(AddressZen::Types::PhoneNumber)
puts "#{result.international_format} (#{result.country})"
else
puts "Invalid phone number"
end