A Node.js CLI That Pulls Quotes From Fauna

Command line tools are everywhere — git, npm, yarn — and they remain one of the fastest ways to automate tasks. In this walkthrough, you'll build a simple quotes application as a command line tool using Node.js and Fauna. The final app will fetch random quotes from a third-party API and store metadata in a Fauna database, complete with a custom keyword and text coloring.

Prerequisites

Before you start, make sure your environment is ready:

  • Node.js version >= 16.x.x installed
  • npm or yarn available
  • Access to the Fauna dashboard

Set Up Fauna

Create an Account and Database

Register a new Fauna account with email or GitHub credentials. Once authenticated, the dashboard appears:

Click New Database on the dashboard:

Enter a database name and save. After the instance is created, you need an access key. Go to the Security tab in the side menu and click New Key to generate your credentials:

Create a Collection

From the side menu, click Collections, then press the New Collection button, enter a name, and save:

Create an Index

Fauna uses indexes to search documents by matching user input against fields. Navigate to the Indexes tab in the dashboard:

With the database, collection, and index in place, it's time to build the command line application.

Initialize the Node.js App

Project Setup and Dependencies

Create a folder for the application and set up npm:

mkdir quotes_cli
cd quotes_cli
touch quotes_app
npm init -y

Next, install axios to make HTTP requests to the quotes API:

npm i axios

Add chalk for colored terminal output:

npm i chalk

Then import dotenv for environment variable management:

npm i dotenv

Build the Quotes App

Write the Main Script

In the main application file, add the following code:

const axios = require('axios')
const chalk = require('chalk');
const dotenv = require('dotenv');
const url = process.env.APP_URL
axios({
  method: 'get',
  url: url,
  headers: { 'Accept': 'application/json' },
}).then(res => {
  const quote = res.data.contents.quotes[0].quote
  const author = res.data.contents.quotes[0].author
  const log = chalk.red(`${quote} - ${author}`) 
  console.log(log)
}).catch(err => {
  const log = chalk.red(err) 
  console.log(log)
})

The script imports axios, chalk, and dotenv. It references both the quotes server URL and your Fauna database URL, then uses axios to make a GET request with headers that request JSON responses.

JavaScript promises handle the response — logging the quote and its author to the console — with a catch method for error handling.

Run the Application

Before executing, update file permissions so it can run as an executable:

chmod +x quotes_app

Then run the app using its keyword:

./quotes_app

The output should look similar to this:

Next Steps

This basic setup demonstrates how Fauna pairs with Node.js command line tools. You can extend the application to add date-based reminders or other scheduled actions. For more advanced operations, refer to the Fauna CRUD documentation.