Axiros | Open Device & Service Management

View Original

Introduction to API


What is an API?

In the modern world, most Internet devices rely on APIs to function properly. But what actually is an API? The word API is an acronym for Application Programming Interface. What does that mean? An interface is just a fancy word for a software component that can communicate with other components. So an API enables an application, or program, to communicate with something else. And these communication interfaces are programable, so that they can respond differently to different inputs. So that’s the basic function of an API - to communicate.

Why would I need that?

As a matter of fact for literally everything. APIs are in use everywhere when something needs to communicate with something else. That may begin on a low level computer view. When your operation system wants a file from your hard drive, it needs some way to ask for it. Or on a higher level, how do you think your phone gets your newest messages? Or how does it know the time? Obviously, via the Internet, but without APIs there wouldn’t be a way to easily do that. With APIs, the messaging application on your device can just ask its servers to send over the newest messages.

But how do they understand each other?

Like in the real world, to communicate properly there must be some kind of language so that different programs can understand each other. And that’s where protocols come into play. Basically, a protocol is a set of rules of how to communicate certain information. There are a lot of protocols, all specialized for a specific use case. The one you probably use the most is HTTP - the HyperText Transfer Protocol. It is used by web browsers, like Google Chrome, to transfer millions of webpages right to you. So if you’ve ever wondered what the HTTPS in your browser's url bar means, now you know. The S means Secure by the way. So if you stumble upon a website with only HTTP, it may be better to leave it. But the end user experience is of course not the only thing protocols and APIs are used for. Another example would be the communication between your Internet access device, like your router, and your Internet Service Provider. The protocol used for that is TR-069 and transfers maintenance and metadata information about your device, so that the ISP can ensure that it works correctly. In some cases the ISP can even push updates to your device over it. How neat! The device can also periodically ask the TR-069 server for some information.

Different API Types

The type in this case does not mean the protocol a specific API uses, but the different paradigms they can use. A very popular example for this would be REST, which stands for REpresentational State Transfer. In short, it means that an API like this needs to be stateless, so that each call to the API is unrelated to any other. The server does not have a state in the sense that it does not save anything from your previous requests and uses it in the next one. But it doesn't mean you can't send data to the server. There are different types of REST requests. The most important ones are GET, POST, UPDATE and DELETE. These are used to do different things with the API.

  • GET is commonly used to fetch one or more data entries from the server. Usually it is done with some kind of object id.

  • POST is used to create new entries on the server, these requests usually include a body, which has different parameters describing the new object.

  • UPDATE and DELETE do pretty much what there names imply, they usually also identify the object the operation targets with an id, like the GET request. An UPDATE request also has a body like the POST request.

Another benefit of REST APIs is that as long as the parameters for the different requests types don’t change, the server and client components can be changed independently from each other. REST is used a lot in combination with the HTTP protocol to create flexible and easy to use web APIs.

Another example for this would be SOAP - the Simple Object Access Protocol. This one is a bit more complex than REST. While it has the same general purpose as REST, to exchange different data between clients and servers, it uses an XML format to achieve that. With that, complex objects can be described and transferred. SOAP is also a lot stricter than REST, which also has to do with some security features that REST does not offer. But due to its structure, it is not as lightweight and flexible as REST. To implement SOAP, some extra components are needed to generate the XML structures every time.

Example:

This script uses NTP - the Network Time Protocol. Most of the work here is done by the ntplib python library. We just have to give it the name of our preferred time server, in this case pool.ntp.org. The library establishes a connection to the server and asks for the current time. We can then extract that value from the response. The long number represents the seconds since the so called epoch, that is January 1 1970 00:00. The ctime function can interpret it and we get the current date

Hopefully this short introduction to APIs helps you better understand how the communication in computers, but most of all in the Internet of things, works.