Search code examples
apiticker

What am I missing with this API connection request string?


I have my api_key in a ".env". I could be overthinking it. Here's my code as it currently stands, works without hiding the api_key. My syntax is incorrect when I pull from process.env.API_KEY.

function call_api(finishedAPI, ticker) {
    request('https://cloud.iexapis.com/stable/stock/' + ticker + '/quote?token= + 'api_key', { json: true }, (err, res, body) => {
        if (err) {return console.log(err);}
        if (res.statusCode === 200){
           finishedAPI(body);
        };
    });
};

I know there's a simpler way to do this and my research to google cues aren't firing on all cylinders. I'll buy you coffee or a tomato basil panini from Starbucks :-)


Solution

  • function call_api(finishedAPI, ticker) { request('https://cloud.iexapis.com/stable/stock/' + ticker + '/quote?token=' + API_KEY, { json: true }, (err, res, body) => { if (err) {return console.log(err);} if (res.statusCode === 200){ finishedAPI(body); }; }); };