Share


What The Hell Is An API?

by Clarissa Ng on 2020-06-06

So, what the hell is an API?

An Application Programming Interface (API) in general stands for any computing interface which defines interactions between multiple (often disparate) chunks of software.

These chunks of software could be a library, such as Pandas in Python, and your Python script. Or your front-end Javascript website communicating with your PHP backend.


apiexample
Example of a generic API

Okay, but… how the heck does it help me?

APIs are a great way to save your time! Instead of putting in hours of work writing code to do something that’s already been done, you can use an API that bridges to the code you need.

To better understand this, let’s think of a translator: instead of learning a new language, you can use a translator to talk to someone speaking that language.

APIs let us communicate across different applications without wasting any of our precious time!

Sounds interesting, so what in the world are these REST APIs I keep hearing of?

REpresentational State Transfer (REST) APIs are the most popular type of APIs. REST APIs allow us to bridge together our code using a defined convention that’s easy to use over the internet aka HTTP(s).

REST APIs are extremely popular these days. If you ever hear the term API being used, you can confidently assume that it is a REST API.

Due to this convention, we’re able to easily communicate between two different applications using HTTP(s), such as an Excel spreadsheet in the cloud and your Python code.


Sweet, but how can I use these REST APIs?

REST APIs can be used by making a request to a URL and receiving a response.

The URL gets accessed using the same process as if you went to your browser and typed in www.apispreadsheets.com or www.medium.com except it is called through your code.

apiworks

Let’s take a deeper look at URLs, requests and responses using our product APISpreadsheets, API and Python as examples.



Are we starting with Requests?

Yup! Because to use an API you have to make a request. The request is made from the client, which could be your Python code, your browser or another application running on your computer such as Postman.

There are many request types but there are only two super common ones that you need to be aware of:

  1. GET is used to request data from a specified resource using a URL
  2. POST is used to send data to a server to create/update a resource using aURL


Okay, how do I make these Requests then?

We will go through the full code steps for Python but just for reference this is what the final code will look like:



Step 1: Request Library

In Python, you can make requests by first importing the requests library at the beginning of your code

import requests


Be sure that the request library is installed already. If not you can $ pip install requests

Step 2: Create your API URL

You need an URL to make requests to. To make requests to your own spreadsheet using API Spreadsheets, follow our tutorial to get your URL

For now, you can use this URL. It accesses a COVID-19 case data spreadsheet from New Jersey from before June 3rd 2020.

https://api.apispreadsheets.com/data/581/



Step 3: Perform your request

Using your URL that you just created, you can now perform a GET request on your spreadsheet and store its response in a variable r.

Remember, a GET request is used to get data from a specified resource



Step 4: Verify everything went smoothly

No matter what happens you always get a response back from a request.

Before we actually retrieve data from the response, let’s first test to make sure the request went through properly. Add in a print line to your code:

print(r)

At this point, you want to run the program and get an output saying “Response [200]”. The 200 is the response code and indicates that your API request worked.


Ooo cool! So what happens if the request didn’t work?

Well, I'm glad you asked. If the request didn’t work you get any other response code besides 200.

In fact, these response codes being standardized is what makes using REST APIs easy.

There are a bunch of response codes but it’s up to a particular API’s designers and developers to return the appropriate ones.

At API Spreadsheets, for a GET request we return any of the following codes

  1. 200 — OK, everything went smoothly
  2. 401 — Unauthorized, don’t have permission to access a file
  3. 404 — Not Found
  4. 500 — Server Error, something wrong happened on the server



That seems helpful. What happened to the data in the response?

Don’t worry. The data is also in the response and can be accessed through the response’s JSON parameter/method.

data = r.json()



Wait, what is JSON?

Javascript Object Notation (JSON) is a data format that is widely used to exchange data between applications due to its fairly straightforward format.

You can see what the data JSON looks like by opening the URL, https://api.apispreadsheets.com/data/581/ in your browser 😀

JSON Example from the Data


Keep in mind that a response doesn’t have to contain JSON data. It’s again up to the designers and developers of an API. As is the format of the JSON itself.

We at API Spreadsheets, only return JSON data with a GET request but a POST request doesn’t have JSON data returned.

And you can see the format of the JSON data in our documentation. Most APIs will have good documentation detailing the format of their JSON and their response codes.


Oh crap, does this mean executing a POST request is different too?

Yes, but not too much. You are still making the request but now you will also need to include a JSON object that contains your data.

Remember, a POST request is used to send data to a server to create/update a resource.

Step 1: Request Library

In Python, you can make requests by first importing the requests library at the beginning of your code

import requests


Step 2: Create your API URL

To make requests to your own spreadsheet using API Spreadsheets, follow our tutorial to get your URL.

For now, you can use this URL. It accesses a COVID-19 case data spreadsheet from New Jersey from before June 3rd 2020.

https://api.apispreadsheets.com/data/581/


Step 3: Perform your POST request with your JSON data

First, we need to prepare the data we want to add.

I know you are probably getting tired of hearing this, but the format of this data is specified by the developer and designer of the API.

For API Spreadsheets, we can add data to the spreadsheet in this format


We can perform the POST request like this


FYI — You might hear this JSON data be referred to as the Body


Step 4: Verify Everything went smoothly

You can print the response or check if everything went well. You should get a 201 code, different than the 200 from GET. 201 stands for created whereas 200 is OK.


Wrapping Everything Up


Congratulations! You have made it to the end. Now you should know what an API is, how it can help you and how to make simple requests in Python!


If you have any further questions, feel free to email us at info@lovespreadsheets.com! We can’t wait to see what you build.

Checkout our other tutorials!

Let us know what you think