Degree Days

Degree Days

Weather Data for Energy Saving

Access the Degree API using JavaScript in Node.js

This page has some sample code showing how to use JavaScript running in Node.js to specify a JSON request, send it to our API servers (past the security system), and process the JSON response that comes back.

It's not a full client library (like we have for Java, .NET, and Python), but, thanks to Node's excellent support for JSON, it should be pretty easy to adapt it to suit your needs.

See the JSON API docs for more about specifying the data you want in the JSON request. There are lots of options, and the request included in the JavaScript code below is just a simple example. The JSON docs also explain more about the data you can expect back in the response.

You might also find the JSON API test tool useful for testing different JSON requests and seeing the JSON responses that come back.

Also please note that we have separate code samples for calling the API with client-side JavaScript running in a web browser and with JavaScript running in an Office Add-In for Excel.

const crypto = require('crypto'), // built-in module, no need to npm install
  fetch = require('node-fetch'); // install with npm install node-fetch

// The test API access keys are described at
// They will let you access data for the Cape Cod area only.
// To fetch data for locations worldwide, sign up for a proper API account at
// and copy your API access keys here.
const accountKey = 'test-test-test';
const securityKey = 'test-test-test-test-test-test-test-test-test-test-test-test-test';
// You can call the API over HTTP using or
// over HTTPS using - set the endpoint URL
// below as appropriate.
const endpoint = '';

// ************* STEP 1: Create the request ************************************
// First we create a JSON request that specifies what we want from the API.
// See for more on this.

// You can fetch data from a station ID, a longitude/latitude position, or a 
// postal/zip code, as explained at
const location = {
  type: 'PostalCodeLocation',
  postalCode: '02532',
  countryCode: 'US'
// In this example we fetch both HDD and CDD, using the same breakdown (daily
// data covering the last 7 days) for both. For more breakdown options see
const breakdown = {
  type: 'DailyBreakdown',
  period: {
    type: 'LatestValuesPeriod',
    numberOfValues: 7
const locationDataRequest = {
  type: 'LocationDataRequest',
  location: location,
  dataSpecs: {
    // Here we specify 2 DataSpec items: one for HDD and one for CDD.  You
    // can specify up to 120 DataSpec items in one request (e.g. to fetch
    // data in lots of base temperatures).  With an API Standard+ account you
    // can have a DataSpec for hourly temperature data too.
    // Give each DataSpec a unique name so you can get the corresponding
    // DataSet from the response.
    myHDD: {
      type: 'DatedDataSpec',
      calculation: {
        type: 'HeatingDegreeDaysCalculation',
        baseTemperature: {
          unit: 'F',
          value: 60
      breakdown: breakdown
    myCDD: {
      type: 'DatedDataSpec',
      calculation: {
        type: 'CoolingDegreeDaysCalculation',
        baseTemperature: {
          unit: 'F',
          value: 70
      breakdown: breakdown
const fullRequest = {
  securityInfo: {
    endpoint: endpoint,
    accountKey: accountKey,
    timestamp: new Date().toISOString(),
    random: Buffer.from(crypto.randomBytes(12)).toString('hex')
  request: locationDataRequest
const fullRequestJson = JSON.stringify(fullRequest);
// Now our JSON request is ready.  Uncomment the line below to log the JSON:

// ************* STEP 2: Send the request to the API ***************************
// Next we sign the JSON request and package everything together into an HTTP
// request which we send to the Degree API.  This follows the spec at
const signatureBytes = 
  crypto.createHmac('sha256', securityKey).update(fullRequestJson).digest();
// The API requires the JSON request and the signature to be base64url encoded.
function base64urlEncode(unencoded) {
  return Buffer.from(unencoded).toString('base64')
    .replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
// Send the HTTP request to the API servers using node-fetch.  You could change
// this to use another HTTP library if you wanted.
fetch(endpoint, {
    method: 'POST',
    body: 'request_encoding=base64url' +
      '&signature_method=HmacSHA256' +
      '&signature_encoding=base64url' +
      '&encoded_request=' + base64urlEncode(fullRequestJson) +
      '&encoded_signature=' + base64urlEncode(signatureBytes),
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  .then(response => {
    if (!response.ok) {
      throw new Error(response.status + ': ' + response.statusText);
    return response.json();
  .catch(err => console.error('Problem connecting to API:', err)
  .then(parsedObject => handleResponse(parsedObject))
  .catch(err => console.error('Problem handling API response:', err));

// ************* STEP 3: Process the response from the API *********************
// The JSON response is explained at
function handleResponse(fullResponse) {
  // fullResponse.metadata has some rate limit info, but we are mainly
  // interested in fullResponse.response (a LocationDataResponse in this case).
  const response = fullResponse.response;
  if (response.type === 'Failure') {
    // See for more about failures.
    console.log('Request failure:', response);
  } else {
    // The response contains a lot of useful info, as shown in the JSON docs
    // at
    console.log('Station ID: ', response.stationId);
    // "myHDD" is the name we gave the HDD DataSpec in our request.
    const hddData = response.dataSets.myHDD;
    if (hddData.type === 'Failure') {
      console.log('Failure for HDD DataSet:', hddData);
    } else {
      for (var v of hddData.values) {
        console.log(v.d + ': ' + v.v);
    const cddData = response.dataSets.myCDD;
    if (hddData.type === 'Failure') {
      console.log('Failure for CDD DataSet:', cddData);
    } else {
      for (var v of cddData.values) {
        console.log(v.d + ': ' + v.v);


crypto is a built-in Node module so you shouldn't need to install anything for that.

node-fetch, however, is a dependency that you will need to install to use the sample code as is. You can do this by running npm install node-fetch in your Node.js project folder.

We looked at quite a few HTTP modules and decided to use node-fetch because:

If you're not already using node-fetch in your project, and you don't want an extra dependency, it should be easy enough to replace it with another library for the step-2 part of the example code above. Feel free to email us if you want to use the built-in http / https modules as we can give you an alternative (and considerably longer) code sample that uses those instead.

Further options and guidance

The sample code above will hopefully get you fetching degree days, but the JSON API docs also explain other options like fetching hourly temperature data and using the API for advanced regression.

You can quickly test out all sorts of JSON requests with the JSON API test tool, then write code like the example above for any that you want to use. The code in step 2 of the sample above will happily send any valid JSON request to the API and give you a response back that you can process.

It is also worth reading the higher-level integration guide for tips on the various approaches to integrating with the API. We have helped a lot of businesses integrate their software with our API so we are very familiar with the patterns that work well for common use cases. And please feel free to email us if you'd like more help.

Choose your API Plan and Start Today!

© 2008–2024 BizEE Software – About | Contact | Privacy | Free Website | API | Integration Guide | API FAQ | API Sign-Up