Neumorphism Website + Github API (Part 4)
Tags: webdev, tutorial, api, discuss
Published: 2023-07-19 13:02:54

Hey dear developers,

In this post, I would like to introduce a simple way to check the status of an API in JavaScript. This can be especially useful when interacting with an external API (like us) and want to make sure it's reachable.

---

Do you like my articles? Then I would be happy about a like and a comment!

---

{% codepen https://codepen.io/SchBenedikt/pen/qBQYpJj %}
{% embed https://github.com/SchBenedikt/schbenedikt.github.io %}
## The challenge

Let's imagine we're developing a web application that interacts with the GitHub API to get information about users and their projects. Before we start using the application, we want to make sure that the GitHub API is working properly. Otherwise, our application could fail or behave unpredictably when the API is unreachable.

## The solution

Here's a simple way to check the status of an API in JavaScript:

1. Create a function that makes a simple API call. For example, we can use the `fetch` method to send a request to the API.

    ```javascript
    async function checkAPIStatus() {
      try {
        const response = await fetch("https://api.github.com/");
        if (response.status === 200) {
          // API is reachable
          console.log("🟢 GitHub API is working");
        } else {
          // API is not reachable
          console.error("🔴 GitHub API is not available");
        }
      } catch (error) {
        // API is not reachable
        console.error("🔴 GitHub API is not available");
      }
    }
```
## Checking API status in JavaScript: A simple method

Hey dear developers,

In this post, I would like to introduce a simple way to check the status of an API in JavaScript. This can be especially useful when interacting with an external API and want to make sure it's reachable before taking any further action.

### The challenge

Let's imagine we're developing a web application that interacts with the GitHub API to get information about users and their projects. Before we start using the application, we want to make sure that the GitHub API is working properly. Otherwise, our application could fail or behave unpredictably when the API is unreachable.

### The solution

Here's a simple way to check the status of an API in JavaScript:

1. Create a function that makes a simple API call. For example, we can use the `fetch` method to send a request to the API.

```javascript
async function checkAPIStatus() {
   try {
     const response = await fetch("https://api.github.com/");
     if (response.status === 200) {
       // API is reachable
       console.log("🟢 GitHub API is working");
     } else {
       // API is not reachable
       console.error("🔴 GitHub API is not available");
     }
   } catch (error) {
     // API is not reachable
     console.error("🔴 GitHub API is not available");
   }
}
```
Call the checkAPIStatus() function and check the status of the API.
```javascript
checkAPIStatus();
```

Based on the status code, you can then decide how to handle API availability. You could show a success message if the API is reachable, or throw an error message if it's unreachable.
Conclusion
Checking API status is a simple but important thing to do to develop a robust web application. By checking the status of the API, we can ensure that our application works smoothly and provides users with a positive experience.

You can incorporate this method into your web projects to ensure external APIs are working properly before taking any further steps. This helps to catch potential errors early and improve user experience.

I hope this method is helpful to you and helps you build reliable web applications. Happy coding! 🚀

Thanks,
schBenedikt