How to Get Notified When Your Site Goes Down using Geekflare API?


Customers take a look at to get right of entry to your website online, you understand what took place? It doesn’t display up. There are some mistakes that you just didn’t determine earlier than. Customers get annoyed and depart your website online.  So, you misplaced some unswerving customers with it.

How to remedy this downside? How do you get to know concerning the website online down earlier than your customers to find it?

There are two imaginable tactics:

In the event you don’t thoughts spending a couple of $, you’ll opt for a tracking answer like StatusCake or others discussed right here. Alternatively, in case you are a developer or now not able to spend per 30 days, you’ll make the most of Geekflare API – Is Site Up?

Is Site Up? API tests whether or not a website online is up or down from other places.

Let me display you Python code that notifies you right away when your website online is going down by way of Gmail.

Let’s get started with exploring the Is Site Up? API.

Is Site Up? API

Prior to checking the API we’d like to set up a bundle referred to as requests to paintings with APIs in Python. However, it’s now not important to use the one Python. You’ll use your most well-liked language. However, be sure you arrange the desired issues to make an API request.

So, for individuals who are using Python set up the requests bundle using the command pip set up requests

Whole the setup for different languages (if you select rather than Python) and proceed to the following steps.

Now, pass to the Geekflare API web page.

Geekflare API Page
Geekflare API Web page

You’ll to find various kinds of APIs together with Is Site Up? API which we’re thinking about for this text. To make use of the Geekflare APIs we’d like an API Key that we will be able to get thru RapidAPI.

Click on at the GET API KEY button to pass to the RapidAPI.

GET API KEY Button
GET API KEY Button

RapidAPI will open in a brand new tab as follows.

RapidAPI
RapidAPI

We’d like to create an account to get the API key. Create an account in RapidAPI when you don’t have one. Check in to your account after growing an account.

RapidAPI Logged In
RapidAPI Logged In

You’re going to to find the Is Site Up? API on the best of all Geekflare APIs which we’re on the lookout for. If it’s now not energetic to find it using the quest to be had and click on on it.

You’re going to get using API at the proper aspect. Make a choice the Python with Requests from the Code Snippets segment at the proper aspect.

Programming Languages
Programming Languages

Make a selection your most well-liked language with the respective bundle when you don’t seem to be using Python.

You’re going to get the code to name the Is Site Up? API. Let’s regulate it a bit of which is helping us to simply upload extra code later. Have take a look at the changed code in Python.

import requests

API_URL = "https://geekflare.p.rapidapi.com/up"

def make_api_request():
    headers = 
        'content-type': "utility/json",
        'x-rapidapi-host': "geekflare.p.rapidapi.com",
        'x-rapidapi-key': "YOUR_API_KEY"
    

    payload = r'"url": "https://www.geekflare.com"'

    reaction = requests.request("POST", API_URL, information=payload, headers=headers)

    go back reaction.json()   


if __name__ == '__main__':
    information = make_api_request()
    print(information)

Change the API_KEY with your individual API key from the RapidAPI within the above code. It is going to be other for each consumer. You’re going to to find it within the RapidAPI underneath the Headers Parameters segment as follows.

API Key
API Key

You’ll to find the similar API key within the pattern code as proven under.

API Key
API Key

Reproduction the important thing and upload it to the above code

Multi-Location

The above code tests whether or not the website online is up or now not from a unmarried location (New York, USA). However, we will be able to check it from other places with places within the request frame.

Different to be had places are England (London) and Singapore. We will be able to go the places information together with the website online URL as follows.


    "places": [
        "uk",
        "us",
        "sg"
    ],
    "url": "geekflare.com"

You might go the places that you just desire from the checklist.

We’ve finished the code to make an API request that fetches the knowledge whether or not a website online is up or now not. Now, it’s time to write some extra code that sends a mail when the website online is down. Let’s pass.

Receiving e-mail when the website online is down

You’ll to find the detailed educational on How to Ship Emails thru Gmail in Python Or use the next code which makes use of a bundle referred to as yagmail specifically designed to ship mails from Gmail.

Prior to sending a mail thru your Gmail account, we have now to flip at the Permit much less protected apps choices. You’ll flip it on right here.

Let’s see the code.

def send_mail():
    gmail = yagmail.SMTP("gmail", "password")

    receiver = "[email protected]"
    matter = "Checking out Topic"
    frame = "This can be a checking out mail"

    gmail.ship(
        to=receiver,
        matter=matter,
        contents=frame,
    )

You’ll to find the whole educational of yagmail right here.

Now, we have now code for API requests and sending mail. Our subsequent step is to invoke the send_mail each time we obtain dangerous standing from the API request.

So, how do we all know that our website online is down or up? When we request the Is Site Up? API, it’s going to reply with some information as follows.

'timestamp': 1629556759685, 
'apiStatus': 'luck', 
'apiCode': 201, 
'message': 'Site is up.', 
'meta': 'url': 'https://www.geekflare.com', 
'followRedirect': False, 
'check': 'identity': 'eu0frmah05mids55elkjgevkzd8ur3vk', 
'information': ['country': 'United States', 'city': 'New York', 
'description': 'Site is up.', 
'statusCode': 301, 'reasonPhrase': 'Moved Permanently']

You’re going to discover a key referred to as message in it. The worth of the important thing message tells us whether or not the website online is up or down. So, can also be two sorts of messages as follows.

  • Site is up.
  • Site is down.

I believe you were given it now. So, we will be able to ship the mail once we obtain the message “Site is down.”. The general code will glance as follows.

import requests
import yagmail


API_URL = "https://geekflare.p.rapidapi.com/up"

def make_api_request():
    headers = 
        'content-type': "utility/json",
        'x-rapidapi-host': "geekflare.p.rapidapi.com",
        'x-rapidapi-key': "API_KEY"
    

    payload = r'"url": "https://www.abcd.com"'

    reaction = requests.request("POST", API_URL, information=payload, headers=headers)

    go back reaction.json()

def send_mail(content material):
    gmail = yagmail.SMTP("gmail", "password")

    receiver = "[email protected]"
    matter = "Your Site is Down"

    gmail.ship(
        to=receiver,
        matter=matter,
        contents=content material,
    )



if __name__ == '__main__':
    information = make_api_request()
    
    message = information['message']

    ## seding the mail
    if message == 'Site is down.':
        ## extracting the mistakes from other places
        locations_data = information['data']

        mail_content = "Your website online is down due to sudden error. See the helpful information to unravel mistakes under.nn"
        for location in locations_data:
            mail_content += f"location['city'], location['country'] - location['error']n"
        mail_content += "nCheck the mistake and unravel them once imaginable."

        send_mail(mail_content)

You might replace the content material of the mail as you like. We’ve finished sending mail each time our website online is down. See the pattern mail that has been won in the course of the above code.

Site Down Mail
Site Down Mail

However, there’s nonetheless an issue.

We’ve to execute our code to take a look at whether or not our website online is up or down. How regularly can we run it? Yeah, it is determined by your choice. Let’s say we have now to take a look at it each one hour.

We will be able to open a terminal or command line and execute our code each hour. However, it’s a repetitive procedure and uninteresting one. And from time to time we will be able to put out of your mind to take a look at it. So, we’d like to execute the code routinely each hour.

Right here we will be able to employ the cron to routinely execute our code each hour. Let’s see how to arrange it.

Cron Setup

Let’s see the stairs to arrange the cron on a UNIX-based running device.

  • Open terminal.
  • Run the command crontab -e which opens the crontab record within the terminal.
  • Press the important thing i to input into the INSERT mode.
  • Now, upload the cron development, Python listing and our code record listing. You notice the instance under.

0 * * * * /usr/bin/python3 /house/pattern.py

Cron
Cron

We’ve set the development to execute the code each hour. However, you could want to set it to each minute in line with the requirement. So, you’ll use the Crontab Guru or different cron equipment to generate the cron development for the time table.

That’s it. We’ve finished the set-up to get notified when the website online is down.

Conclusion

Use the cron to time table the script to run periodically to your cloud server that runs 24/7 to get notified thru e-mail when the website online is down. Automation saves a large number of time and works for us. So, employ it as we did on this article.

Satisfied Tracking 🙂

Related Articles

Leave a Reply

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

Back to top button