This article is a collaboration with UPDIVISION, an outsourcing company building complex software for companies all over the world.
Picture this: you’re going to the post office to send a package to a friend. The employees will fill all the necessary forms, register the package, generate a tracking code, and get the ball rolling in the delivery process. You, the sender, will only provide the package itself with a shipping label on top. While your friend, through the work of the postal service employees, will receive their package.
In software development, processes like these happen through APIs.
So today, we are going to look into APIs, their usage, and examples. Along with it, I will list down 3 Github repositories for APIs.
So, what is an API?
An API is a set of programming codes that enables data transmission between one software product and another.
This is the basic definition of APIs. Let's do a deep dive to truly understand it. API stands for Application Programming Interface. It can act as a bridge between two applications, products to share the data.
Suppose we are going on a weather application. It will display weather information according to the location provided by the user. Let's divide our application into 3 parts: frontend(application), database, and the API.
Frontend: This is the frontend part of our application containing all the UI design, Javascript, and containers.
Database: It contains all the data regarding the weather. The data in the database is stored and managed by a different organization. This is data that keeps on updating at a regular time interval.
API: Our frontend needs data from the database to be able to perform its task. So here the API provides a bridge between the frontend and the database. This API will help in exchanging the data between the two.
API Endpoints: It is the point at which the two elements, here database and frontend, communicate with each other. Web-based API has URLs as endpoints. For example:
Examples of APIs
As the name suggests, it's a weather API. It provides data related to a weather app. It’s free and refreshes every 5 minutes. It supports most of the major locations around the world. Its free trial is best for developing a weather website.
In my opinion, it is the best free movie-based API available on the internet. It has many endpoints for different queries such as popularity, year, movies, series. etc. You can develop a movie and series tracker with this API, or anything else your imagination comes up with.
It is a currency exchange rate monitor API. It has the latest as well as the historical data for the exchange rate.
The birth and growth of APIs
The term API traces all the way back to the 1940s. However, its definition today is vast and different compared to the original one.
The first modern uses of API started in the early 2000s. During this period, the eCommerce business was booming. Amazon, eBay, and Salesforce were leading the industry.
These companies wanted to reach more people. They decided to put their data in the public domain so that developers could use them and get creative. Salesforce was first: they launched their API on February 7th 2000. It was an XML-based API.
After this, in November 2000, eBay also launched its API. Followed by Amazon, who released it in 2002. The latter allowed other developers to feature Amazon products in their searches - which was the very first use case of modern APIs.
Reaching technical stardom
Due to the success of eCommerce-based APIs, more companies started to provide theirs. The use cases of APIs also went beyond data sharing.
In 2004, photo-sharing platform Flickr launched its own API. It allowed the user to embed images from Flickr into their website. In 2006, Facebook and Twitter also launched their APIs. Users were able to view profiles, images, events, and information via the Facebook API - while not being on Facebook itself.
As time went by, APIs grew and companies found more uses for them. For instance, Twilio is one of the first applications that used APIs as a full system. Earlier it was one part of a bigger structure. Twilio uses an API to make calls and send messages, and it was launched in 2008.
Powering IoT-based devices
Today, IoT-based devices are running on the API interface. Alexa, FitBit, Google Homes are based on APIs for all their features. Companies like Tuya Global are providing APIs to control IoT-based devices.
Today, APIs are being used to share data, communicate, run applications, and many more. So, they’ve evolved a lot from their early days.
Security of APIs
Most APIs are free to access without any limitations - and they’re able to bear the huge traffic. On the other hand, some APIs that need to be updated after a certain interval of time - like weather APIs - put restrictions on their usage.
These APIs provide a secret key to the user, so that they can verify every call. A security key is mentioned in the URL or as an authentication. A security key is a combination of alphanumeric characters, kind of like these: abcdef12345
, jjfdsaf564asdf5f
.
An API endpoint with a security key can look like this:
https://api.openweathermap.org/data/2.5/weather?q=london&appid= {security_key}.
Another, more secure, way to authorize a user is using OAuth2 protocol. Instead of using a permanent token, as in the above example, this protocol allows users to generate new tokens which are valid just for a limited time. The frontend service will receive the generated token and the refresh token. It can use the token to call the API’s endpoints. If the token expires in the middle of the session, the frontend service can get a new valid one using the refresh token without asking the user his credentials. It also provides a way to restrict access using scopes.
Types of APIs
There are 3 major API types based on their architecture:
REST APIs
REST stands for representational state transfer. It is also known as RESTful API. It is the most common way of developing a web-based API. It uses a server/client approach to transfer data. When a client calls the API, the server responds by providing the data.
Data Received from a REST API:
SOAP APIs
SOAP is a Simple Object Access Protocol. It is being used to create web APIs using XML. Its call and response consist of 3 major elements i.e, Envelope, Header, and Body.
Envelope: The essential part of every SOAP call and request. It contains the header.
Header: Optional element. It can be used to pass authorization.
Body: It consists of the data used to call or receive from the response.
Data Received from a SOAP API:
RPC APIs
Remote Procedural Protocol(RPC) is used to send multiple parameters to the user. RPC can be implemented with two languages, JSON and XML. It is then called JSON-RPC API and XML-RPC API accordingly.
Data Received from an RPC API:
Fetching API data in JavaScript
To use the API data in our application, we need to fetch it. There are various methods to fetch it using libraries or with a simple fetch() function. We are going to use the basic fetch() function to fetch the data in our application.
fetch(): We need to provide the endpoint URL of the API inside quotes in the fetch. The response contains all the information about the call to the API. It contains data such as response type, body, header used, status, and many more. We use the JSON() method on the response to convert the body into the JSON format. After this you have the data, which you can store or log into the console, as required.
SWAPI is a fun Star Wars API. It contains a lot of data related to Star Wars such as people, films, planets, spaceships, etc. You can visit their website here.
fetch("https://swapi.dev/api/people/1")
.then(response => response.json())
.then(data => console.log(data))
Output
{
"name": "Luke Skywalker",
"height": "172",
"mass": "77",
"hair_color": "blond",
"skin_color": "fair",
"eye_color": "blue",
"birth_year": "19BBY",
"gender": "male",
"homeworld": "https://swapi.dev/api/planets/1/",
"films": [
"https://swapi.dev/api/films/1/",
"https://swapi.dev/api/films/2/",
"https://swapi.dev/api/films/3/",
"https://swapi.dev/api/films/6/"
],
"species": [],
"vehicles": [
"https://swapi.dev/api/vehicles/14/",
"https://swapi.dev/api/vehicles/30/"
],
"starships": [
"https://swapi.dev/api/starships/12/",
"https://swapi.dev/api/starships/22/"
],
"created": "2014-12-09T13:50:51.644000Z",
"edited": "2014-12-20T21:17:56.891000Z",
"url": "https://swapi.dev/api/people/1/"
}
Use cases of APIs
APIs provide data useful in building an application. And so, public APIs can help individual developers with data.
This method is also used to exchange data between the frontend and backend.
API Repositories
Here is a list of GitHub Repositories that contain APIs you can use in your apps:
public-apis
This is a collective list of APIs that are available to the public. It has around 180K stars on GitHub. You can develop various kinds of applications using these APIs. The list is categorized into Anime, CryptoCurrency, Entertainment, Weather, and others.
public-api-lists
Another great repository to find good APIs for development. It has more than 4K stars on GitHub. These APIs are more inclined towards the development side. It is also organized into suitable categories.
Public-APIs
More public APIs list to find the best for your project. Public-APIs will help you in finding the best API for your project. It has more than 17K stars on GitHub.
Conclusion
We have seen the definition, explanation, examples, history, fetching data into the application, uses, and repositories to find APIs for web development projects. The power of APIs is turning a simple website into a web application with data from the API. Learning APIs and developing a project with them is crucial in web development.
I hope this article has helped you in understanding APIs. Thank you for reading this blog post.
To find out more about code, the software development process, or to have awesome apps built for your business needs - contact UPDIVISION.
Top comments (0)