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 :
- Flexibility (Think microservices).
- Simplifies design.
- Easy to main and administer.
- And leaves scope for innovation
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:
- Maintaining standards across various components.
- Reusability of code.
- Documentation: readability increased among programmers.
- Abstraction: exposing only relevant information for other systems and protecting business logic.
- Designing, Testing, Building, Managing, and Versioning systems, becomes easier.
Developing an API
- Analyzing the specifications
- 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.
- 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.
- 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.
- 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:
- lat,lon: Geographical coordinates (latitude, longitude).
- appid: Your unique API key (you can always find it on your account page under the "API key" tab)
- exclude: By using this parameter you can exclude some parts of the weather data from the API response.
- units: Units of measurement. standard, metric and imperial units are available.
- lang: You can use the lang parameter to get the output in your language.
Example of API call:
Example of API response :
"sender_name": "NWS Philadelphia - Mount Holly (New Jersey, Delaware, Southeastern Pennsylvania)",
"event": "Small Craft Advisory",
"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.",
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