Skip to content
Rex St. John

Get in touch

Sports Salaries Using The Mashery USA Today API

This article is a continuation from my last article on the topic of using Intel® XDK IoT Edition with Mashery APIs to make REST API calls using Node.js.

Originally published on the Intel blog

Tags
  • intel
  • iot

Originally published at the Intel Developer Zone on October 26, 2014. Recovered from the Internet Archive.

This article is a continuation from my last article on the topic of using Intel® XDK IoT Edition with Mashery APIs to make REST API calls using Node.js.

Getting Started

Thisarticle assumes you have followed the instructions outlined in my last post to set up and configure a Node.js environment with Intel XDK IoT Edition. Before we can get started making calls to the USA Today Sports Salary API, we will need to register for a key from the USA Today developer portal. As always with the Mashery API Network, if you have a registered developer account you can login using those credentials otherwise you will need to click "Register" to create a new account.

Getting A Developer Key

To get a developer key to use the USA Today Sports Salary API, you will need to click "My Account" in the upper right hand corner of the USA Today developer portal and then -> "Applications" -> "Create A New Application" and complete the provided form. Once you have done this, you will receive a new developer API key for use with the application we will now build.

Creating Our Script

In our Intel XDK project we created in our last article, create a new script and name it "usatoday.js." Copy the following source code into that script:

// Fetch a sports salary with USA Today API
var request = require("request");
var moment = require("moment");

var url = "http://api.usatoday.com/open/salaries/mlb"
var api_key = "XXXXXX"
var params = {"players":"derek-jeter", "encoding":"json", "api_key":api_key };

request({
    url: url,
    json: true,
    qs: params
}, function (error, response, body) {
    if (!error && response.statusCode === 200) {
        var data = body["salaries"];
        console.log("Average Salary: " +data["average"]);
    }
});

Finally, copy your newly provisioned USA Today API key into the "api_key" variable defined above. Click the "Hammer" shaped icon to deploy the script to your Intel Edison and download the necessary Node.js packages.

As with the previous example, SSH into your Intel Edison over the local network, navigate to the folder containing your Node.js projects (located in the root directory) and run "node usatoday.js" to discover Marshawn Lynch's salary:

  • cd /
  • cd node_app_slot
  • node usatoday.js