E-CommerceCompetitive PricingMarketing Ready
Dotcreo Digital logo

The Basics: What Is An API?

APIs
12 mins read
19th Feb 2419th Feb 24

Introduction

This seems to be a topic that is very misunderstood by non-developers. Even some frontend developers struggle to wrap their heads around it.

Why? Well, my guess is that from my experience, the general explanation of an API(Application Programming Interface) you will find on the internet is rather complicated to understand and somewhat vague. I'll try my best in this article to break it down enough so even non-developers can grasp it.

The general explanation you see online of an API is elaborated as:

"An API is like a waiter in a restaurant, a middleman that helps two different entities communicate with each other and exchange information"

Whilst this is essentially true, I don’t find it the best way to elaborate the definition. The way I like to explain an API is just by saying what it is - a function.

What Is A Function In Programming?

At a macro level, a function in programming is a self-contained block of code designed to perform a specific task. Functions usually accept input parameters, commonly referred to as arguments, and produce an output value or result.

Let's take a look at a basic function, not related to an API:

function addNumbers(x, y) {
  const newNumber = x + y
  return newNumber
}

A Basic API Example

Now, let's look at a really basic API that returns all members of a team:

const TeamMember = require('../models/teamMember')

async function findAllTeamMembers(request, response) {
  const requestTeamId = request.body.teamId

  const teamMembers = await TeamMember.findAll({
    where: { teamId: requestTeamId }
  })

  return response.status(200).json(teamMembers)
}

module.exports = {
  findAllTeamMembers
}

Don't let this scare you, it's pretty simple too. I'll break it down:

  1. We import the TeamMember model at the top. For all non-developers just think of this as a gateway to the database where all the team members are for every single team.
  2. The function is marked with async because it needs to be asynchronous in order to work nicely with await in our database query with await TeamMember.findAll({ where: { teamId: teamId }}).
  3. The function takes two input parameters, request and response. It obtains the teamId from request.body and saves it in a variable called requestTeamId.
  4. The function then calls TeamMember.findAll, which is a method that queries the database for all team members where the teamId matches requestTeamId.
  5. The await keyword is used to ensure that JavaScript waits for the database query to complete before moving on to the next line of code.
  6. Once the database query is complete, the result (an array of team members) is stored in the teamMembers variable.
  7. The function then sends a response with a status code of 200 and the array of team members in JSON format.
  8. Finally, the function findAllTeamMembers is exported so it can be used in other parts of the application.

Obviously different APIs vary in complexity and size, but they all do the same thing on a macro level: receive information, perform operations, and send a response. In that order.

Samuel King headshot
Author: Samuel King
Full Stack DeveloperDesignerMarketer
Book A Free Call →