Introduction to APIs

Table of Contents

We are living in the world where things, by that I mean devices, are becoming more and more connected. Now, we even have the internet of things where these devices can communicate with each other and share data. Our smartphone can now talk we some of our electronic appliances and you can control them this way. Did you ever get a message from your fridge telling you that you are out of vegetable? Does it sound interesting to you? And this is only beginning. The future holds much bigger surprises. Bigger than we can imagine. Let’s start at least with exploring the APIs we have now available.

What are APIs?

In this article I will focus only on Web APIs and left the other libraries and protocols aside.

The API abbreviation originally means Advanced Programming Interface, as you can see by yourself, is now more known under Application Programming Interface. These interfaces are defined by HTTP requests with some content in XML or JSON (JavaScript Object Notation). APIs work on client/server relationship. Client – user – sends some request or data and server responds accordingly. In order to make this communication successful, the API must follow couple of principles called REST (Representational State Transfer). These principles are:

– separate the client from the server

– not hold state between requests all information necessary to respond or a request are available in each individual request, nothing is held by the server from request to request)

– use HTTP and HTTP methods.

Requests

Since I’m more into JavaScript, I will provide an example for JS. To make a request you will use the XMLHttpRequest command:

var request = new XMLHttpRequest(); // creating a variable for our new request
request.open('GET', 'http://www.twitter.com/', false); // making new request

HTTP Methods

We have four basic types of HTTP request:

1. GET – retrieves information

2. POST – sends new information

3. PUT – updates existing information

4. DELETE – removes existing information

The simplest HTTP request is made up by three parts – request line, header and body. The request line specifies the type of request (GET, POST, PUT or DELETE) and the source. Header sends other additional information like the id of a client. Body can be left empty (in case of GET) or contain some data (if you are posting – POST – or updating – PUT – something).

Another important part of any request is endpoint. Endpoints are location where data are stored. You can think about them simply as a piles of information.

If we would live in a perfect and safe world … we would probably die bored in short time, but we would also didn’t need any security features. Fortunately this isn’t true (at least not yet), so some APIs require also something called API Keys. These keys are long alphanumeric security codes generated for every user/client of an API. Through your personal API Key and ID, basically a username and password, you can access secured APIs. Thanks to IDs and Keys these APIs can also easily track and log your every move. Watch out.

Responds

After we talked a bit about requests we should also mentioned the other side – responds. Response is a message containing three-digit code which specifies the status of the server you are communicating with. These codes can start with 1, 2, 3, 4 or 5. 1 means that server is working on your request. 2 will be send to you if server is successfully responding to your request. 3 can mean that server is busy and you have to wait or you have to be re-routed to other address. 4 is very well known – 404 (server didn’t respond). 5 means that server probably freeze and can’t respond to your request.

Every respond contain 3 parts (like request) – response line (here you will find the status code), header (information about the server) and body (text of the response).

Example of response:

HTTP/1.1 200 OK // response line
Content-Type: text/xml; charset=UTF-8 // header

<!--?xml version="1.0" encoding="utf-8"?>
xmlns="http://www.codecademy.com/">Accepted
// these are the body

As said above, you can communicate with API through XML (Extensible Markup Language) or JSON (JavaScript Object Notation). You are not free to choose between these types. Most of the time, there will be one type which is supported and every API should have specified in its documentation which one it is.

It’s APIs, who makes sharing information easier than ever before. With their help, you can read and create posts on facebook or twitter, get latest weather information or play some video on youtube without opening these pages in your browser. If you know a bit about coding, you can program some simple app by yourself. Why should open many tabs to view the information you want to know if you can tinker with code and get all of them in one place. You can also use APIs as an enhancement in your mobile apps and games (if you are a developer) – sharing your game score on your favorite social network etc.

If you liked this article, please subscribe so you don't miss any future post.

If you'd like to support me and this blog, you can become a patron, or you can buy me a coffee 🙂

By Alex Devero

I'm Founder/CEO of DEVERO Corporation. Entrepreneur, designer, developer. My mission and MTP is to accelerate the development of humankind through technology.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.