APIs

Abhijeet Kamble
3 min readOct 31, 2019

Luckily through the internet of things we are having an abundance of this data and this data can be accessed through the use of APIs. Let’s say you want to add some finance data to your website. There’s an API for that. You want to add funny GIFs, there’s an API for that. Or if you want to add data about the best reviewed pubs in your neighborhood, there’s an API for that. APIs are a fundamental piece of how most web applications work today. But what exactly is an API and how do you use one? In this blog, I’ll explain the fundamentals of APIs and how they work. I’ll break down APIs in simple terms and I’ll demonstrate how you can get started working with APIs to incorporate data into a website quickly and easily.

API stands for Application Programming Interface. While it may not be completely clear what that means at first, it’s actually a fairly straight forward concept. One way that may help to understand APIs is to think of them like a restaurant. Let’s say you’re really in the mood for a pastry. You could make it from scratch, but you’d have to go to the store, buy all the ingredients and bake it yourself. It can be expensive. It could take a lot of time. And you may not have the expertise to bake it properly. Or you could go to a restaurant. At a good restaurant the food is excellent and all the work has already been done for you. You order from a menu that lists all the available dishes and organizes them into different categories. Like appetizers, salads, entrees, and desserts. The menu typically lists information about the dishes, including ingredients and price. Once you decide you simply tell the server what you’d like. I’ll get the muffin, please. — All right. — They take your order to the kitchen and the food is brought to you ready to eat. — Here you go. — You just had to know how to order it and now, you get to enjoy delicious food without having to do any of the work. While this isn’t a perfect analogy, it is a helpful way to think about how APIs work. With an API you also want to get something. Although it’s not food, it’s data. There are all kinds of companies out there that provide data through the use of their APIs. Data on sports, weather, finance, outer space, gifs, you name it. Like a menu in a restaurant, companies provide documentation that explains what data’s available and how to ask for and get that data. Then like a server at a restaurant, you request that data from the API, it goes and gets it and brings it back for you to use as you like.

Documentation on APIs, The order is important too, like in a restaurant you order soup first then the appetizers then the main course and desert. Similarly, for an API you need to Make a request to the API, then filter Responses to the API, access data in the response , use the response data in HTML(Hyper text markup language) and finally loop through the response data. The scope of this blog is just to familiarize with APIs, I will have another blog that goes through each and every step, one at a time.

Finally, one should look out for error handling when dealing with an API. If there is an error with your request the xmlhttp request object has an event handler for handling these errors. We’ll use the on error event handler to deal with any errors that might come up. So in our code, so we’ll type request.onerror and we’ll create a function to handle any errors. Inside this function we’ll just type console.log and into our console we’ll just print out a very simple error message.

Certainly you could be much more involved with the way you handle errors and you probably should, but as I mentioned we’re not really concerned with errors at this point. We just wanted to get started working with APIs. But I wanted to briefly introduce you to this because you will likely deal with it in the future and there’s a very convenient event handler to deal with this.

--

--