Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

martin cartledge
martin cartledge
1,933 Points

Where do I place my mashape API key in Node application...?

I am using the Netflix Roulette API found on Mashape in my Node application, but I'm stumped as to where I need to place my API key in the request. Any ideas?

var EventEmitter = require("events").EventEmitter;
var https = require("https");
var http = require("http");
var util = require("util");

/**
* An EventEmitter to get actor's Netflix info
* @param actor
* @constructor
*/
function Actor(actor) {

  EventEmitter.call(this);

  actorEmitter = this;

//  setRequestHeader("X-Mashape-Authorization", "API KEY HERE");

  var request = https.get("https://community-netflix-roulette.p.mashape.com/api.php?actor=" + actor.replace(/\s/ig, "%20"), function(response) {
    var body = "";

    if (response.statusCode !== 200) {
      request.abort();
      actorEmitter.emit("error", new Error("There was an error getting the actor/actress' info for " + actor + ". (" + http.STATUS_CODES[response.statusCode] + ")"));
    }

    response.on('data', function(chunk) {
      body += chunk;
      actorEmitter.emit("data", chunk);
    });

    response.on("end", function() {
      if (response.statusCode === 200) {
        try {
          var actor = JSON.parse(body);
          actorEmitter.emit("end", actor);
        } catch (error) {
          actorEmitter.emit("error", error);
        }
      }
    }).on("error", function(error) {
      actorEmitter.emit("error", error);
    });
  });
util.inherits(Actor, EventEmitter);
module.exports = Actor;

1 Answer

Kevin Korte
Kevin Korte
28,149 Points

Either in some sort of settings json file, or in an environment variable, either should work fine.