What are APIs?

What are APIs?

The Internet (SERIES) - Blog #5

Introduction

APIs, Application Programming Interfaces, provide a standardized way for an application to allow itself to be integrated with other third-party apps without the need to code for every possible integration.

CRUD and HTTP Methods

So how do third-party applications use an API? That's where we must first learn about CRUD operations. CRUD stands for Create, Read, Update, and Delete. There are also 5 commonly used HTTP methods. Which are POST, GET, PUT, PATCH, and DELETE.

HTTP Methods and what CRUD operation they are (chart)

The POST Method creates something on your web application. The GET method reads information from the web application. The PUT and PATCH methods update information on the application. The DELETE method, as expected, deletes information on the web application.

Difference between PUT and PATCH

You may be reading this and wondering what the difference between PUT and PATCH is. Well, PUT and PATCH are almost identical, except that PUT is usually used for larger data updates, whereas PATCH is used for smaller data updates.

Let's say I have this piece of data:

{
    "first_name": "John",
    "last_name": "Smith",
    "age": 31,
    "city": "Chicago",
    "job": "Software Engineer",
}

I want to update the first_name, last_name, age, and city fields. I could use the PUT method. When using the PUT method, it requires you to state all of the data parameters (even ones you don't modify) because PUT updates the entire resource, meaning leaving out any field in a PUT request, will cause you to lose that entire field.

I want to update just the first_name field, I could use the PATCH method. With the PATCH method, I would only have to provide the parameters I'm changing. This is better for smaller updates as if you were updating one out of 20 fields, you wouldn't have to restate all 20 fields.

Real-World Example

Here's a relatable example. Let's imagine that you have worked hard on building a network of weather stations that report reliable and current weather statistics.

Then let's say a third-party mobile application wants to use your weather data. The third-party can integrate your API into their application. Here's a sample "conversation" between their application and your API.

Sample Mobile App and API conversation

So as you can see, the third party sent a GET request, which is requesting your weather data. In response, your API sent a JSON response. By the way, most APIs usually return in a standardized format, called JSON.

And a sample API response might look like this:

{
    "unit_system": "imperial",
    "temperature_unit": "Fahrenheit"
    "temperature": 65,
    "temperature_min": 55,
    "temperature_max": 70,
    "feels_like": 54,
    "humidity": 65,
    "precipitation": 87,
    "air_quality": "fair"
}

Now the third-party mobile app can use your weather services and display your data in a nice and pretty manner.

Conclusion

So a quick summary, APIs are used on top of an existing application to expose certain functionalities of the application for others to use.

Thank you for reading!

ย