Skip to main content

What is a API?

An API (the full form is Application Programming Interface) is an interface for communication between two systems. API is a set of definitions and protocols for building and integrating applications to talk to each other in a reliable and simple manner.

API offer :

  1. Flexibility (Think microservices).
  2. Simplifies design.
  3. Easy to main and administer.
  4. And leaves scope for innovation
note

API allows multiple software systems to communicate with each other, with a well-defined computing interface and protocols.

API in the real world

Mobile Application: The UI component of an app is rendered by the Application itself(Flutter, React Native etc ), but the data that is displayed is fetched using APIs(from a server that interacts with a database) usually in JSON. In a real-time application like Instagram, Facebook or Amazon, the data is displayed through APIs. Weather information, emails, and live game scores are all fetched from APIs that are hosted on a server.

Similarly, modern websites fetch on-demand data, by making AJAX calls, with web sockets, this interaction between different components happens via APIs.

Whenever you click on a like button of a social media website, a PUT request will be made via an API to update the like counter. The social media Share button use API, provided by other services like WhatsApp, Gmail, and Telegram to generate links that can be shared.

How to use an API

The main goal of an API is to simplify the complexity of the problem that it is trying to solve, without the system that is requesting the API knowing the internal complexity of that API. All we need to know is how the API should interact with other systems.

For example, Let's consider a movie ticket booking application, a server must expose an API endpoint to check the availability of a movie in a particular theatre. Any time a customer logs in via Mobile/Web Application a request must be made to the API endpoint based on the user preference. The booking API can further process the details of the movie tickets when the customer proceeds to book the tickets and generate an invoice triggering another API to generate it.

Booking API need not know how the invoices are being generated, it should only pass relevant information to to invoices API(for example number of tickets, movie name etc).

Likewise, any application can interact with multiple API. The customer who is trying to book ticket need not worry about the complexity of the system.

What is the purpose of an API

Every application is developed in pieces, to avoid writing code multiple times which serves the same purpose, this code can be exposed with API making it reusable. Such reusable components can be exposed to other components of systems via APIs.

Advantages of an API-based system:

  1. Maintaining standards across various components.
  2. Reusability of code.
  3. Documentation: readability increased among programmers.
  4. Abstraction: exposing only relevant information for other systems and protecting business logic.
  5. Designing, Testing, Building, Managing, and Versioning systems, becomes easier.

Developing an API

  1. Analyzing the specifications

API should:

  • Maintain a standard.
  • Should be clean, and easy to interact with.
  • API should be Well documented, with examples and how to interact with them.

The Specification should answer the following questions :

  • API should clearly define the business logic that it is going to solve.
  • API should have a URL or an interface.
  • API should maintain standard protocols and methods.
  • API should have a Request structure(headers, POST/GET etc) and define the fields.
  • API should have a Response structure(JSON) and each label should be clearly defined.
  • API should have valid values in requests as well as responses and appropriate status codes.
  • A mechanism for limiting, sorting or querying the data.
  • An authentication/authorization mechanism, to securely interact with API
  • Should display error and success codes whenever necessary and any other information, for another system to interact with the API.
  1. API Security

API must be secure, it should never expose confidential information to the public(credit card details,). The API must be protected from unauthorized access. Only authorized/verified users with secure credentials should be able to access the API. Constantly monitor the API for any malicious activity.

  1. API Logging

An API exposed, should be monitored to understand if it is being abused or not and maintain a log audit trail if things go south and the issue can be traced back, so the system can be made more resilient.

Maintain alerts for API, in case some users try to misuse by DDos, and maintain a rate limiter if needed.

  1. Performance of an API

The utmost priority of API is to handle performance during peak loads. API should be fast and fault tolerant, slow API could allow the user to drop off from the application resulting in loss of business.

Weather API example:

https://api.weatherreport.org/data/1.0/onecall?lat={lat}&lon={lon}&exclude={part}&appid={API key}

Parameters :

  1. lat,lon: Geographical coordinates (latitude, longitude).
  2. appid: Your unique API key (you can always find it on your account page under the "API key" tab)
  3. exclude: By using this parameter you can exclude some parts of the weather data from the API response.
  4. units: Units of measurement. standard, metric and imperial units are available.
  5. lang: You can use the lang parameter to get the output in your language.

Example of API call:

https://api.weatherreport.org/data/1.0/onecall?lat=33.44&lon=-94.04&exclude=hourly,daily&appid={API key}

Example of API response :

{
"lat": 39.31,
"lon": -74.5,
"timezone": "America/New_York",
"timezone_offset": -18000,
"current": {
"dt": 1646318698,
"sunrise": 1646306882,
"sunset": 1646347929,
"temp": 282.21,
"feels_like": 278.41,
"pressure": 1014,
...
]
},
"minutely": [
{
"dt": 1646318700,
"precipitation": 0
},
...
},
"hourly": [
{
"dt": 1646316000,
"temp": 281.94,
"feels_like": 278.49,
"pressure": 1014,
...
}
"daily": [
{
"dt": 1646326800,
"sunrise": 1646306882,
"sunset": 1646347929,
"moonrise": 1646309880,
"moonset": 1646352120,
},
...
},
"alerts": [
{
"sender_name": "NWS Philadelphia - Mount Holly (New Jersey, Delaware, Southeastern Pennsylvania)",
"event": "Small Craft Advisory",
"start": 1646344800,
"end": 1646380800,
"description": "...SMALL CRAFT ADVISORY REMAINS IN EFFECT FROM 5 PM THIS\nAFTERNOON TO 3 AM EST FRIDAY...\n* WHAT...North winds 15 to 20 kt with gusts up to 25 kt and seas\n3 to 5 ft expected.\n* WHERE...Coastal waters from Little Egg Inlet to Great Egg\nInlet NJ out 20 nm, Coastal waters from Great Egg Inlet to\nCape May NJ out 20 nm and Coastal waters from Manasquan Inlet\nto Little Egg Inlet NJ out 20 nm.\n* WHEN...From 5 PM this afternoon to 3 AM EST Friday.\n* IMPACTS...Conditions will be hazardous to small craft.",
"tags": [

]
},
...
]

The above API call gives the essential weather data for a specific location based on :

  • Current weather
  • Minute forecast
  • Hourly forecast
  • Daily forecast
  • National weather alerts
  • Historical weather data

Thanks for reading!

If you liked this article, feel free to check out and explore more topics at socialcol.com