Creating my own Cocktail API — Node.js & Express.js

Alexander Rubino
3 min readOct 6, 2023

--

I have recently started my journey into the depths of backend development, therefore I started learning Node.js and Express.js.
Because of this, I wanted to share what I have learned so far.

I have created my first API, which includes some simple data about cocktails, including name, image, ingredients and difficulty.
It will not win any rewards, however I hope you can learn from my work.

Project setup

My project contain a couple of files:
You can find these files (not the .env file), in the git repository.

  • app.js (Main)
  • /routes/routes.js
  • /data/cocktailData.js
  • .env (To contain our auth token)

Listening to the server — App.js

Lets start out by setting up our App.js file.
Nothing fancy is happening on this one, we just simply create a server with the port 4000. Which means that on dev deployment, it will be launched to: http://localhost:4000

Note: If you can’t use import, it is because your package.json file is missing:
“type”: “module”

import express from "express";
import routes from "./routes/routes.js";

const app = express();
const PORT = 4000;

app.use(express.json());

app.use("/api/v1/", routes);

app.listen(PORT);

How the API works.

The way that the API works, is that you first enter the Bearer Token, which will grant you access to use the API.
Then you will need to add a body, which contains the ingredients that your desired cocktail contains.
Lets look at an example:

The body of the API:

{
"ingredients": [
"Brandy"
]
}

Which will find all of the cocktails that contains Brandy:

{
"filteredCocktails": [
{
"id": "6",
"title": "Sangria",
"difficulty": "Easy",
"image": "https://apipics.s3.amazonaws.com/coctails_api/6.jpg",
"ingredients": [
"Brandy",
"Red wine"
]
},
{
"id": "72",
"title": "Flaming rum punch",
"difficulty": "Easy",
"image": "https://apipics.s3.amazonaws.com/coctails_api/72.jpg",
"ingredients": [
"Dark rum",
"Brandy"
]
}
]
}

So with this in mind, lets take a look at how we make this happen.

Handle the GET request — Routes.js

When the .get function is called, we filter out our cocktailData. So that only the cocktails that fill the criteria gets returned.
After we have created our filteredCocktails, we can send this data back in a json format.

import express from "express";
import cocktailData from "../data/cocktailData.js";

const router = express.Router();

router.get("/", (req, res) => {
const filteredCocktails = cocktailData.filter((cocktail) => {
return findCommonElement(cocktail.ingredients, req.body.ingredients);
});

res.json({ filteredCocktails });
});

function findCommonElement(array1, array2) {
for (let i = 0; i < array1.length; i++) {
for (let j = 0; j < array2.length; j++) {
if (array1[i] === array2[j]) {
// Return if common element found
return true;
}
}
}
// Return if no common element exist
return false;
}

export default router;

However, currently anyone can access this API, which is not ideal. Therefore we have to implement Authorization.
We can do this by simply checking if the token from the request, is equal to your ACCESS_TOKEN.

Note: Create your own .env file, with a ACCESS_TOKEN const.

// .. //

import dotenv from "dotenv";
dotenv.config();

// .. //

router.get("/", authenticateToken, (req, res) => {
const filteredCocktails = cocktailData.filter((cocktail) => {
return findCommonElement(cocktail.ingredients, req.body.ingredients);
});

res.json({ filteredCocktails });
});

function authenticateToken(req, res, next) {
const authHeader = req.headers["authorization"];
const token = authHeader && authHeader.split(" ")[1];

if (token === process.env.ACCESS_TOKEN) {
next();
} else {
return res.sendStatus(401);
}
}

// .. //

Conclusion & what to do now?

In conclusion, this API can be expanded or set up with a database. Which would enable POST, PUT and DELETE functions as well, however this must be for another day.

If you want to publish your API, I will recommend using Cyclic.sh, and watching this video. It is what I used to publish mine for free.

Moving forward, I will use this API together with Next.js to create an application to simplify cocktails recipes.
I will post this project when finished.

Hope you enjoyed.

--

--

Alexander Rubino

Hi, I'm a Frontend Developer based in Denmark. You can learn more about me here: alexanderrubino.com