A Layman’s Guide to (REST) APIs

Hemant Jain
Code Dementia
Published in
5 min readJul 22, 2020

API’s are all around us. Not only are they on the internet, but there are numerous APIs that are controlling your system as well. Your operating system uses a lot of APIs to make sure that everything in your system works well.

APIs help different programs talk to each other and offer their services to one another. When a program exposes an API, it means that the program is providing some functionality that it can perform if it is asked to do it. A user or another application can ask it to perform any action that it offers, and the program exposing the API will perform the action.

An analogy for an API could be a barber or a hairdresser. The barber tells you what he can do for you: give you a nice hairstyle, trim your beard or give you a face massage. The barber is exposing the services he can offer, just like an API can expose its services to other programs or users.

Another analogy could be that of a bank clerk. The analogy of a bank clerk can help you understand a CRUD API really well.

A CRUD API does four things for you:-

  1. Creates new data
  2. Reads existing data
  3. Updates existing data
  4. Deletes existing data

Hence the acronym CRUD. This type of API is prevalent in web or app development since most of these programs deal with the manipulation of data. Coming back to our bank clerk analogy, the clerk can create new deposits for you, the clerk can read your account balance for you, the clerk can update your bank balance and can help you remove(delete) your accounts from the bank. A CRUD API exposes the same kind of functionality for users and other programs.

Let’s take an example of Instagram.

  1. When you post new photos, one of their APIs helps you to create a new photo post along with a caption.
  2. When you are browsing through your Instagram feed, their API is reading photos posted by others for you.
  3. When you update your caption on some photos, their API is updating the caption for you.
  4. When you delete some posts from your profile, their API deletes those posts for you.

Similar(and much more complex) types of APIs help you interact with all the apps on your phone like Amazon, Whatsapp, Youtube, and a whole lot of others.

I only talked about a CRUD API, but there are many types of API(such as in your OS) that perform a whole lot of other tasks and are just as crucial as CRUD APIs. I just took the example of CRUD since it is easy to relate to the real world and can help you understand the basic idea behind an API.

REST API

REST API stands for Representational State Transfer API and is a guideline or a set of principles for designing an API. Some people think it is a new type of API with different functionality like CRUD API, but it is simply a “way” in which APIs are written with a particular set of rules.

An API has to follow these six guiding principles to be called a REST API:

Client-Server Architecture

Web APIs(and REST APIs) work on a client-server architecture. The client sends a request to a server, and the server responds by giving a particular piece of data to the client. This is called the Request-Response Cycle.

For a REST API, The client and the server have to be independent of each other. The changes on the server shouldn’t affect the client and vice versa. This helps the client and the server to scale(grow in size or capacity) on their own by not affecting each other.

Stateless

The request sent by a client to a server must contain all the information for the server to respond to that request. The server cannot take advantage of any stored state of the client that is communicating with it.

Cache

Caching means to store data that is being frequently accessed. REST APIs say that a request or a response should clearly state if it is cachable or not. If it is cachable, then the client should be able to cache the response and reuse it if it needs it again.

This reduces the load on the server, and since the client reuses the information it would have requested from the server, it also makes the user experience faster. An example of this would be the caching of web pages done by your web browsers so that they load quickly.

Uniform Interface

All the clients communicate with the server in a standard way by using a standard protocol(usually HTTP) and by using a standard way of asking for data(URIs). The responses are also sent in a standard way(usually JSON or XML), providing a uniform interface for the clients and the servers on the network.

Layered System

The API should have layers to it. All the layers should show a clear hierarchy where the inner layers provide information to the outer layers. These layers should be loosely coupled, meaning that changing one should not cause the need to make significant changes to the other layers. This layered system makes it easy to upgrade the API and add new parts to it.

Code On Demand (Optional)

Code on demand means that the server can send not only data but code that can be executed by the client. This principle is usually not followed due to security risks and the reduction of efficiency.

Advantages Of A REST API

  1. REST APIs scales very well and can be extended to a large number of users.
  2. Since the server and client are independent of each other, it becomes very easy to modify data and functionality on the server. It is easy to upgrade and maintain.
  3. Because of a layered system, parts of the API can be changed and upgraded without breaking the functioning of the entire API.

Some Popular APIs

Google Maps API -https://developers.google.com/maps/documentation

Twitter API -https://developer.twitter.com/en/docs

Facebook & Instagram API- https://developers.facebook.com/

REST APIs are very popular and are used by most of the major websites such as Facebook, Instagram, Netflix, and countless others since they scale very well and can handle millions(or even billions!) of users at any time.

This was a brief introduction to the world of APIs, and I have tried to explain it as simply as possible. Most of the sites that you visit and the apps you use, utilize the power of APIs to serve you, and they are an essential part of the internet. If you are looking to work as a web or app developer, a fundamental understanding of APIs will surely help you along the way.

Thank You

--

--

Hemant Jain
Code Dementia

Growing And Trying To Understand The Subtleties Of The World.