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).
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.
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:
Your client kicks things off by initiating the API requests via the URI (**Uniform Resource Identifier**)
The API request is sent to the server, relaying your intentions.
The server processes your request, gathers the necessary information, and sends back a response.
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:
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.
Based on Architectural Style:
Some of the most used types of APIs are:
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.
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? which is a part of my newsletter called Hello, World! where we help developers learn a new software engineering concept every Monday. Read what engineers from Google, Microsoft, and Amazon are reading for free.
Top comments (0)