Creating a Whois Checker with Sinatra and WhoAPI

Creating a whois checker with Sinatra and WhoAPI

Ever used a Whois service before? Of course, you have! It’s so useful to be able to check who has registered a domain name or when it’s expiring. It also gives you a way to contact the owner in one click or see if your WhoisGuard is working correctly. Well in this article we are going to build a whois checker with Sinatra and Ruby. In case you are looking for code examples to make Whois API requests in Ruby open this page.

1. Introduction

There are lots of tools that let you see the information for a specific domain name. Most of them are websites where you just input a domain name and get the details back.

But what if you’d like to have that INSIDE your application? You would basically be able to do the following:

  • Do automated checking to get the status of a domain name
  • Offer domain checking to your users as part of your services
  • Run daily queries to aggregate information for domain names, IPs and more

Those are just some examples and there are many more reasons to do this.

2. WhoAPI and the Whois API

If you don’t want to do everything manually, you are going to need some kind of external API to gather the data.

Well, you’re lucky! That’s exactly what we offer.

What is WhoAPI?

WhoAPI offers a range of services allowing the querying of domain name data. Track everything and never miss a renewal anymore (domain name, certificates, blacklists, etc).

In this tutorial, we’re going to talk more specifically about our Whois API and see how simple it is to implement. I believe the best way to learn something, especially in the programming world, is to actually build something with the tool. That’s exactly what we are going to do.

3. Building a simple application with the Whois API

Let’s build something simple that will let us explore the Whois API JSON. We are going to create a simple Sinatra application with one form that lets you submit a domain name. The application will then query our Whois API to get all the domain information and display them in a ‘formatted’ way (in a table basically).

The following tools will be used:

Note: I’m expecting you to already have Ruby installed on your machine. If you don’t have it, check out RVM for a painless way to do it.

Let’s get started!

3.1 Signing Up

Before building anything, we need to sign up with the service and get an API key.

Head over to Whoapi.com. You should end up on the following page.

Click on sign up in the top right corner and you will end up on the signing form. Fill in all the information (email, password, contact name, company, country, and VAT ID if applicable).

whoapi signup form

After the signup process, you will receive a confirmation email. Once your account is confirmed, you can access our beautiful backend.

WhoAPI Backend View

Click on account in the sidebar (last icon) to access your API Key. Remember where to find, you will need it soon.

WhoAPI API KeyNow we’re ready to use the API! Pretty simple, right?

3.2 Creating the project

Now it’s time to get our hands dirty. We are going to use Sinatra so let’s install it.

gem install sinatra

Create a new directory named whois_app and add the file app.rb in it.

mkdir whois_app && cd whois_app && touch app.rb

Open the whois_app/app.rb file and add the following to it.

# whois_app/app.rb
require 'sinatra'

get '/' do
  'Welcome to my Whois Application!'
end

You can now run ruby app.rb and access http://localhost:4567 to see this beautiful screen:

welcome

Let’s add a view that will contain our form. Add the folder views to your projects and create two files in it: index.erb and whois.erb.

mkdir views && touch views/index.erb views/whois.erb

Here is the content for views/index.erb:


Nothing fancy, just a simple HTML form that contains an input to insert the domain name for which we want to get information.

Notice that we are sending this form to /whois so let’s define this route in our app.rb. We also need to tell Sinatra to render the index template in our first action (/). To do this, we need to use the erb method.

# app.rb
get '/' do
  erb :index
end

post '/whois' do
  domain = params['domain_name']

  # Everything in locals will be available in our view
  erb :whois, locals: {
    domain: domain,
    info: {}
  }
end

And here is the content for the eRB file named whois.erb that we created earlier. The data we will get back from the API later is just a hash so we’ll just loop through it and display the keys and values in a table (to avoid having you copy/paste some CSS, a table will do).

Domain Data for “”


Alright, let’s check it out! Restart or start (ruby app.rb) your server and head over to http://localhost:4567.

You will see our beautiful form on the home page:

whoisform

Fill in a domain name, press submit and tada! Nothing! 🙂

empty

I guess it’s time to connect to the Whois API and get some real data in there.

3.3 Calling the API

Even if this is a small sample application, we are going to create a wrapper for the Whois API to avoid putting logic in our actions. Create a new file named whois_wrapper.rb at the root of your project.

touch whois_wrapper.rb

This file will contain a Ruby class that will connect to the API usingNet::HTTP. Here is the code for this file, don’t forget to replaceYOUR_API_KEY with your actual API key.

In this application, I’m embedding the API key in the file. Never do that in a versioned production application and prefer the use of environment variables to store sensitive data.

require 'net/http'
require 'json'

class WhoisWrapper

  API_KEY = 'YOUR_API_KEY'

  def initialize(domain_name)
    @domain_name = domain_name
  end

  def query
    JSON.parse(data)
  end

  private

  def data
    Net::HTTP.get('api.whoapi.com', "/?domain=#{@domain_name&r=whois&apikey=#{API_KEY}")
  end

end

This class is pretty simple. The interesting part is the data method that will build the URL and make the call to api.whoapi.com. Also notice r=who is to specify that we want to check whois information. You can find the complete list of options here.

All that’s left is updating the app.rb with our new class. Before using it, we need to require it at the top of the file.

# whois_app/app.rb
require 'sinatra'
require './whois_wrapper'

get '/' do
  erb :index
end

post '/whois' do
  domain = params['domain_name']

  erb :whois, locals: {
    domain: domain,
    info: WhoisWrapper.new(domain).query
  }
end

Restart your server and reload the page. You will now be able to get all the Whois information for any domain!

google

The data whois API sent back as JSON so you’re free to only extract what you need.

4. Wrap Up

That’s finally the end. We had a good time building this simple yet powerful little application and I hope you learned a lot about WhoAPI.

If you’d like to dive into the documentation head over here. To run some test queries, feel free to use our console. You will be able to see the different types of requests that you can use (you can use those as value for the r parameter.)

It’s time for you to start building!

Looking for a great Ruby library for a whois API, domain availability check API, screenshot API, email blacklist API, SSL API, or any other API that WhoAPI provides, you’ve come to the right place.

If you are a Ruby developer and just starting out, either with Ruby or WhoAPI, you are going to love this.

All services are provided by WhoAPI in this Ruby library.

We published a WhoAPI GEM – find it here.
Or if you want to fork it on Github – fork it here.

Please let us know if you would like us to build a library in a different programing language, or if you have ideas/recommendations on the Ruby library we just shared with you.

P.S.
In case you missed our tutorial on how to build a whois checker in Ruby with WhoAPI, you can find it here.

Thibault

Thibault Denizet is a software engineer with more than 3 years of experience in web development. He also enjoys writing technical articles and tutorials to share his knowledge and spread the love for the Ruby programming language.

Post a Comment

Your email address will not be published. Required fields are marked *