What are APIs and Why do we need them

What are APIs and Why do we need them

Photo by Robs on Unsplash

It is a very common question to hear beginner developers ask, "Why do we need APIs?" I recently tried to explain the concept of an API to some friends, and it was particularly difficult for them to understand the real-world applications and how the abstract concepts translate into useful functionalities.
To tackle this the right way, the best way to start would be to understand what APIs are. An API is an application programming interface, which is really just a contract of service between two computer systems. In a server-client architecture, each side has to understand how to communicate properly with the other side. That is achieved through a set of protocols and rules that guide the entire communication process.
An analogy that I like to use is how, in a restaurant, there are chefs and there are customers. The chefs need the waiters and waitresses to deliver the food to the customers; those waiters are the protocols; without them, the flow of food (information) would be difficult and erratic.

Types of APIs

APIs are of various types, and some of the common ones are

  • SOAP APIs: These are relatively the oldest, and they stand for Simple Object Access Protocols. They provide very strict guidelines on implementation, and while they support HTTP for transport, they can also use other transfer protocols like SMTP, TCP, and UDP. They are more complex and slower compared to REST APIs, and these reasons contributed to their reduction in usage and adoption of RESTful APIs during their advent. Data transfer format in SOAP is usually XML.

  • RESTFUL APIs: REST stands for representational state transfer, and some of the principles guiding it are statelessness (the server does not store any context of the client between interactions) and cacheability, which means that some of the response can be stored on the client device such that response time can be reduced for similar subsequent requests. RESTful APIs only allow data transfer through the HTTP protocol, and verbs are used for operations (CRUD) such as GET, POST, UPDATE, DELETE, and PATCH.
    JSON is the most popular data transfer format in REST, but it also supports others like XML, plain text, and even HTML.
    Restful APIs, SOAP APIs, and GraphQL APIs

  • GraphQL APIs: These are personally my favourites to work with, allow the client to request only what they need from the server; this prevents issues of over-fetching and under-fetching. Imagine a situation where there is an endpoint to fetch student information. If a Rest API exposes the student's name, email, age, and faculty to you, then all of those will be returned to you, but with GraphQL, you can choose to fetch only the names and email addresses of the student.

Importance of APIs

The importance of APIs in software development cannot be overstated, and it is vital to note that they are part of the building blocks of applications that we have today.
Some of their advantages include:

  1. Third Party Integration: The concept of APIs means that third-party features can be used in separate applications in order to achieve functionality. An example is how you can build a food delivery service application and let Stripe handle and receive the payments for you from within your app. This is possible through the use of the Stripe API.

  2. Scalability: Since APIs are not tightly coupled with the applications that they are built for, they can be scaled independently, and new functionalities can be added without any major disruptions to the applications that consume them. An example is how multiple servers can be added and configured to accept requests and respond to requests from applications.

  3. Rapid Development: Since API endpoints are reusable, developers do not have to worry about building all of the functionalities from scratch. An example is how a company only needs to create an API for fetching employee information from the database, and every new application built after that can just consume the database through exposed endpoints.

  4. Cross-Platform Development: This means that applications can be developed for different platforms, but they all consume the same API endpoints. APIs are not restricted to specific platforms, which means that they can be used anywhere. An example is how the GraphQL endpoints that are used for the Facebook app on Android can also be used for the Web and iOS applications.

Challenges with APIs

It is helpful to note that they have their own limitations and challenges to be careful of in order to ensure best practices and address potential issues effectively.

  • Performance and Latency: Calls to APIs are network calls that can cause overhead, especially in the case of high-latency APIs, which might eventually lead to some performance issues.

  • Security Issues: APIs can be vulnerable to security attacks if proper security and authorization measures are not put in place. In cases like this, the data served by the API can be exposed unintentionally, thereby causing data leaks.

  • Availability: Depending on an external API also means relying on the availability of the API. An example is how if the Stripe API goes down due to some server-related issues, then that food company can't provide services too because it won't be able to receive payments until Stripe is back up.

In essence, it helps to understand that APIs are an important backbone of software development, without which communication between software components would be nearly impossible, if not completely impossible. Understanding them is a vital skill for every developer to have.