Connecting Worlds: Everything About Working with APIs
Hello HaWkers! Jeff Bruchado here.
This is another article in our series that will cover our journey to Learn Programming from Scratch.
Well, without further ado, let's get started!
Today, we're going to learn to work with APIs in Programming:
Working with APIs is like being a master chef in the kitchen, mixing ingredients from different places to create something amazing.
First, let's understand what an API is
API stands for Application Programming Interface.
Think of it as a waiter who takes your orders to the kitchen (a system or server) and brings back what you ordered (data or services).
Why is it so important to know how to work with APIs?
Access to External Data: APIs are like magic portals that give you access to data from other services. Want weather information, recent tweets, financial data? API solves these problems!
Integration: They allow you to integrate your application with external functionalities. For example, embedding Google maps or processing PayPal payments on your site.
Automation: APIs can automate tasks. Like automatically updating stock quotes in your financial app.
Customization: You can take data from an API and shape it however you want, creating personalized experiences for users.
But how do you work with them?
Understand the API Documentation: Each API comes with documentation. It's like a recipe book that explains how to request the data you want.
Use API Keys: Many APIs require a key, which is like a VIP pass that gives you access. This key is known as a Token.
Make HTTP Requests: You request data by making HTTP requests, which can be GET, POST, PUT, DELETE, among others.
Handle the Data: When the API returns data, usually in JSON format, you use it however you want in your app.
A practical example:
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.log(error));In this code, you make a GET request to an API, wait for the response, convert to JSON, and then do something with that data.
Remember, each API is unique. Some are easy, others are like deciphering an ancient riddle. But all of them give you superpowers to bring data and functionality from other worlds to your app.
Practice
To make a simple GET request, just call the Fetch function with the API URL as an argument:
fetch('https://example.com/api/data')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));When the response arrives, the .json() method is used to transform it into an object. You can also use other methods, like .text() to get the response as a string or .blob() to get the response as a Blob (a type of object that represents binary data).
If you want to send data to the server, just set the POST method in the options and include the data in the request body:
fetch('https://example.com/api/data', {
method: 'POST',
body: JSON.stringify({
name: 'Jeff',
age: 25,
}),
headers: {
'Content-Type': 'application/json',
},
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));UPDATE example:
fetch('https://example.com/api/data', {
method: 'PUT',
body: JSON.stringify({
name: 'Jeff',
age: 25,
}),
headers: {
'Content-Type': 'application/json',
},
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));DELETE example:
fetch('https://example.com/api/data', {
method: 'DELETE',
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));File upload example:
const formData = new FormData();
formData.append('file', myFile);
fetch('https://example.com/api/upload', {
method: 'POST',
body: formData,
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));In this example, we're creating a FormData object and adding a file to it using the append() method.
Then, we make a POST request to the URL "https://example.com/api/upload" and include the FormData object in the request body.
When the response arrives, we use the .json() method to transform it into an object and then display the data in the console.
If there's any error during the request, it will be handled by the catch block.
Conclusion
Working with APIs is important because a large part of modern applications we work with today depend on external data to function, often we have the Frontend decoupled from the Backend, in the case of the Backend(s), we can have multiple different services, this way, communication with the user layer (Frontend) is done through HTTP requests.
💡 For example, a news site can use an API to access the latest news from an external source, while a messaging app can use an API to send and receive messages from a server.
Knowing how to work with APIs is essential because it allows you to access this data programmatically.
I hope I've somehow helped clarify why Fetch is important and why it's important to know how to work with APIs in JavaScript.
Well HaWker, we've reached the end of another article.
As always, if you have any questions or want to explore any point further, just reply with your question to this email.
And remember, I'm here to help you improve 1% with each new article!
If you made it this far, as usual, please reply to this email with "I managed to learn how to make API requests in JavaScript".
This way I'll know that this article series is being useful for you, and that you're following along and taking advantage.
As you already know! Only a true HaWker makes it to the end.
Congratulations!
I'm happy to have you here.
See you soon.
Let's go! 🦅
- Jeff Bruchado
When you feel the time is right, there are 2 ways I can help you:
01) Learning JavaScript from Scratch: Become a valuable programmer and learn to program using JavaScript, with organized and condensed content focused on what really matters for you to learn. The content has all the articles I've been sharing weekly with you, and also has practical exercises, with answers for you to learn the theory and immediately start getting hands-on.
02) Consulting: Join my mentorship and have classes directly with me. If you're interested in this, reply to this email with "I'm interested in the mentorship."
Congratulations on making it this far, HaWker! 🎉
Now that you've learned about how to work with APIs in JavaScript, how about testing your knowledge? Take the quiz below and see how much you absorbed from our article.
It's a great way to reinforce learning and prepare for the next steps in your programming journey.
Good luck and have fun!

