API is an abbreviation for application programming interface, which is an abstract construct made available in various languages. Such interfaces enable software developers to write cleaner code and build complex functionality easily and quickly.
APIs come in many different forms and, when implementing one, developers can choose from a range of standards and protocols that the interface must implement and meet, depending on the API type and its purpose. In fact, you can design and implement APIs for pretty much anything in the context of programming — from interactions with an operating system to hardware systems or even database modifications and data retrieval.
In the next section we will discuss some of the most commonly used architectures and protocols used in the context of APIs that enable them to communicate and exchange information with other clients.
What Is an API?
Common API Architectures and Protocols
Application programming interfaces enable communication between an endpoint (i.e., a component of the API itself) and the consumer (e.g., an application). Therefore, you need to apply some pre-agreed principles to facilitate communication and the exchange of data in an intuitive and consistent way.
The sets of rules and constraints that drive the implementation, operation and behavior of interfaces are called protocols, each of which has its own characteristics and can serve different purposes. The three most commonly used protocols and architectures for designing and implementing APIs are REST, RPC and SOAP.
Common API Architectures and Protocols
- REST
- RPC
- SOAP
REST APIs
REpresentational State Transfer (REST) architecture was originally created by Roy Fielding in 2000. It consists of a set of constraints that RESTful APIs must meet in order to interact with RESTful services.
When the client side initiates a request via an API call, the RESTful endpoint returns a representational state of the resource and vice-versa. The term representation state is used to describe a collection of interconnected resources across the web (equivalent to a state machine), where a system or user can navigate by selecting links that, when clicked, will result in the next resource’s representation. For example, Python will transfer the client (for example, a web browser) to the resource representation of that specific article on the Built In platform.
This information can be represented in several different forms, such as JSON, XLT, Python or even plain text, and is delivered via HTTP. Despite this, JSON is the most popular one given that it’s human-readable and language-agnostic, which means any programming language can read or use it.
In order for an API to be RESTful, it must meet the following criteria:
Requirements for Restful API
- The interface implements a client-server architecture where the requests are managed via HTTP.
- The communication between the client and the server is stateless (i.e., every request made by the client is independent of one another).
- Resources should be cacheable whenever possible, and API responses must specify explicitly whether they can be cached. This requirement aims to improve the performance on the client side.
- The system follows a layered architecture that can possibly consist of different types of components (such as load balances, network security, et cetera). Requests can go through these different layers and this process should be invisible to the client.
RPC APIs
Remote Procedure Call (RCP) is a protocol used for the implementation of client-server based applications where the client program consumes resources or services provided by the server. For example, whenever you log in to your personal banking app, your browser acts as the client that initiates requests to the bank’s servers.
Depending on the encoding format, an RPC can be categorized either as JSON-RPC (encoding in JSON) or XML-RPC (encoding in XML).
An RPC endpoint accepts calls that may include numerous parameters and must return only one result. Unlike REST architecture, which mostly deals with resource representations, RPC protocol is commonly used for performing certain actions and executing blocks of code, such as procedures or commands, on a remote server.
SOAP APIs
Simple Object Access Protocol (SOAP) is a protocol that enables systems written in different languages and technologies to communicate effectively. Unlike REST, which is a set of architectural principles, SOAP imposes a set of rules that APIs must meet in order to be compliant with the specified protocol. This means a higher level of complexity that tends to make response times slower.
On the other hand, SOAP services tend to outperform alternatives when it comes to enterprise solutions and mission-critical systems, such as billing or navigation software systems. SOAP APIs are also ACID compliant and, therefore, offer reliability when executing transactions in databases. ACID compliance means that the transactions must meet certain requirements:
What Is Acid Compliance?
- Atomicity: Every statement in a transaction must correspond to a single unit to avoid corruption. Therefore, if multiple transactions are packed in a single statement, all of them must be executed or none at all such that the state of the database will only change if all statements are executed with no failures.
- Consistency: Rollbacks should be feasible in case of transaction failures such that all changes can occur in a pre-defined way based on the rules specified on the database such as constraints, triggers, cascades, et cetera.
- Isolation: Every transaction is independent of the others. This means that concurrent transactions don’t interfere with each other in case multiple users are interacting with the same resource.
- Durability: Successfully completed transactions are persisted, even upon a system failure, meaning that when committed, the transaction will be stored permanently on the data set.
SOAP APIs also offer the advantage of being able to operate over a wide range of different communication protocols, including SMTP, TCP and HTTP, which means they can be used to support different use cases. In contrast to REST, which can only transmit over HTTP, SOAP is independent of any specific transport layer. It can transmit over HTTP but also over SMTP that handles email communication asynchronously.
In contrast to other protocols and architectures, SOAP can also be used to support stateful operations. Modern web services take advantage of stateless architectures — such as REST — but there are still use cases that need to support stateful operations that are multiple chained operations. A few examples that would benefit from stateful operations are money transfer and online reservation systems.
Now You’re No Dummy When It Comes to APIs
If you’re just starting your career in technology, you need to make sure that you’re familiar with application programming interfaces, what their purpose is and why they’re necessary. Additionally, ensure that you have some exposure to the consumption and implementation of API endpoints. Finally, make sure you know what protocols and architectural patterns are available so that you are able to make a wise choice when creating one. Different use cases require different approaches.
Whatever engineering path you decide to follow, I can guarantee that APIs are something you will come across very frequently, so be sure to build strong foundations around the concepts we discussed earlier. And never forget that practice makes perfect!