DEV Community

Cover image for Random Bhagavad Gita Quotes API
Piyush Kumar Das
Piyush Kumar Das

Posted on

Random Bhagavad Gita Quotes API

In this world of technology, where innovation knows no bounds, developers are finding inspiration in unexpected places. One such fascinating project has emerged from the intersection of ancient wisdom and modern coding โ€“ the Random Bhagavad Gita Quotes API. In this blog post, we'll delve into the creation of this unique API, built using NodeJS and ExpressJS, and hosted on Vercel.

Tech Stack

NodeJS and ExpressJS

NodeJS, known for its speed and scalability, is the perfect choice for building server-side applications. ExpressJS, a minimalist web application framework for Node, developers can create robust APIs with ease.

Crafting the API

Before we dive into coding, make sure you have Node.js installed on your system. If not, you can download it from here.

Now, open your terminal and follow these steps:

1. Create a new project:

Run the following command to initialize a new Node.js project and create a package.json file:

npm init -y
Enter fullscreen mode Exit fullscreen mode

The -y flag auto-fills default values for your package.json file, making the setup quicker.

2. Install dependencies:

Install express โ€“ the web application framework for Node.js โ€“ by running the following command:

npm install express
Enter fullscreen mode Exit fullscreen mode

3. Create the entry point:

Create a new file, let's name it index.js, as the entry point for your application.

// index.js

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Welcome to the Bhagavad Gita API!');
});

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});
Enter fullscreen mode Exit fullscreen mode

4. Run the server:

Execute the following command in your terminal to start the server:

node index.js
Enter fullscreen mode Exit fullscreen mode

If you see this in your terminal:

Server is running on port 3000
Enter fullscreen mode Exit fullscreen mode

And this in localhost:3000 in your browser:

Example Output
Then very good, you have done a praiseworthy task till now.

5. Create a quote object:

For example:

const bhagavadGitaVerses = [
  {
    id: 1,
    text: "Your thoughts are arrows, Arjuna. Aim them towards the divine, and pierce the veil of illusion.",
  },
  {
    id: 2,
    text: "Doubts are clouds obscuring the sun of your inner wisdom. Seek clarity through focused meditation.",
  },
  {
    id: 3,
    text: "Action without attachment is the dance of the soul, a graceful offering to the cosmic stage.",
  }
];
Enter fullscreen mode Exit fullscreen mode

Similarly add more quotes to it you can use ChatGPT for that.

6. Create an endpoint

Before creating endpoint, I would like to tell you that, Endpoints act as gateways or entry points to access the functionalities provided by the API. Each endpoint represents a specific operation or resource that the API makes available.
Add this to your index.js:

app.get("/api/random-verse", (req, res) => {
  const randomVerse = getRandomVerse();
  res.json(randomVerse);
});
Enter fullscreen mode Exit fullscreen mode

But wait something is missing I guess. Correct! it's the getRandomVerse() function, so we will create that next.

7. Creating the function to generate Random Quotes:

function getRandomVerse() {
  const randomIndex = Math.floor(Math.random() * bhagavadGitaVerses.length);
  return bhagavadGitaVerses[randomIndex];
}
Enter fullscreen mode Exit fullscreen mode

Add this function to your index.js file.
Now your code might be looking something like this.

final code
Great Work!
Now head back to your browser and go to: http://localhost:3000/api/random-verse

JSON output
If you get this JSON as output on your browser.
Congratulations! You just created an API.

Top comments (0)