A Beginner's Guide To APIs
Tags: javascript, api, beginners, webdev
Published: 2020-09-07 12:41:39

*So the other day I went to my grandma’s place to explain to her about APIs.*

If you’re a student studying Computer Science or someone who’s trying their luck getting into the software industry, one might often encounter the phrase API. API stands for **Application Programming Interface**. It is an integral part of software development. To understand APIs, let’s start with the last word in the acronym — **Interface**.



##What is an Interface?
An interface is a layer (or an abstraction) wherein most of the underlying functionality is removed from the eyes of the user, and provides a simple platform to the user to interact with the entity, such as a TV!

We use a TV remote to change the channels, control the volume or the brightness of our display. Here the TV remote acts as our interface to the TV. We, as the end user, can control all the functionalities of our idiot box using only the buttons present on the TV remote without even knowing HOW it is done.

>Ever heard of the term GUI? A GUI — Graphical User Interface, provides an **Interface** which makes it easier for the user to operate a particular program.

Similarly, for developers, most of the components are reusable, which are abstracted and accessed via an **Interface**, for **Application Programming**. Hence, the API.

##Introduction to APIs
APIs provide the required data and abstracts most of the implementation by the underlying code to the software developer, which saves a lot of time, developmental costs and in general, a lot of work, if done from scratch.

Today, it’s safe to assume that whenever we hear the word “API” it’s mostly a web-based API, but APIs constitute a much larger demographic. For example, if converting a string to uppercase, any programming language provides a method to achieve it. That’s APIs in action. We can do the same by manipulating in the low level bits, but by using APIs is much easier, faster and more convenient.

>Likewise, Frameworks provide an API to developers for faster development for their applications. These APIs perform a ton of work for us, instead for the developer to reinvent the wheel. All we need to do is provide the required data to the framework API for it to work.

##Web-based API
As we all now know that APIs do a lot of work for us by abstracting a ton of things under the hood, as technology evolved, so did APIs.

With the advent of internet and more specifically cloud computing, more and more people started to send data through the internet. Likewise, many businesses and institutions started to provide their services through the internet using — APIs.

Say if a user wanted to find today’s weather, the user obviously doesn't have an inbuilt barometer in his phone or wires connected to the local weather station from his phone. The user can just use a weather app which uses the location from the user’s phone’s GPS and sends it to a **remote web-based API** which in turn responds with the current weather data to the user’s phone.

Remote APIs are constructed by developers using several approaches. The most popular one is **REST — Representational State Transfer**. The APIs that utilise this approach are said to be **RESTful**.

>REST API is so popular that if people refer to any API, they usually mean it as a RESTful one.

To understand how one accesses data from a RESTful API, one needs to understand how computers talk with each other via internet.

##The Internet
There are different types of devices, desktops, smartphones, servers, gaming consoles etc, that are connected to the internet. One might wonder, how come a smart refrigerator in South Korea is able to talk and get data from a weather sensing station somewhere in the Hawaii through the internet? There must be some kind of common language, a rule or in geeky terms — a **protocol**.

Let me introduce you to the **Hypertext Transfer Protocol or HTTP**, in short. **HTTP** is the common protocol (or rule) where in all the devices no matter their underlying design, when connected to the internet, use it to communicate with each other in a way they could understand.

In **Hypertext Transfer Protocol**, as the name suggests, transfers something called ‘Hypertext’.

>For our reference, lets limit our understanding of a Hypertext as a document that contains some text and links that are displayed on a computer screen (or similar devices) which can be accessed by a user easily.

This Hypertext is usually obtained by sending a request from a client (your computer) to a remote server (some other computer connected to the internet) which then responds with a Hypertext document containing the data.

>An HTTP request should be accompanied by an HTTP verb — GET,POST,DELETE etc., usually by default it is GET. But again this blog isn’t about HTTP requests or computer networking, so for our reference, let’s assume GET is used to retrive the data from the server, POST is to send and DELETE is to send a request to delete data on the server.

So coming back to how our RESTful APIs get data, a client(the smart refrigerator) requests a **Resource** (weather data) to the server (a very busy and lonely computer in Hawaii) and the server responds with the resource (weather data again). Also, the REST APIs are **Stateless** — No data of the transfer is stored anywhere between the client and the server.


##Okay now, what can we do with APIs?
We can practically build any app for any purpose that we can think of!

 * Want to build an app that tells you the location of satellites orbiting the Earth? — https://api.nasa.gov/
 * Control your Tesla using Alexa? — https://www.teslaapi.io/ (not officially supported)
 * Build a weather app under 15mins? Sure — https://openweathermap.org/api
 * Want to check your local train times? — https://indianrailapi.com/
 * Did you say memes? Why not — https://imgflip.com/api

In the next blog, I’ll call one of these APIs to GET the data using several methods. Till then enjoy this comic:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0xt0v5pt5m34dhq6z8fi.jpg)

*For more: https://theycantalk.com/*


##Useful Links
* https://www.youtube.com/watch?v=GZvSYJDk-us&t=5086s — Awesome video from freeCodeCamp.org. Most of my content is from this video.
* https://developer.mozilla.org/en-US/docs/Web/API
* https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol