- Node.js Command Line Interface (CLI) app that takes an API key user input and outputs nomics cryptocurrency API data. Tutorial code (see 'Inspiration' below) updated to use ES6 module imports etc.
- Note: to open web links in a new window use: ctrl+click on link
- Postman used to test API using a simple GET request
- node.js bin field used as executable file - maps command name to local file name. Important: file referenced in bin;
./bin/coindex.jsstarts with#!/usr/bin/env node, otherwise the scripts are started without the node executable! coindex.jsis the first file executed. This calls the files in thecommandsfolder. Thekey.jsfile calls thelibclassKeyManagerwhich includes key set, view and delete functions. It also calls theutilsconstisRequired. Thecheck.jsfile calls thelibclassesKeyManagerandCryptoAPIand uses axios to make a http call to the nomics cryptocurrency API.
- Node.js v16
- commander v9 node.js CLI solution
- Inquirer v8 CLI user interface, used to prompt user for API key in
key.jsfile - configstore v6 used in KeyManager class to load and persist the API key user input
- colors v1 to add colours to node.js (text colors: black, red, green, yellow, blue, magenta, cyan, white, grey. Other options available including bright colors etc.)
- nomics cryptocurrency API for JSON crypto coin data
- axios v0.26.0 promise-based http client.
async/awaitused instead of.then() - Postman API v8 to GET JSON data from API
- Install dependencies using
npm i - Create
.envfile - See
.env.examplethen add a default 32-character key from nomics cryptocurrency API - Type
npm linkto make all commands work from any CLI directory - Get yourself a 32-character key from nomics cryptocurrency API
- type
nodejs-api-datato get top level commands list;keyto manage API key,checkto see crypto currency prices andhelp - type
nodejs-api-data keyto get API key commands list;setto add 32-character key,showto see API key andremoveto delete the key - type
nodejs-api-data key -hto get full list of key help commands - type
nodejs-api-data key setto get add key prompt. Enter the key obtained from nomics cryptocurrency API - type
nodejs-api-data key showto display the API key - type
nodejs-api-data key deleteto delete the API key - type
nodejs-api-data check priceto see list of crypto currency prices. - type
nodejs-api-data check price --coin=BTCto see price and rank of BTC crypto (comma separated coin names (with NO spaces) used by API GET request - note: putting a coin name of ' ' (empty space) will return a list of ALL 2709 cryto coins!)
key.jsfunction to set API key from user input. Note: I replaced the isRequired utility to use my own key from the .env file as default if no other key is set. I also added a check of the input string to ensure it is 12 characters long and only contains letters or numbers. Keys are now 32 characters long.
async set() {
const keyManager = new KeyManager();
const input = await inquirer.prompt([
{
type: "input",
name: "key",
message: "Enter API key obtained from https://nomics.com or press ENTER to use default API key. ".green
},
]);
// If user enters an empty string my default API key from the .env file is used
if (input.key === "") {
const key = keyManager.setKey(process.env.API_key);
console.log("default API key used".blue);
return key;
}
// check if user inputs a key that is alphanumeric & 32 characters long
else if (input.key.length === 32 && input.key.match(alphaNumeric)) {
const key = keyManager.setKey(input.key);
console.log("API key set".blue);
return key;
}
// user to input key again if the input key was not an empty string or it was not 32 characters long
console.log("API key should be alphanumeric and 32 characters long. ".red + "Try again.".blue);
return;
},npm linkcommand used to make all commands work from any CLI directory
- Status: Working
- To-Do: Nothing
- N/A.
- Repo created by ABateman, email: gomezbateman@yahoo.com

