What is an API and How do I use it?

APIs are one of the biggest buzzwords around the web in 2021. In September, the founder of RapidAPI made a guest post on Forbes about Why Almost Every Company is an API Company now. According to Allied Market Research, the Cloud API space is expected to reach a market cap of almost $1.8 BILLION by 2026. It’s expected to grow at a rate of over 20% year over year. APIs have been around for a while, but it has only been recently that they’ve grown so quickly. I would bet they’ll grow even faster and hit a higher market cap by 2026. Now that we’ve discussed what’s going on with APIs in the market, let’s take a look at what an API is. So many companies are making APIs now that even the New York Times has an API.

A Simple Introduction to APIs

API stands for Application Programming Interface. In plain English, it is a contract between the application and the user. It says “you send me data in the form of x, and I’ll send you data in the form of y”. Let’s break this down further by examining what each of these words mean.

Application

We’ve all used applications. Applications have become so ubiquitous in our lives that we’ve shortened their name to “apps” (not to be confused with appetizers). You’re reading this from an app, that’s right, your web browser is an app. Applications are simply programs designed to fulfill a particular purpose. 

How did apps become so popular? Other than the fact that almost everything we use requires applications to be useful, I’d bet it was the iPhone. Remember when Apple came out with the iPhone and they had all those commercials like “there’s an app for that”? That flooded their app store with apps. Applications were the name of the game in the 2010s. Now we’ve stepped that up. In 2021, applications are no longer enough. APIs provide a way to connect multiple applications together, that’s part of why they’re so powerful.

Programming

Programming. This one is pretty straightforward. Programming is the act of creating a program. Since apps are just programs with a particular purpose (are there programs that don’t have a purpose?) programming can be linked to the production of applications. As I said earlier, APIs provide a way to connect multiple applications together. Since APIs are contracts that define the data being passed in and returned, programs can parse this.

The beauty of having an Application Programming Interface is that the data format is all predefined. Predefined data formats equal being programmatically accessible. This means that we have a way to programmatically (think automatically) send data to an API endpoint and get a data response back. This allows us to connect applications and create more complex and useful apps.

Interface

If you’ve been programming for a while you’re probably familiar with interfaces. In Java, interfaces are contracts that define which parameters need to be passed and what will be returned. As we’ve already said, APIs are just contracts for data processing. 

Putting it All Together

Now that we’ve covered what each of the words mean separately and how they play into each other let’s put it all together. Application Programming Interfaces are a tool to help programs connect applications by providing contracts detailing the interaction.

How do I use an API?

Part of why APIs are gaining traction so quickly is because they’re easy to use. Web APIs are the next wave of “apps” and for good reason. All you have to do to use a (good) web API is sign up for an API key and send off HTTP requests! We’ve already covered How to Send a Web API Request in Python. In the future we’ll also be covering how to send web API requests in other languages such as Go and JavaScript.

One of the beautiful things about web APIs is that they let us do complex things like summarize text with just an HTTP request. Let’s do an example call in Python to summarize some text. To set up our code we’ll import the requests and json libraries to send requests and parse JSON format.

First we’ll set up the right headers and URL endpoint to hit. Then we’ll set up our body, fire off a POST request, and parse the response. For you to reproduce this code, simply sign up at The Text API for your free API key and use that instead of the literal apikey shown.

import requests
import json
 
headers = {
    "Content-Type": "application/json",
    "apikey": apikey
}
text_url = "https://app.thetextapi.com/text/"
summarize_url = text_url + "summarize"
 
body = {
    "text": "This is an example. There are four sentences. This example will return one. This is an example."
}
response = requests.post(url=summarize_url, headers=headers, json=body)
summary = json.loads(response.text)["summary"]

When you get it back you should get back the sentence “This is an example.”

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Yujian Tang

6 thoughts on “What is an API and How do I use it?

Leave a Reply

Discover more from PythonAlgos

Subscribe now to keep reading and get access to the full archive.

Continue reading