In this tutorial, I am going to demonstrate how to use the Geekflare API in NodeJS to check the DNS records of any domain.

We are going to build a simple script that, when executed, prints out the IP address of Google’s Search server.

This script will use the Geekflare DNS Lookup API.

To build it, we are going to use three approaches, the first uses the built-in https module in NodeJS. The second will use the node-fetch module. Then the last will use the axios client library.

What is the Geekflare API?

Geekflare API offers suits of REST APIs for website performance, DNS, and security metrics testing. You can do stuff like take a screenshot, generate PDFs, do web scraping, port scanning, and a lot more.

Prerequisites

To follow this tutorial, you will need an understanding of JavaScript, including promises and ES6 syntax. As for the software, you should have NodeJS and a text editor such as Visual Studio Code installed.

You will need a Geekflare account to get an API key for authentication when making requests. To get one, head to the API landing page and create a free account.

After you create the account, you should be redirected to the dashboard, where you will find your API key.

Geekflare-Dashboard-2

Building the project

To begin, create a project folder and open it with a terminal of your choice, then run the command below.

npm init -y

The above command will initialize the project directory as a NodeJS project.

Next, run the command below, which will install all dependencies for our project

npm install dotenv axios node-fetch

After the dependencies have been successfully installed, create three scripts in the project root folder namely vanilla.js, with-axios.js, with-fetch.js and a .env file to store our environmental variables.

In the end, the project root should look like this:

Screenshot-from-2022-10-06-10-27-03

Next, open the .env file and add your Geekflare API key with the following line of code:

API_KEY=<api key>

Replace <API key> with your actual API key.

Vanilla.js

NodeJS has a built-in http and https module that we can use to make client requests. We are going to use this approach first.

Open the vanilla.js file and add the following lines of code at the top to import the project’s dependencies.

import { request } from "https";
import { config } from "dotenv";

Next, we are going to call the config() function to load environmental variables. Then we are going to store the API key and hostname in variables.

config();

const apiKey = process.env.API_KEY;
const host = 'google.com';

When we call the request function to start an HTTP request in NodeJS, we will need to provide options for the host and endpoint we want to connect to, the HTTP method we are going to be using, and the headers for the request. So next, we are going to create a variable that will store these options.

const options = {
  hostname: "api.geekflare.com",
  path: "/dnsrecord",
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "x-api-key": apiKey,
  },
};

So far, the code in the vanilla.js file looks like this:

import { request } from "https";
import { config } from "dotenv";

config();

const apiKey = process.env.API_KEY;
const host = 'google.com'

const options = {
  hostname: "api.geekflare.com",
  path: "/dnsrecord",
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "x-api-key": apiKey,
  },
};

Now we can proceed to call the request function passing in the options method:

const req = request(options, response => {

  // we are going to add response handlers here

});

As you can see, the request function takes in two arguments. The first is the options object we defined earlier. The second is a callback function that will handle the response from the server. Inside the callback function, we can add event listeners for when the server sends data, finishes sending data, or sends an error.

To add the different response handlers, add the following lines of code inside the callback function:

let data = "";

response.on("data", chunk => {
  data += chunk;
});

response.on("end", () => {
  console.log(JSON.parse(data).data.A);
});

response.on("error", error => {
  console.log(error);
});

The data variable is simply a string where we are going to store the server’s JSON response as it is streamed back to us.

To actually store the data, we are going to listen to the response object’s on data event. Whenever this event is fired, we are going to append the chunk of data sent by the server to the data variable.

Then to finally use the data, we are going to listen to the on end event on the response object. This will be called when all the data has been sent from the server, and it has ended its response.

Lastly, we are going to listen for errors and log them to the console if they arise.

Therefore the call to the request function should look like this

const req = request(options, response => {

  let data = "";

  response.on("data", chunk => {
    data += chunk;
  });

  response.on("end", () => {
    console.log(JSON.parse(data).data.A);
  });

  response.on("error", error => {
    console.log(error);
  });

});

Lastly, we need to write some data to the request body and end the request.

req.write(JSON.stringify({ url: host, types: ["A"] }));
req.end();

In the end, the file should look like this:

import { request } from "https";
import { config } from "dotenv";

config();

const apiKey = process.env.API_KEY;
const host = 'google.com'

const options = {
  hostname: "api.geekflare.com",
  path: "/dnsrecord",
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "x-api-key": apiKey,
  },
};

const req = request(options, response => {

  let data = "";

  response.on("data", chunk => {
    data += chunk;
  });

  response.on("end", () => {
    console.log(JSON.parse(data).data.A);
  });

  response.on("error", error => {
    console.log(error);
  });

});

req.write(JSON.stringify({ url: host, types: ["A"] }));
req.end();

Now, if you go back to the terminal and run the script using the node vanilla.js command, you should get the following output.

[
  { address: '172.253.122.101', ttl: 247 },
  { address: '172.253.122.113', ttl: 247 },
  { address: '172.253.122.100', ttl: 247 },
  { address: '172.253.122.102', ttl: 247 },
  { address: '172.253.122.138', ttl: 247 },
  { address: '172.253.122.139', ttl: 247 }
]

That’s it for the first part. The obvious disadvantage of using the built-in HTTP/S modules is that it is verbose. Client libraries such as node-fetch will help you create the same program but with clearer and more concise code.

node-fetch

To create the same script but with node-fetch, open the with-fetch.js file and add the following imports to the top.

import fetch from "node-fetch";
import { config } from "dotenv";

Then call the config function to configure environmental variables and set up constants for the API_KEY and the host whose A records we are going to be requesting.

config();

const apiKey = process.env.API_KEY;
const host = 'google.com'

Next, we are going to define a function to make the API call. This function will be asynchronous.

async function request() {
  // The function body will go here
}

Inside the function’s body, we need to call the fetch function we imported earlier from the node-fetch package.

const response = await fetch("https://api.geekflare.com/dnsrecord", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "x-api-key": apiKey,
    },
    body: JSON.stringify({ url: host, types: ["A"] }),
});

Then after the call to the fetch function, we would want to parse our response and handle any errors that may arise.

if (response.ok) {
    const { data } = await response.json();

    console.log(data.A);
  } else {
    console.log(response);
  }

At this point, add a call to the function after its request.

request();

Your file should now look like this:

import fetch from "node-fetch";
import { config } from "dotenv";

config();

const apiKey = process.env.API_KEY;
const host = "google.com";

async function request() {
  const response = await fetch("https://api.geekflare.com/dnsrecord", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "x-api-key": apiKey,
    },
    body: JSON.stringify({ url: host, types: ["A"] }),
  });

  if (response.ok) {
    const { data } = await response.json();

    console.log(data.A);
  } else {
    console.log(response);
  }
}

request();

And running that script with node with-fetch.js should produce the following output:

[
  { address: '172.253.122.113', ttl: 134 },
  { address: '172.253.122.138', ttl: 134 },
  { address: '172.253.122.100', ttl: 134 },
  { address: '172.253.122.139', ttl: 134 },
  { address: '172.253.122.102', ttl: 134 },
  { address: '172.253.122.101', ttl: 134 }
]

Axios

Lastly, we are going to use Axios to access the Geekflare API. To begin, let’s import the dotenv and axios packages.

import axios from "axios";
import { config } from "dotenv";

Next, let’s call the config function to set up environmental variables. Additionally, let’s store the host’s name and the API Key in separate constants.

const host = "google.com";
const key = process.env.API_KEY;

Now, let’s store the URL of the API endpoint in another constant

const url = "https://api.geekflare.com/dnsrecord";

Next, let’s store the data which will be sent as part of the request body in another constant

const data = { url: host, types: ["A"] };

Then the last thing to do before sending out the request will be to also store the meta options, such as headers, in another constant.

const options = {
  headers: {
    "Content-Type": "application/json",
    "x-api-key": key,
  },
};

Lastly, let’s make the call to the post function we imported earlier, passing in the url, data and options variables we defined earlier as arguments. Because this will return a promise, you can then use then to handle the response when it is eventually returned.

axios.post(url, data, options).then(({ data }) => {
  console.log(data.data.A);
});

At the end of all this, the code in the with-axios file should look like this:

import axios from "axios";
import { config } from "dotenv";

config();
const host = "google.com";
const key = process.env.API_KEY;

const url = "https://api.geekflare.com/dnsrecord";
const data = { url: host, types: ["A"] };
const options = {
  headers: {
    "Content-Type": "application/json",
    "x-api-key": key,
  },
};

axios.post(url, data, options).then(({ data }) => {
  console.log(data.data.A);
});

And when you run the script using node with-axios.js, it should display the following output:

[
  { address: '142.251.163.138', ttl: 60 },
  { address: '142.251.163.113', ttl: 60 },
  { address: '142.251.163.100', ttl: 60 },
  { address: '142.251.163.101', ttl: 60 },
  { address: '142.251.163.102', ttl: 60 },
  { address: '142.251.163.139', ttl: 60 }
]

Final Words

In this post, we created the script with three different approaches. The objective of this was to highlight how easy using the Geekflare API is, and how we might go about using it in Javascript, specifically NodeJS.

Explore Geekflare API documentation for more.