Writing answering system using Node.js and YottaAnaswers API


We released our API for public use last year. In this tutorial, we will leverage this API to construct a basic responding application in Node.js, Javascript runtime environment.

YottaAnswers API

There are two versions of API, Public and Private. The Public version is free, and there is no need for registration to use this API. Downside of this version is that it has a rate-limit and that you will share the system with other Public users. If you want better performance, you should use the Private version, where you can register using email, where you will get your API key. It will give you 1000 requests per day, and 30000 requests overall.

Using Node.js for application development

Right now, Node.js is one of the most popular platforms for developing applications. One of the main reasons for that vast number of libraries and community support is enormous. We believe that our API would be a great tool for almost every developer.

Let’s start

The first thing to do is to initialize our Node.js project (which you can find on the link). After that create a main file app.js and we can start coding.

For this project we need request module (we know that it is deprecated, but for this project, it’s a simple solution, for anyone to follow), for calling the API.

const request = require('request');

After that we will create async function getYottaAnwsers that will be our body of the project. In function options for calling the API. For this most part the public path is used. The method is POST and it expects the question inside of the body.

  const options = {
    method: 'POST',
    url: 'https://api.yottaanswers.com/api/v1/publicQuestion',
    headers: {'Content-Type': 'application/json'},
    body: {question: input_question},
    json: true
  };

And then the API is called. And because calling request is async, Promise is returned. If some error was received rejection is returned, otherwise a list of answers is returned. The API returns a list of objects, which contains the fields: answer, link, sentence. Right now the answer is only needed, so response is mapped and returned.

  return new Promise((resolve, reject) => {
    request(options, (error, response, body_out) => {
      if (error) {
        reject(error);
      } else {
        resolve(body_out.map(x => x.answer));
      }
    });

At the end, inside main function, before created function getYottaAnwsers is called with given question. And with returned results, we will enumerate and print them.

    const question = "Who created Javascript?";
    console.log(`Question: ${question}`)
    try {
      var objectsList = await getYottaAnwsers(question);
      objectsList.forEach((x, i) => console.log(`${i}) ${x}`));
    } catch (error) {
      console.error(error);
    }

For example, question “Who created Javascript?” output looks like:

Question: Who created Javascript?
0) Programmers at Sun Microsystems
1) by Arthur Guiot as an alternative to jQuery
2) James Gosling , a Canadian computer scientist employed by Sun Microsystems (currently owned by Oracle) 
3) by scientist Tim Berners-Lee
4) the programmers of the CERN lab in Switzerland
5) The browser side portion of applications created with Morfik FX 
6) Jan Biniok
7) families with 365farmnet, to search in ihre whitelist zu setzen, west sacramento
8) Palestinian Arabs's collective identity 
9) Froala editor's core code 

Code for Private version

There is not much difference between codes for both versions of API. If you want to call the Private version, you need to add authorization to options and add privateQuestion to your path.

headers: {
      Authorization: 'Bearerey <YOUR API KEY>',
      'Content-Type': 'application/json'
    },

Full code

const request = require('request');

function getYottaAnwsers(input_question) {

  const options = {
    method: 'POST',
    url: 'https://api.yottaanswers.com/api/v1/publicQuestion',
    headers: {'Content-Type': 'application/json'},
    body: {question: input_question},
    json: true
  };

  return new Promise((resolve, reject) => {
    request(options, (error, response, body_out) => {
      if (error) {
        reject(error);
      } else {
        resolve(body_out.map(x => x.answer));
      }
    });
  });
}

async function main() {
    const question = "Who created Javascript?";
    console.log(`Question: ${question}`)
    try {
      var objectsList = await getYottaAnwsers(question);
      objectsList.forEach((x, i) => console.log(`${i}) ${x}`));
    } catch (error) {
      console.error(error);
    }
}

main();

Summary

In this post, we looked at how to create a simple answering system using YottaAnswers API. This API is a very simple and very powerful tool for every developer interested. We believe that there are endless possibilities with this API, but this should be a great start in your journey.

This is it for now. Thank you for reading. We hope this post was useful for giving you some idea of what you could do with API. Feel free to share your thoughts and ideas in the comments.


Leave a Reply

Required fields are marked *