Key Takeaways
API is a set of defined rules and protocols that enable different software applications to communicate with each other.
92.7% of developers spend their time testing or planning to test their APIs.
Open APIs, internal APIs, partner APIs, composite APIs, and RESTful APIs are some of the different types of APIs.
Introduction
Did you know there's a software interface that has over 3.2 million repositories on GitHub? It's used by 92.7% of developers and in 2020, 29.5% of developers spent their time building it. That software interface is an API.
As a software engineer, you must have probably interacted with an API without even realizing it. So, what exactly is an API?
Imagine you want to travel and experience the world, but dealing with multiple airlines, hotels, and tour operators seems daunting so, you engage the services of a travel agent. You provide the travel agent with your preferences, and they handle all the bookings and arrangements on your behalf, shielding you from the complexities of dealing with multiple travel providers.
In this analogy, the travel agency serves as the API, enabling communication between you, the client, and the various airlines, hotels, and tour operators, who act as the server. You don't need to understand the inner workings of the API; you simply pass your request to it, and it handles the communication on your behalf, returning the result.
What is an API?
API stands for application programming interface (API). It is a set of defined rules and protocols that enable different software applications to communicate with each other.
APIs allow various software programs to communicate with one another. It can be used for a variety of purposes, including retrieving data from external sources, integrating different software systems, and providing access to a service or platform.
How API Works
APIs act as a bridge to facilitate interaction and communication between various software programs. Whether that is an information request, an e-commerce transaction, sending money to a bank account, or any other situation requiring a two-way data transfer.
The client submits a request containing details about the desired action to a particular API endpoint (URL). The request body and, if applicable, the request headers are processed by the API after it receives the request. The API then carries out the required tasks or accesses the needed information.
The API's security settings may require the client to include authentication credentials in the request to verify its identity and permission to access particular resources.
Image by Freepik
Types of APIs
There are several types of APIs, including:
Open APIs: Open APIs are APIs that are available in the public domain and can be accessed by third-party developers and users outside of the organization that created them. Open APIs have defined API endpoints and request and response formats. Examples of Open APIs include Google Maps API, Stripe API, SendGrid, PayPal, etc.
Internal APIs: Internal APIs, also referred to as private APIs, are used within an organization to facilitate communication and integration between various parts, services, and systems. They are safe and inaccessible to outsiders because they are only accessible within the organization's private network.
Partner APIs: These are APIs that are shared between different organizations that have a partnership or business relationship. They are only available to specifically selected and authorized outside developers or API consumers, as a means to facilitate business-to-business activities.
Composite APIs: Composite APIs also known as mashup APIs are APIs that combine data from multiple sources or services into a single API. They perform the role of intermediaries, giving programmers a quick and easy way to access data from various sources through a single endpoint. In addition to encouraging modularity and reusability in software development, composite APIs seek to lessen the administrative burden associated with overseeing multiple API calls and data transformations.
RESTful APIs: These APIs transfer data between systems using the Representational State Transfer (REST) architecture, which Roy Fielding first described in his doctoral dissertation in 2000. RESTful APIs are a crucial component of contemporary web development because they are frequently used for online communication and data exchange between various applications and services.
API Protocols
API Protocols are a set of rules and conventions that governs the communication and exchange of data between different software systems and applications. They define the format, structure, and rules for sending data and the actions that can be performed on that data. Here are some common API protocols:
REST (Representational State Transfer): REST is a set of web API architecture principles. They adhere to certain REST architectural constraints. It’s possible to build RESTful APIs with SOAP protocols, but the two standards are usually viewed as competing specifications. They use HTTP requests to perform CRUD (Create, Read, Update, Delete) operations on resources. REST APIs are widely used for communication and data exchange between different applications and services over the internet, making them a fundamental building block of modern web development.
SOAP Protocol: SOAP is an acronym for Simple Object Access Protocol; It is an XML-based protocol for accessing web services over HTTP defining how web services talk to each other or talk to client applications. SOAP APIs make it easier to share information between apps or software components that are running in different environments or written in different languages. SOAP is primarily used in enterprise-level applications and legacy systems where XML-based messaging is required.
JSON-RPC: This uses JSON (JavaScript Object Notation) as the data format for both request and response messages to transfer data instead of XML. It is commonly used in scenarios where a lightweight and simple remote procedure call mechanism is needed. It is often used in client-server architectures, APIs for embedded systems, and other applications where JSON is the preferred data format.
GraphQL: Released in 2015 by Facebook, GraphQL is not a protocol; rather, it is a query language and runtime for executing queries with your existing data. It is often used as an alternative to traditional RESTful APIs for fetching and manipulating data from servers.
APIs In Action
In the example that follows, we'll use the Fruityvice API to retrieve the first 10 fruits and their attributes stored in the database. The Pandas DataFrame library will be used to present our findings in a structured tabular format.
import requests
import json
import pandas as pd
import requests
: This imports the requests
library, which enables HTTP requests to be sent to web servers.
import json
: This imports the json
library, which provides functions for working with JSON data.
import pandas as pd
: This imports the Pandas library with the alias pd
. Note: the alias can be named anything, in this case, we used pd
.
fruitydata = requests.get("https://fruityvice.com/api/fruit/all")
result = json.loads(fruitydata.text)
fruitydata = requests.get("
https://fruityvice.com/api/fruit/all
")
sends an HTTP GET request to the specified URL using the requests.get()
function.
result = json.loads(fruitydata.text)
: The json.loads()
function parses the JSON data returned by the API and converts it into a Python dictionary.
pd.DataFrame(result).head(10)
.DataFrame(result).head(10)
creates a Pandas DataFrame using the pd.DataFrame()
function, which converts the Python dictionaryresult
into a tabular format. The .head(10)
function is used to display the first 10 rows of the DataFrame (0-9).
Conclusion
Application Programming Interfaces, or APIs, are the cornerstone of modern software development and provide access to the connected digital world. In addition to helping developers create sophisticated and cutting-edge solutions that meet users' constantly changing needs, APIs play a significant role in facilitating communication between a variety of applications and services.