Skip to content

📋 Node.js app to display data from a cryptocurrency API. Tutorial code updated to latest ES6

Notifications You must be signed in to change notification settings

AndrewJBateman/nodejs-api-data

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⚡ Node.js API Data

  • 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

GitHub repo size GitHub pull requests GitHub Repo stars GitHub last commit

📄 Table of contents

📚 General info

  • 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.js starts with #!/usr/bin/env node, otherwise the scripts are started without the node executable!
  • coindex.js is the first file executed. This calls the files in the commands folder. The key.js file calls the lib class KeyManager which includes key set, view and delete functions. It also calls the utils const isRequired. The check.js file calls the lib classes KeyManager and CryptoAPI and uses axios to make a http call to the nomics cryptocurrency API.

📷 Screenshots

postman screenshot nodejs screenshot

📶 Technologies

💾 Setup

  • Install dependencies using npm i
  • Create .env file
  • See .env.example then add a default 32-character key from nomics cryptocurrency API
  • Type npm link to make all commands work from any CLI directory
  • Get yourself a 32-character key from nomics cryptocurrency API
  • type nodejs-api-data to get top level commands list; key to manage API key, check to see crypto currency prices and help
  • type nodejs-api-data key to get API key commands list; set to add 32-character key, show to see API key and remove to delete the key
  • type nodejs-api-data key -h to 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=BTC to 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!)

💻 Code Examples

  • key.js function 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;
},

🆒 Features

  • npm link command used to make all commands work from any CLI directory

📋 Status & To-Do List

  • Status: Working
  • To-Do: Nothing

👏 Inspiration

📁 License

  • N/A.

✉️ Contact

Releases

No releases published

Packages

No packages published