How to solve this problem? How do you get to know about the site down before your users find it? There are two possible ways: If you don’t mind spending a few $, you can go for a monitoring solution like StatusCake or others mentioned here. However, if you are a developer or not ready to spend monthly, you can take advantage of Geekflare API – Is Site Up? Is Site Up? API checks whether a site is up or down from different locations. Let me show you Python code that notifies you immediately when your site goes down via Gmail. Let’s start with exploring the Is Site Up? API.

Is Site Up? API

Before checking the API, we need to install a package called requests to work with APIs in Python. But, it’s not necessary to use only Python. You can use your preferred language. But make sure you set up the required things to make an API request. So, for those who are using Python install the requests package using the command pip install requests Complete the setup for other languages (if you choose other than Python) and continue to the next steps. Now, go to the Geekflare API page. You can find different types of APIs, including Is Site Up? API, which we are interested in for this article. To use the Geekflare APIs, we need an API Key that we can get by signing up for the Geekflare API. Click on the Sign Up Free button on the header and create an account in Geeflare API. Once you create your account, you will be redirected to the dashboard of Geekflare API. In the dashboard, you will find the API key and other details like your email, active plan, etc.., You can copy the API key. Now, let’s check the documentation of Is Site Up API. Click on your profile icon and documentation. It will redirect you to the Geeflare API documentation. You will find the Site status (Is Site Up?) API documentation here. Go to it. You will get the usage of API on the right side. Select Python from the Code Snippets section on the right side. Choose your preferred language if you are not using Python. You will get the code to call the Is Site Up? API. Let’s modify it a bit which helps us to add more code later easily. Have a look at the modified code in Python. Replace the YOUR-API-KEY with your own API key from the Geekflare API dashboard. It will be different for every user. Copy the API key from the dashboard and add it to the above code

Multi-Location

The above code checks whether the site is up or not from a single location (New York, USA). But, we can test it from a different location with proxyCountry in the request body. Other available locations are England (London) and Singapore. We can pass the proxyCountry data along with the site URL as follows. You may pass any location that you prefer from the available locations here. We have completed the code to make an API request that fetches the data on whether a site is up or not. Now, it’s time to write some more code that sends mail when the site is down. Let’s go.

Receiving Email When the Site is Down

You can find the detailed tutorial on how to send emails through Gmail in Python Or use the following code, which uses a package called yagmail specially designed to send emails from Gmail. Before sending a mail through your Gmail account, we have to turn on the Allow less secure apps options. You can turn it on here. Let’s see the code. You can find the complete tutorial of yagmail here. Now, we have code for API requests and sending mail. Our next step is to invoke the send_mail whenever we receive bad status from the API request. So, how do we know that our site is down or up? When we request the Is Site Up? API, it will respond with some data as follows. You will find a key called message in it. The value of the key message tells us whether the site is up or down. So, can be two types of messages as follows.

Site is up. Site is down

I think you got it now. So, we will send the mail when we receive the message “Site is down.”. The final code will look as follows. You may update the content of the mail as you prefer. We have completed sending mail whenever our site is down. But there is still a problem. We have to execute our code to check whether our site is up or down. How often do we run it? Yeah, it depends on your preference. Let’s say we have to check it every hour. We can open a terminal or command line and execute our code every hour. But it’s a repetitive process and boring one. And sometimes we can forget to check it. So, we need to execute the code automatically every hour. Here we can use the cron to execute our code every hour automatically. Let’s see how to set it up.

Cron Setup

Let’s see the steps to set up the cron on a UNIX-based operating system.

Open terminal. Run the command crontab -e which opens the crontab file in the terminal. Press the key i to enter the INSERT mode. Now, add the cron pattern, Python directory, and our code file directory. You see the example below.

0 * * * * /usr/bin/python3 /home/sample.py We have set the pattern to execute the code every hour. But you may need to set it to every minute based on the requirement. So, you can use the Crontab Guru or other cron tools to generate the cron pattern for the schedule. That’s it. We have completed the setup to get notified when the site is down.

Conclusion

Use the cron to schedule the script to run periodically on your cloud server that runs 24/7 to get notified through email when the site is down. Automation saves a lot of time and works for us. So, make use of it as we did in this article. Happy Monitoring 🙂

How to Get Notified When Your Site Goes Down using Geekflare API  Geekflare - 90How to Get Notified When Your Site Goes Down using Geekflare API  Geekflare - 58How to Get Notified When Your Site Goes Down using Geekflare API  Geekflare - 24How to Get Notified When Your Site Goes Down using Geekflare API  Geekflare - 64How to Get Notified When Your Site Goes Down using Geekflare API  Geekflare - 86How to Get Notified When Your Site Goes Down using Geekflare API  Geekflare - 33How to Get Notified When Your Site Goes Down using Geekflare API  Geekflare - 14How to Get Notified When Your Site Goes Down using Geekflare API  Geekflare - 4How to Get Notified When Your Site Goes Down using Geekflare API  Geekflare - 63How to Get Notified When Your Site Goes Down using Geekflare API  Geekflare - 34How to Get Notified When Your Site Goes Down using Geekflare API  Geekflare - 38