Understanding APIs: What They Are and How They Work

Understanding APIs: What They Are and How They Work

Know about the protocol & format on which the APIs work

How does the computer communicate?

You are right if you think they only talk in 1s and 0s. But...

That’d be like saying humans communicate by making noises and speaking in different languages of their own choice. In all of this, you would ignore the fact that some noises are in English, while others are in Hindi, some in Arabic, and so on. Point is, even computer programs can use different languages for communication.

Here comes the bridge that connects different computers or programs, which we call API.

So, what is an API?

An Application Programming Interface or API connects an application to a server. API is meant to bridge the gap between various programs, ensuring that data crosses the language barrier. They facilitate communication over the web, so they’re also called web services. Using a set of functions, apps can access data and interact with external software components, operating systems, or microservices. The API sends your request to the system and receives its response afterward.

Why are APIs powerful development tools?

Because they speed up the creation of different applications and software and can be integrated very quickly into your project. For example, if you are building your software and need to add extra security for your users, integrating a face recognition feature on your own would be very time-consuming.

How can APIs act as a bridge between so many different software products and not become complex?

Because it uses strict protocols that dictate how programs interact with the API. Efficient data transfer requires rigid regulations. The more standardization there is in requests and responses, the less likely something will get wrong.

We will deep dive into these protocols in future blogs. Right now, you understand that these protocols dictate how the sender formats their message and how the receiver interprets that message. As a Web Service, they probably use HTTP or Hypertext Transfer Protocol.

HTTP is a request-response protocol, meaning that any interaction between two parties is composed of a request and a response. The client issues the request to the server, and then the server sends a response to the client, even if it can’t fulfill the request.

We will discuss the Architecture of HTTP Request-Response in detail in our future blogs.

Now, let's see exactly what kind of format we are talking about...

For humans, a person who facilitates communication, such as a translator, understands several languages or "formats" and sends information in the format preferred by the listener. Likewise, computer programs have format preferences, but their solution is to choose a few designated formats to use for all communications.

For APIs, the most widely used and well-known data formats are JSON and XML.

Understanding JSON and XML

JSON (JavaScript Object Notation) and XML (Extensible Markup Language) are two commonly used data formats responsible for exchanging data in HTTP API requests and responses. They both have specific syntax and rules for formatting data.

JSON

JSON is a lightweight data-interchange format that is easy for humans to read and understand. It is based on a subset of the JavaScript programming language, and it's designed to be easily parsed by machines. JSON supports basic data types such as numbers, booleans, strings, and null, and more complex structures such as arrays and objects.

A JSON file consists of many components:

  1. Array([ ]) - In a JSON file, the Square bracket ([) represents a JSON array.

  2. Object({ }) - In a JSON file, the Curly brackets ({) represent a JSON object.

  3. Key - A JSON object contains a key that is just a string. Pairs of key values make up JSON objects.

  4. Value - Each key has a value that could be a string, integer or double and etc...

Here's an example of JSON data for a book object:

{
  "title": "The Hitchhiker's Guide to the Galaxy",
  "author": "Douglas Adams",
  "genre": "Science Fiction",
  "price": [ 
          { 
                "Hard_copy" : 12.99
          },
          {
                "soft_copy" : 5.99
          }
      ]        
}

XML

XML is a markup language used to encode documents in a human-readable and machine-readable format. XML documents consist of tags, similar to HTML tags, and attributes, which provide additional information about the data enclosed in the tag. XML documents can be validated using a Document Type Definition (DTD) or an XML Schema.

Here's an example of an XML document for a book object:

<book>
  <title>The Hitchhiker's Guide to the Galaxy</title>
  <author>Douglas Adams</author>
  <genre>Science Fiction</genre>
  <price>12.99</price>
</book>

Outro

In summary, HTTP-implementing APIs with JSON and XML provide an easy way for software applications to communicate with each other and exchange data. JSON provides a lightweight and easy-to-read way of formatting data, whereas XML provides more advanced features such as validation and data type enforcement. Creating an HTTP API with JSON or XML involves defining the endpoints, HTTP methods, and request and response formats. By using these principles, developers can create rich, interoperable APIs that allow different systems to work together seamlessly.

Okay, let's end this here...

Hope you guys have understood exactly what is an API and how it helps in the communication of different programs. And thanks for making it to the end!!

We will be continuing our discussion on APIs, their different types, stateless & non-stateless feature, more about HTTP, and other protocols too.