JavaScript GET Requests - Unlocking Using APIs
Tags: javascript, beginners, codenewbie
Published: 2022-08-23 14:52:52

If you're a beginner to software engineering, like myself, it's very exciting to unlock all the potential that comes with accessing APIs. APIs (short for Application Programming Interface) allow two applications to talk to each other, letting us input data from an API into our websites. There's APIs for all kinds of fun information, including Pokémon, Star Wars, even the Real Housewives of New Jersey. (All these topics are important to our work of course.) But how do we access them?

Great question, me! To access APIs, we'll use a method in JavaScript called fetch().

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r4bgevw17jegkuevr52q.jpg)
_Mean Girls was wrong. Fetch is a thing._

Fetch() <u>fetches</u> a resource from the API we ask it to and returns a <u>promise</u> which is fulfilled once the <u>response</u> is available. I <u>promise</u> you don't have to worry too much about what a promise is to do a GET fetch() request, so long as the method is called correctly. Just know that you can learn more about promises if you want to look at the other three ways that you can use fetch(). The simplest way to use fetch() is GET, which <u>gets</u> the data from an API and we will examine now.

But wait! To fetch() you need to use terminal to use a command to allow our computer to communicate with a server using the command: 
`json-server --watch db.json`

JSON is a common data-interchange format used for APIs. For now, all we need to know is how to take that JSON data and return it in JavaScript, so that we can use it to populate API data in our webpages.

Now that our computer can communicate with an API, we can use the  a fetch() request with GET to get the information on the API. To fetch() use the following syntax:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mh4lbd7hsvpktnd0w6oe.png)

You might notice that the fetch() code didn't actually include the word, "GET" that's because a fetch() request that doesn't ask for a different method will automatically do a GET request.

Our particular use of the fetch() method would <u>fetch</u> the data from the API at the URL we passed through the method. In this case, it would fetch all of the memes from a meme API. (Something all employers, no doubt use.) Our code then would convert the response (all the API data) from JSON format to JavaScript, using the JSON() method.

The a sample of the JSON we are selecting would look like the following:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1dq4qmrfcmos04osxpfp.png)

As you can see it has keys and values to provide us with details such as the name and url to images for memes. Now that we have used a GET request via fetch() we can add that data to our website to spice it up. We can populate our website with the values (names and images) from the API easily by selecting elements in our HTML to add the API details to by using methods such as document.querySelector() and changing the .innerText and .src of the elements of our HTML using Javascript. Like so:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mtnydjbzshwxd0g5ikmo.png)

With some simple coding and a GET request we can very quickly add a lot of interesting data to our website, bringing it from this husk:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cuqkbvtxnmjs41mqhoaw.png)

To something full of information (and in our case memes!) that we can display however we would like:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8kaetdaryb42vppdaxdv.png)

So look for free APIs today and use GET with fetch() to start building out websites full of information quickly! Now if you'll excuse me, I'm off to build that Pokémon, Star Wars, and Real Housewives of New Jersey website...