Integrations
Validate phone numbers and emails in Ruby
Check emails, phone numbers and IPs from Ruby with Net::HTTP, no gem needed. Reject bad input, flag risky traffic and retry politely on rate limits.
Updated 2026-11-23
This recipe validates an email, a phone number and an IP address from Ruby with the standard library. It runs in a Rails app, a Sinatra route or a script.
What you need
- Ruby 3.0 or later.
net/httpandjsonare part of Ruby. - A Qelka API key in the
API_KEYenvironment variable. Ask_test_key returns simulated results and never spends credits: see Test mode. - Server-side code only: see Authentication.
Steps
1. A client
require 'json'
require 'net/http'
require 'uri'
module Qelka
BASE_URL = 'https://api.qelka.com/v1'.freeze
RETRYABLE = [429, 500, 502, 503].freeze
class ApiError < StandardError
attr_reader :status, :type, :request_id
def initialize(status, type, message, request_id)
super("#{type}: #{message}")
@status = status
@type = type
@request_id = request_id
end
end
def self.call(path, body, max_attempts: 4)
uri = URI("#{BASE_URL}#{path}")
(1..max_attempts).each do |attempt|
request = Net::HTTP::Post.new(uri)
request['Authorization'] = "Bearer #{ENV.fetch('API_KEY')}"
request['Content-Type'] = 'application/json'
request.body = JSON.generate(body)
response = Net::HTTP.start(uri.host, uri.port, use_ssl: true, open_timeout: 3, read_timeout: 10) do |http|
http.request(request)
end
status = response.code.to_i
return JSON.parse(response.body) if status.between?(200, 299)
if RETRYABLE.include?(status) && attempt < max_attempts
retry_after = response['Retry-After'].to_i
sleep(retry_after.positive? ? retry_after : [2**(attempt - 1), 16].min + rand * 0.25)
next
end
failure = JSON.parse(response.body)['error']
raise ApiError.new(status, failure['type'], failure['message'], response['X-Request-Id'])
end
end
end
A 429 waits for the Retry-After header, 500 and 502 back off with jitter, and every other error raises Qelka::ApiError at once with the documented type and the X-Request-Id. See Errors and Rate limits.
2. Check a signup
def check_signup(email:, phone:, country: nil, ip: nil)
errors = {}
checked = true
e164 = nil
review = false
guard = lambda do |&block|
block.call
rescue Qelka::ApiError => error
checked = false unless error.type == 'unprocessable_input'
warn "validation skipped: #{error.type} request_id=#{error.request_id}"
rescue Timeout::Error, SocketError, SystemCallError, OpenSSL::SSL::SSLError => error
checked = false
warn "validation skipped: #{error.class}"
end
guard.call do
result = Qelka.call('/email', { email: email })
if result['status'] == 'undeliverable'
errors[:email] = result['did_you_mean'] ? "Did you mean #{result['did_you_mean']}?" : 'This email address cannot receive mail.'
elsif result['status'] == 'risky' && result['reason'] == 'disposable'
review = true
end
end
guard.call do
body = { phone: phone }
body[:country] = country if country
result = Qelka.call('/phone', body)
if result['status'] == 'valid'
e164 = result['e164']
elsif result['status'] == 'invalid'
errors[:phone] = 'This phone number is not valid.'
end
end
if ip
guard.call do
result = Qelka.call('/ip', { ip: ip })
review ||= result['risk']['level'] == 'high' || result['flags']['tor']
end
end
{ ok: errors.empty?, errors: errors, checked: checked, e164: e164, review: review }
end
3. Read the result
| Field | Value | Action |
|---|---|---|
status of an email |
undeliverable |
Reject, show did_you_mean |
status of an email |
risky |
Accept and flag, or reject disposables |
status of an email |
unknown |
Accept, confirm by email |
status of a phone |
valid |
Store e164 |
status of a phone |
unknown |
Send country, or ask for the + format |
risk.level of an IP |
high |
Ask for extra verification |
When checked is false, accept the signup and queue a re-check. A 422 unprocessable_input on the IP is a private or loopback address, which is what you see in local development; the code treats it as "no signal". Behind a proxy, read the real client address from the forwarded header: see Flag VPN and data center traffic.
Save each result with the record so you never check the same value twice. For lists, POST /batch accepts up to 100 items per request: see the batch reference.
Related
Frequently asked questions
Do I need a gem to call the API from Ruby?
How do I keep the API key out of my code?
What should happen when a check fails?
Put this check in your product
Get an API key in under a minute. 100 free credits every month, no card required.
- 100 free credits a month
- No card
- Test keys spend nothing
- Cancel any time