IDK- API basics and React
Tags: react, api, webdev, javascript
Published: 2023-04-04 14:21:41


##APIs & React 


What is an API? Yes, we know what API stands for...Application Programming Interface, but what is it really?

I am quite sure there are plenty of technical definitions that can make learning what an API is and does, very convoluted and hard to comprehend. So, I'll tell you what my understanding of what an API is. 

Let's think about it like you're going to pick out a vehicle at a car lot. The car lot has a catalog of vehicles. The catalog 
is organized into various categories, like Sedans, SUVs, Trucks, and Vans. The catalog may also have additional information
about the vehicles, including the price and features. After glancing through the catalog, you simply tell the car salesman (the sever) what you want. I'd like a 2024 Range Rover SV please...👀.  Next, the salesman takes order and brings your vehicle of choice all ready and prepared to go. You'll recieved what you wanted and you 
don't even need to know how the car lot made it happen. The only thing you need to understand is how to make a request
the for the vehicle and now you get to drive off the lot into the sunset in your brand new Range *Beep Beep*.

I know this analogy doesn't cover all the bases of APIs, it is just meant to give you a general idea of how an API works.

- You want data from an API
- You make a request to that API
- API returns the data requested

###What information is needed to know how to interact with the API?

Most times the entity with the data will provide the documentation necessary to make the request.Like the car lot catalog, they will tell you what data is available and instructions on how to retrieve that data. Then, just like the salesman on the lot, you request the data from the API, it 
goes to get the data and brings it back for you to use. 

This is the Request and Response cycle.

_Request (I want some data) ===> Reponse (Here's the data)_

The Request and Response cycle involves your Web Browser (the client) communicating with a Server.The typical request is a GET request, you are making a request to get data from a server.


###What will the data look like ?

The most common format the data sent back will in JavaScript Object Notation (JSON).
JSON is formats the data like JavaScript objects, using key-value pairs

EXAMPLE :

```
{
  "type": "SUV",
  "name": "Range Rover SV" ,
  "price": "$$$$"
  "features": ["cool","stuff", "here"]
}
```


* one key (or property) would be "type" and the value would be "SUV"

###How do you make the request for the the data?

There are a few different tools you can use to fetch data from an API, including the built-in JavaScript
Fetch API, Axios library, SWR, or even React Query.

I will use a simple example to fetch data from the DOG API (https://dog.ceo)

 -This example uses React, the useEffect/useState hook, and the Fetch API

 

```

import React, { useEffect, useState } from "react";

const FetchExample = () => {
  const [data, setData] = useState(null);
 
 useEffect(() => {
    fetch("https://dog.ceo/api/breeds/image/random")
      .then((resp) => resp.json())
      .then((apiData) => {
        setData(apiData.message);
      });
  }, []); 
```


Finally, you can use the data for you want. In this case, I'll just display the image on the screen.

 

```

return (
    <div>
      <h1> My new dog </h1>
      <img 
        width={500} 
        src={data}     
        alt=‘image of dog’
      />
    </div>
  );
}

export default FetchExample;

``` 
    

That's it.

Great! Now, you're an expert!
Well, maybe not an expert but, you're definitely on your way to becoming one.

I've added a few useful resources with information on free APIs you can use for your
future projects or just to practice.

- https://github.com/public-apis/public-apis
- https://apilist.fun
- https://rapidapi.com (Free & Paid)

 Happy Coding :)