How to make a simple API call with JavaScript

In this tutorial, I will show you how to make an API call with JavaScript.

Image by https://morioh.com/p/65bcbc15d7c6

What is an API ?

An API (Application Programming Interface) is a layer between one or more softwares. It allows communication between the softwares through standard protocols across applications.

What is JavaScript fetch API ?

The fetch API is an API provided by JavaScript to access and manipulate the HTTP protocol. Using the fetch method provided by JavaScript, you can use, for example, the GET method, available in the HTTP protocol.

How to make an API call using JavaScript fetch API ?

Let’s write a code to make a call to the GitHub API, using the fetch method of JavaScript.

Now we will write the following code inside the js file.

API Call

You should receive something like this:

As you can see, the GetUserGitHubAPI function returns a JSON data structure from the GitHub API.

Now you can access any property inside of our JSON data, for example, if I want to access the property avatar_url, all I heve to do is follow the code below:

Get avatar_url

Thanks for reading. This article has been duly corrected and revised.

Please follow me on my social media, for more contents like this.

Linkedin
Instagram

Eu sou um desenvolvedor Front-End apaixonado pela Stack JavaScript: ReactJs, React Native, NodeJs, Typescript