EP 1: What is an API?
Tags: webdev, api, http, beginners
Published: 2024-03-02 10:09:24

## What’s an API?

In simple terms, API is just an output to an input that you gave to someone. They’re like the ordering system for your software. You give them instructions (inputs), and they reliably provide you with what you asked for (predictable outputs).


![Request Response in API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0e6sqcuh4bd3jacbqjtj.png)

Let’s consider the most widely used Google Maps, when we give the address as an input, it gives you back that address’s latitude/longitude coordinates as an output.

Let’s understand APIs better with a real-life analogy.

**Analogy: The Remote Control**

Think of an API as a remote control for your home entertainment system. When you want to change the channel on your TV, increase the volume, or play a DVD, you don’t need to understand the complex electronics inside your devices. You simply press buttons on the remote control, and your commands are executed.

In this analogy:

* The remote control serves as a client, providing a user-friendly interface.

* The various entertainment devices (TV, sound system, DVD player), this is where the logic is i.e. server.

* Your commands (changing channels, adjusting volume) represent API requests.

Just as the remote control abstracts the technical complexities of your entertainment system, APIs abstract the inner workings of software systems, allowing them to communicate and perform tasks with ease.

## The Power of APIs

APIs are the backbone of modern software development. They allow developers to leverage existing services and functionalities, saving time and resources. From the data you receive from a weather app to the seamless integration of social media platforms, APIs are everywhere to enhance our daily digital experiences.

In brief:

* APIs make cross-platform development a breeze.

* APIs are like standardized contracts for communication.

* APIs facilitate communication between services.

* APIs also play a crucial role in security.

**Payment Gateway APIs:** When you make an online purchase, the payment process is often facilitated by payment gateway APIs like PayPal, Stripe, or Square. They handle secure transactions, sparing developers from implementing complex payment systems.

**Social Media Sharing APIs:** Social media platforms offer APIs that allow users to share content from other apps directly to their social feeds. For instance, the “Share on Facebook” option in various apps is made possible through Facebook’s API.

## How does an API Work?

APIs function through a request and response process, sharing data among applications, systems, and devices. When a user interacts with an application, they initiate a request for data, which is sent to the API. The API retrieves the data and returns it to the user, completing the cycle.

To illustrate this process, let’s liken APIs to the workings of a restaurant. In this analogy, the user plays the role of a customer placing an order. The API functions as the restaurant’s order-taking service, receiving the order details, specifying the desired items, and arranging the preparation process. The kitchen staff, akin to an API server, ensures that the order is accurately prepared and ready for delivery. Any additional requests, such as specifying cooking preferences or adding extra toppings, are handled seamlessly. This comparison serves to simplify the concept of APIs by drawing parallels with a familiar scenario — the ordering process in a restaurant.


![Customer Waiter API example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3va1nipk57vkrjrhhwn.png)


For example, imagine a user interacting with a restaurant API to place an order for a pizza. The user specifies the type of pizza, size, toppings, and any special instructions through the API (**Input**). The API, acting as the order handler, communicates this information to the restaurant’s system (**Server**). The Chef then prepares the pizza according to the specifications. Once ready, the pizza is delivered to the user’s location (**Response**), ensuring a smooth and efficient process facilitated by the restaurant API.

Take a closer look, the API client assembles requests based on user actions and sends them to the relevant API endpoint. These endpoints are like URLs, providing access to specific resources in a database.

For instance, if a user wants to view products in an e-commerce store, the API client sends a GET request to the /products endpoint. This way, APIs enable efficient data exchange in the digital world.

## How API works between client and server

Imagine a client-server architecture where the client, representing the user, makes requests through a middleman — the API — and receives responses from the server in the backend. Here’s how it works:


![Client Server API Interaction](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xp0vtq3dzv778p33shbi.png)


 1. Your client kicks things off by initiating the API requests via the URI ([**Uniform Resource Identifier](https://flight.beehiiv.net/v2/clicks/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1cmwiOiJodHRwczovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9Vbmlmb3JtX1Jlc291cmNlX0lkZW50aWZpZXI_dXRtX3NvdXJjZT1ody5nbGljaC5jbyZ1dG1fbWVkaXVtPXJlZmVycmFsJnV0bV9jYW1wYWlnbj1lcC0xLXdoYXQtaXMtYW4tYXBpIiwicG9zdF9pZCI6ImYyODgzZDU3LTNmNTgtNGQwNS04ZjBmLWUxZGIwN2IxNjEyNCIsInB1YmxpY2F0aW9uX2lkIjoiODcxNTU2OGEtYWY2OC00NzE4LWFkZmMtMDE2ZGJmZmQyYjA5IiwidmlzaXRfdG9rZW4iOiIyYzI3ZTIxNi02ZmI0LTRmZWYtOGFmYy01ZjJmNzIwMzc1ODAiLCJpYXQiOjE3MDkyODE5MDYsImlzcyI6Im9yY2hpZCJ9.b6LB6ERswWLp66lEbHxk0JbZhYAc9l9jd6f6WcZMiss)**)

 2. The API request is sent to the server, relaying your intentions.

 3. The server processes your request, gathers the necessary information, and sends back a response.

 4. The API, acting as the go-between or the communication channel, hands you the response containing the data you need.

APIs are designed to be secure, using authorization credentials and an API gateway to control access and keep things safe. For extra layers of security, various techniques like HTTP headers, query string parameters, and cookies are employed.

## Types of APIs

APIs are categorized into many different types based on the use case. In practical experience, I’ve come across people using the term “API” in three different contexts. These are:

**Based on Accessibility:**


![Types of API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f0d9ljqtlkvj6rtbluyu.png)


**Private APIs:** These are also known as internal APIs. When companies create their applications, they build them using a set of interacting APIs. They’re used within an organization to connect various software components. Private APIs are not available for third-party use.

For example, an e-commerce platform might have numerous private APIs for order processing, product catalog, recommendation, payment processing, etc. working altogether coordinating with each other.

>  💭 **Even database calls are API calls under the hood.** The queries that we do are sent to the database through an API call. The database then proceeds to run that query and return the data as a response.

**Public APIs:** Public APIs provide access to an organization’s data or services for third-party developers. These APIs can be freely available or offered as paid products. Sometimes, companies make some of their APIs available to the public and provide instructions on how to use them.

A good example is the YouTube Data API, YouTube provides a public API that allows developers to access and interact with YouTube’s vast video content, including searching for videos, retrieving video details, uploading videos, and managing user playlists.

Developers can use this API to create applications that integrate YouTube video features, such as video search, recommendations, and video uploading, into their own platforms. This public API has enabled the development of various third-party YouTube applications and services.

**Code Interfaces:** Now, the first two types of APIs are quite practical, but developers also use the term “API” for lower-level stuff in code, like functions.

A code interface API, or simply a code API, refers to a set of functions, methods, or procedures that a programming language or library provides for developers to interact with and use in their code.

A classic example is the Math.random() function in JavaScript. It's an API that generates random numbers. There are other mathematical operations like Math.power() (power), Math.round() (round), and Math.sqrt() (Square root).

>  💡 There might be different types of API, but one thing is in common, they give an output based on an input. They might be named Public, Private, or Code Interfaces but still use API as a single term to describe them.


![Internal, Public and code interfaces API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oru0xnn0sdgr7i99bf59.png)


**Based on Architectural Style:**

Some of the most used types of APIs are:

 1. **REST:** Representational State Transfer (REST) is the most common architectural style for Internet data transfer. It uses standard HTTP methods like GET, POST, PUT, and DELETE to interact with resources through endpoints.

 2. **SOAP:** Simple Object Access Protocol (SOAP) uses XML to exchange structured messages between a client and server. It’s often used in enterprise environments and offers advanced security features but can be slower than other architectures.

## How developer talks about APIs


> “I’ve developed an API that allows you to discover the top 10 trending videos on YouTube tailored to your preferences, delivering personalized content effortlessly.”

Less manual work and faster access to the desired search result.

> “I keep encountering this authentication issue with the API; it’s like trying to enter a password that I can’t quite get right.”

The API is throwing this puzzling auth error at me, like a locked door that requires the right key. I’m struggling to get through with the correct credentials.

> “You can use the Facebook API to get that data into your app”

Connect your app with the Facebook API, and you can pull in data seamlessly. It’s like having a magic portal to grab the information you need for your application.

> “I crafted an API that acts like a personal shopper, finding you the best book recommendations based on your preferences. It’s your literary guide in the vast bookstore of information.”

The future is here! Getting things done within moments in a few touches.

By now, you must have had a clear idea of **APIs,** from its working to testing you know all of it now. However, understanding APIs on a theoretical level is just the first step. To know the practical aspects of working with APIs, you can try working on them by implementing some of the concepts in projects.

>  The post was originally published at [What is an API?](https://hw.glich.co/p/what-is-an-api) which is a part of my newsletter called [Hello, World!](https://hw.glich.co/subscribe?utm_source=medium&utm_medium=bottom&utm_campaign=what-is-an-api) where we help developers learn a new software engineering concept every Monday. Read what engineers from Google, Microsoft, and Amazon are reading for free.