What Are APIs and How Can We Use them with the Fetch Method?

Eun
3 min readJun 13, 2023

An API (Application Programming Interface) is a set of commands, functions, protocols, and objects that programmers can use to create software or interact with an external system. There are countless types of APIs available today, spanning different industries and contexts. For example, APIs can be used to access weather data, financial information, social media platforms, and more.

Don’t worry! I’ll explain with examples :)

Imagine you want to create a crime map of London and require crime data for that purpose. However, the police cannot provide all the details of crime data due to confidentiality concerns. Instead, authorized personnel can access confidential data.

But they may be willing to provide basic information such as the location or time of the crime, which can be found in news reports. APIs act as a contract between users and data providers, specifying the extent of data that can be revealed and the methods users can employ to access and manipulate that data to create software or websites.

API is serving your menu like a waiter in a restaurant!

APIs are designed to be user-friendly, much like ordering from a restaurant menu. When you order a pizza at a restaurant, the server responds by serving you the pizza. Similarly, when a user sends a request to an API, the server responds with the requested data if the request is valid. If the request is incorrect, the server does not respond.

Now that we know what APIs are, let’s explore how we can use them in our real projects using fetch() method.

JavaScript provides the Fetch API, a built-in feature that simplifies making HTTP requests and handling responses. The Fetch API allows you to send GET, POST, PUT, DELETE, and other types of requests to APIs.

To fetch data from an API, you can start by making a GET request. Here’s an example of how to use the Fetch API to retrieve data:

fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => {
// Handle the data
console.log(data);
})
.catch(error => {
// Handle any errors
console.error('Error:', error);
});

In the code above, we use the fetch function to send a GET request to the specified API endpoint (https://api.example.com/data). We then use the then method to handle the response by extracting the JSON data using the json method. Finally, we can access and process the data in the second then block or handle any errors in the catch block.

The response from the API can vary depending on the specific API you’re working with. It could be JSON, XML, HTML, or plain text. Make sure to check the API documentation to understand the response format and structure.

Besides GET requests, you may also need to send data to an API using a POST request. Here’s an example:


const data = {
name: 'John Doe',
email: 'johndoe@example.com'
};

fetch('https://api.example.com/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => {
// Handle the response
console.log(data);
})
.catch(error => {
// Handle any errors
console.error('Error:', error);
});

In this example, we use the fetch function again, but this time we provide additional options in the second parameter. We specify the HTTP method as 'POST' and set the 'Content-Type' header to 'application/json' since we're sending JSON data. The data object is converted to a JSON string using JSON.stringify() and included in the request body.

Well done! Now we know pretty much all about APIs!

Fetching data from an API is a fundamental skill in web development. With JavaScript’s Fetch API, you can easily retrieve data from external sources and incorporate it into your applications. By following the steps from my article, you should now know how to fetch an API using JavaScript and handle the response data effectively.

Written by Eun & ChatGPT

--

--

Eun

Korean mom living in the UK 🇬🇧 Writer & Blogger & Reader