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. You normally have the app installed on your phone and you search for the contact, type the message and hit the send button. The message get delivered. How?
The app calls its servers, sends a token (which identifies your phone number), the target phone number (or account) and the message. The server receives that data and tells your app that the message has been received by the server and will be delivered in due time. One it delivers the message (and when the message is read), the server connects to the app on your phone and tells that the message has been delivered (or read).
All of this happens over network. So for every action where the app needs to talk to the server, it makes a call over network. The important part here is: the app needs to know what message to send to the server for getting things done. This is just like a local program needs to know which function calls it has to make for reading and saving files.
One of the most popular forms of APIs is REST. REST stands for REpresentational State Transfer and is implemented over HTTP protocol. REST is a standard set of guidelines where the action to be taken (read, create, modify or delete) are specified in the HTTP method (like GET, POST, PUT, PATCH, DELETE etc.) and the thing that it needs to work on (often called a resource) is specified in the URL.
APIs or Application Programming Interfaces are methods which can be called by a client (web browser, local executable file, mobile apps) to get something done. The APIs are implemented on the server (the system that gets the work done) and are consumed by the client. The terms implementation and consumption as such as standard terms used for making clear the role of the thing we are talking about.