3

I am a noob at coding, so forgive me for this rather trivial question.

I am trying to make a program that can access the Stack Exchange API. Specifically I want to make a program that gets live question feed (preferably as a question object) from any Stack Exchange site of my choice.

I have scoured the Stack Exchange API page but I'm not able to understand how to exactly code it. I usually learn by examples. For example, I know that to access discord.js I write in my JS code as:

const Discord = require('discord.js');
const bot = new Discord.Client();

bot.on('message', (message) => {
    //code to deal with message
});

bot.login('BOT-TOKEN-HERE');

Is there any standard template such as the example I gave for API Stack Exchange? For certain, I'm sure we would have require('stackexchange-node'); (I believe stackexchange-node is the official SE npm package) and as well as a constructor.

2

There is an example of how to invoke the constructor here:

// app/src/resolvers.js
const stackexchange = require('stackexchange-node');
const config = require('../config/config');

const options = {version: 2.2};
const context = new stackexchange(options);

const API_KEY = config['API_KEY'];

const resolvers = {
    Query: {
        questions: async (root, {tagged, limit, sort, score}) => {
           var result;

           await _getQuestionsByFilter(tagged, limit, sort, score).then(function (data) {
               result = data.items;
           });

           return result;
        }
    },
};

async function _getQuestionsByFilter (tagged, limit, sort, score) {
    return await _getQuestionsStackExchangeApi(tagged, limit, sort, score);
}

function _getQuestionsStackExchangeApi(tagged, limit, sort, score) {
    let filter = {
        key: API_KEY,
        pagesize: limit,
        tagged: tagged,
        sort: sort,
        score: score,
        order: 'asc'
    };

    return new Promise(function (resolve, reject) {
        context.questions.questions(filter, function (err, results) {
            if (err) throw err;
            resolve(results);
        });
    });
}

module.exports = {resolvers};

As well as an example which uses TypeScript.

References

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .