If you are reading this, then you probably already know what APIs are. If not, here is a short explanation:
API stands for Application Programming Interface.
- Application stands for user application. It is normally a program that you use on your computer.
- Programming represents that APIs are used for programming (or building) an application.
- Interface means a method or a way for one object to talk to another object.
APIs, thus, are programmatic means and methods using which a program can ask for something to be done. Normally, APIs provide certain services which can be used by an application and to use them normally, the application calls a method.
Types of APIs
APIs can be of multiple types:
- Local (System/Library) APIs: These are usually in form of a function/method call. The program(er) basically adds the library in one of the source files and calls a function.
- Remote (over-the-network) APIs: These are usually in form of calling an endpoint and waiting for the result. The programmer usually has to call a method which calls the remote resource using a protocol (socket, websocket, HTTP, custom) and gets the result once the remote computer has processed and returned the result.
Let’s get into a bit of depth:
Let’s look at a very common operation: saving a file on your computer’s storage device (usually a hard-disk). Sounds simple and common - after all we do this everyday, don’t we? And of course we also use different software to save files, e.g. Downloading a file from the web or by creating a text file or by modifying a word document.
Although common, saving a file is actually a complicated operation. When you create a new file, the computer has to look for free space on the disk and write the data there and then maintain a separate record of the location on disk where it stored the file. When you modify a file, it would typically store the data elsewhere and mark the old location as unused/empty. Now, accessing the disk is complicated thing. A disk can be a Hard Disk, a Solid State Disk (SSD), a USB device attached to the machine or even a network drive attached to the operating system.
However, if you have some basic idea of programming, especially around handling files - you would be able to recall that saving a file was not this complicated. You would typically call a fopen (or a similar function to open a file handler) and then use fwrite (or something similar to write data into a file) and then use fclose (or something similar) to close the file handler. And that was it!
This set - fopen, fwrite and fclose is a good example of a local API:
- They are functions. In some languages, they can be class methods, but the idea remains the same.
- They are available to you right on the machine and there is no network call. Even when you are trying to save a file on a network-mounted volumne/drive, you only call the function locally - it is the function which takes care of the network.
There are multiple types of APIs which are available locally for creating system applications - drawing things on screen, opening and using network connections and so on.
Let’s look at another common operation: sending a message to another human. Let’s assume that we are using WhatsApp, a popular messaging app.
One of the most popular forms of APIs is REST. REST stands for REpresentational State Transfer and is implemented over HTTP protocol.