Request: Processing: Response: Delivery
Request: Processing: Response: Delivery
Definition
An API, or Application Programming Interface, is a set of rules and protocols
that allows different software applications to communicate and exchange
data with each other. Think of it as a messenger between two applications,
enabling them to interact and share information.
APIs are the building blocks for the today’s websites in which heavy data is
transferred from the client to server and vice versa.
APIs are essentially a contract or interface that defines how one software
application can interact with another.
How it works:
When one application needs to access data or functionality from another, it
sends a request through the API, which then translates that request and passes it
to the target application. The target application processes the request and sends
back a response, which the original application receives through the API.
APIs work in a simple step-by-step process:
Request: A client (user) sends a request through the API's URI (Uniform
Resource Identifier).
Processing: The API forwards the request to the server.
Response: The server processes the request and sends the response back to
the API.
Delivery: The AsPI returns the server's response to the client.
Examples:
Web development: APIs allow web applications to communicate with
servers and databases to retrieve and display information.
Mobile apps: APIs enable mobile apps to access data and functionality from
cloud services or other applications.
IoT devices: APIs facilitate communication between IoT devices and other
systems, allowing them to share data and control functionality.
Third-party services: APIs allow different applications and web services to
work together and share data.
Microservices: APIs are used to build microservices architectures.
Benefits:
Reusability: APIs allow developers to reuse existing functionality and
data instead of building everything from scratch.
Integration: APIs enable different applications and systems to integrate
and work together seamlessly.
Innovation: APIs facilitate the creation of new business models, products,
and services by enabling easy integration and rapid innovation.
Scalability: APIs can help developers build scalable applications that can
handle large amounts of traffic and data.
***What is a URI?
A Uniform Resource Identifier (URI) is a string of characters used to identify a
resource on the internet(whether it's a logical or physical resource). A URI can be
broken down into several components:
Scheme: Specifies the protocol (e.g., https, ftp).
Authority: Identifies the domain (abstractapi.com).
Path: Specifies the resource location (/v1/).
Query Parameters: Provide additional information (?
[email protected]).
Fragment: Points to a specific section within a resource (rarely used in
APIs).
For example, in the URI:
https://emailvalidation.abstractapi.com/v1/?
[email protected]
https is the scheme.
emailvalidation.abstractapi.com is the authority.
/v1/ is the path.
[email protected] is the query parameter.
***Microservices Vs APIs
Microservices and APIs are related but distinct concepts in software
development.
Microservices are an architectural style where an application is broken
down into smaller, independent services. APIs, on the other hand, are the
protocols and rules that allow these services, or any other software, to
communicate and exchange data.
Microservices often use APIs to expose their functionality to other services
or external clients.
Microservices use APIs:
Microservices often use APIs to expose their functionality to other
microservices or external clients, allowing them to interact and exchange
data.
Microservices:
Microservices architecture is a software development approach where an
application is built as a collection of independent, small services, each
with its own functionality and database.
Benefits of Microservices:
Increased Agility: Easier to make changes, deploy new features, and
respond to evolving business needs.
Faster Development Cycles: Independent development and
deployment allow for quicker iteration and innovation.
Better Scalability: Individual services can be scaled independently to
handle varying workloads.
Improved Fault Tolerance: If one service fails, others can continue to
operate.
Resource Efficiency: Only necessary services are scaled, reducing
infrastructure costs.
Enhanced Collaboration: Different teams can work on different
microservices simultaneously.
Challenges of Microservices:
Increased Complexity: Managing a distributed system of microservices
can be complex.
Data Consistency: Maintaining data consistency across multiple
databases can be challenging.
Service Discovery: Finding and connecting to other services in a
distributed environment can be complex.
Monitoring and Logging: Centralized monitoring and logging can be
challenging for distributed systems.
Debugging: Debugging failures across multiple services can be more
complex.
Types of APIs:
APIs are broadly categorized based on their purpose, audience, and underlying
architecture. Commonly, they are classified as Open APIs, Partner APIs, Internal
APIs, and Composite APIs, and are further distinguished by their protocol (like
REST, SOAP, or GraphQL). Additionally, APIs can be categorized by their
architecture, such as monolithic or microservices APIs.
Here's a more detailed breakdown:
By Architecture:
Monolithic APIs: Single, coherent codebase providing access to a
complex data source.
Microservices APIs: APIs that are built using a microservices
architecture, where each API provides a specific functionality.
By Protocol:
REST (Representational State Transfer): A popular type of API that
focuses on using HTTP methods to interact with resources.
SOAP (Simple Object Access Protocol): A protocol that uses XML
messages for communication between applications.
GraphQL: A query language for APIs that allows clients to specify the
exact/only data they need, improving efficiency.
RPC (Remote Procedure Call): A protocol that allows procedures to be
called on a remote server as if they were local.
Webhooks: A type of API that allows applications to receive real-time
updates from other applications.
REST API
REST stands for Representational State Transfer, and follows the constraints of
REST architecture allowing interaction with RESTful web services. It defines a set
of functions (GET, PUT, POST, DELETE) that clients use to access server data. The
functions used are:
GET (retrieve a record)
PUT (update a record)
POST (create a record)
DELETE (delete the record)
Its main feature is that REST API is stateless, i.e., the servers do not save clients'
data between requests.
How it Works:
1. Client Request:
A client application sends a request to a RESTful API using a specific HTTP
method (e.g., GET to retrieve data, POST to create data, PUT to update data,
DELETE to delete data).
2. Server Response:
The server processes the request and returns a response, typically in a
standard format like JSON or XML, along with an HTTP status code indicating
the success or failure of the request.
3. Data Exchange:
The client application then uses the received data to update its user interface or
perform other actions.
SOAP API
SOAP, or Simple Object Access Protocol, is a messaging protocol. It allows the
exchange of structure information without any platform. Soap uses the XML data
format due to the complexity. It is mostly used for complex systems with strict
standards ensuring security and reliability.
Key Concepts
SOAP is a protocol as it has some strict rules for data format and
communication.
It manages the records and maintains the state between the requests.
SOAP relies on SSL and WS-Security for secured communication.
SOAP works with the XML data format to handle the complex data
SOAP REST
Also, the major difference is that SOAP cannot make use of REST whereas REST
can make use of SOAP.
Stateful vs Stateless API
In API design, a stateless API treats each request as independent, while
a stateful API maintains client state across multiple requests.
Stateless APIs are generally easier to scale and are common in microservices,
while stateful APIs are useful for applications requiring persistent sessions or
user context.
Here's a more detailed breakdown:
Stateless APIs:
Definition:
Each request is treated as a new, isolated transaction, without the server storing
any information about previous requests.
Scalability:
Stateless APIs are highly scalable because any server can handle any request, as
it doesn't rely on the state of previous requests.
Resource Utilization:
Stateless APIs generally require fewer resources as they don't need to store
session data.
Examples:
***Many REST APIs are designed to be stateless.
Advantages:
Scalability: Easier to scale horizontally by adding more servers.
Simplicity: Simpler to implement and maintain.
Fault Tolerance: If a server fails, the client can simply re-submit
the request to another server.
Disadvantages:
No Context: Cannot maintain user context or session data across
multiple requests.
Increased Load: Each request might require the client to re-transmit
all necessary information.
Stateful APIs:
Definition: The server maintains information about the client's state or
session between requests.
Scalability: Stateful APIs can be more difficult to scale because each
client is tied to a specific server that stores its session data.
Resource Utilization: Stateful APIs require more resources to store and
manage session data.
Examples: Web applications with login sessions or shopping carts.
Advantages:
o Context: Maintains user context and session data across multiple
requests.
o Personalized Experience: Can provide a more personalized user
experience.
Disadvantages:
o Scalability Challenges: Can be difficult to scale horizontally.
o Fault Tolerance: If a server fails, the client might lose their session
data.
Used when the client requests only part of a resource, and the server fulfils that partial
request.