Print

Posted in:

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.

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. 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, checkout 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.

whoapi homepage

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 sign up 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.

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

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

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.

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.

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).

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.

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.

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.

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

google

The data is 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 request that you can use (you can use those as value for the r parameter.)

It’s time for you to start building!

Written by 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.

1 posts

3 Pings & Trackbacks

  1. Pingback:

  2. Pingback:

  3. Pingback:

Leave a Reply

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