API — An Introduction

Elijah Wines
5 min readOct 18, 2022

Whether you’re just beginning your coding journey, or you’ve already become an experienced Software Engineer, you’re guaranteed to come across an API. The fact is, they’re everywhere! From checking the weather, to looking up movie tickets at local theaters, almost every web application you encounter utilizes the awesome capabilities of an API. So.. what is it? And how do I get started?

Overview

In this article, we will discuss what an API is, its use-cases, benefits, and how to interact with one. I first came across an API during my first project at the coding bootcamp I attended. Trust me, this project was nothing fancy, but by utilizing the power of a free API I found online, it became something that could actually provide some useful information! So, lets get into it shall we!

What is an API?

API stands for Application Programming Interface. It contains data that you can showcase throughout your application. You can think of it as a mediator between the users of your application, and the resources and data your application provides. For example, you may know of a few websites that show you nearby restaurants and their reviews. The API is given your current location, then provides you with information on the restaurants closest to you, and whether or not they’re worth trying! Without the use of an API on a site like this, it’s pretty much pointless, but by taking advantage of the awesome power of an API, you can create something that people keep going back to on a day-to-day basis!

What is a REST API?

A REST API is architectural style to structure your API. REST stands for Representational State Transfer, and refers to the design of your API, and how developers can interact with it. To make an API RESTful, it must follow a few constraints:

  1. Uniform interface
  2. Client-server
  3. Stateless
  4. Cacheable
  5. Layered system
  6. Code on demand

You can read more into these constraints in this article here.

When Should I Use an API?

To answer this questions simply, you should use an API whenever you’re building an application that contains data that you intend to showcase throughout the program. API’s provide data, which means there are virtually an infinite number of ways this data can be used and made accessible to others. An API may be built for a companies infrastructure, or to showcase data they have gathered for others to use. For example, the Twitter API belongs to Twitter of course, but they allow developers to use the data that they have gathered to build other applications. There a many ways that this data can be delivered, but the most common, and most popular way to format your API is through JSON. Read more on JSON here.

What Are The Benefits of Using an API?

Although we have already discussed a few ways an API can improve, or full-on make your application, there are a couple benefits I would like to highlight.

Communication

They allow application and system components to easily communicate with one another, whether that be over internal networks, or over the internet.

Automation

An API can perform many tasks with very little human interference. Your data can be accessed, created, edited, and removed on an application that takes advantage of an API

Efficiency

If you are using a framework such as Ruby on Rails, building an API that connects to a database is quite easy. You don’t need to completely reinvent a way to make your data accessible for users, and there are tons of libraries and frameworks other than Rails that give you API building capabilities.

How Do I Interact With an API?

Considering JavaScript is one of the most popular front-end programming languages, I am going to show you how to use JavaScript to collect data from an API, which can be showcased in your application.

Example

let pokemon;fetch('https://pokeapi.co/api/v2/pokemon/charizard')
.then(resp => resp.json())
.then(data => {
pokemon = data;
console.log(pokemon);
});

In this example, I used the Pokemon API to collect data on Charizard using the built in ‘fetch’ method in Javascript. I sent an HTTP GET request to the url, and stored the data that was returned in the ‘pokemon’ variable (more on HTTP requests later).

Here is what was logged in the console:

As you can see, the variable ‘pokemon’ is an object containing tons of data on the Pokemon Charizard. Now that we have this data, we are free to use it throughout our application however we’d like!

HTTP Requests

Most public API’s will only allow you to send a GET request, as they do not want their data tampered with. However, if you are building an application that intends on manipulating an API that is connected to a database, you’ll want to allow users to manipulate certain data within the application.

Here are the 5 main HTTP requests

  1. GET — used to read/retrieve data from your API resource
  2. POST — used to send data to the server hosting your API. If successful, the submitted data will then be added to the API for later use
  3. PUT — used to edit existing data on the API resource
  4. PATCH — similar to PUT, except it only modifies part of the data. It only replaces the content you wanted to update
  5. DELETE — deletes specified data on your API

As previously mentioned, the example we used only sent a GET request to the Pokemon API. If you’d like to learn more about the other types of HTTP requests, check out this article here.

Conclusion

Overall, an API is incredibly convenient when building complex web applications, and are extremely useful when learning how to interact with data within an application. Like many others, I will continue to make use of API’s in my future projects, and I hope this article will be a helpful introduction and resource to others who are stepping into the world of Software Engineering. Happy Coding!

Sign up to discover human stories that deepen your understanding of the world.

Elijah Wines
Elijah Wines

Written by Elijah Wines

Software Engineer - JavaScript, Ruby, React, Ruby on Rails

Responses (1)

Write a response

What a solid comprehensive article! Keep it up Elijah!

--