Degree Days

Degree Days

Weather Data for Energy Saving

Access the Degree API using Ruby

This page has some sample code showing how to use Ruby to specify an 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 Ruby'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 Ruby 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.

Many thanks to Thiago Nuic from Wattics who helped get us started with this Ruby/JSON sample by sending us a Ruby/XML sample that we published here for quite some time before we released the JSON API.

require 'time'
require 'securerandom'
require 'json'
require 'openssl'
require 'base64'
require 'net/http'

# 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.
account_key = 'test-test-test'
security_key = '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.
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
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:
breakdown = {
  'type' => 'DailyBreakdown',
  'period' => {
    'type' => 'LatestValuesPeriod',
    'numberOfValues' => 7
location_data_request = {
  '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
full_request = {
  'securityInfo' => {
    'endpoint' => endpoint,
    'accountKey' => account_key,
    'timestamp' =>,
    'random' => SecureRandom.uuid
  'request' => location_data_request
full_request_json = JSON.generate(full_request)
# Now our JSON request is ready.  Uncomment the line below to see the JSON:
#puts full_request_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
# (though you can just copy/paste).
signature_bytes = OpenSSL::HMAC.digest('sha256'), security_key, full_request_json)
# Prepare the HTTP request parameters to send to the API servers:
request_parameters = {
  'request_encoding' => 'base64url',
  'signature_method' => 'HmacSHA256',
  'signature_encoding' => 'base64url',
  'encoded_request' => Base64.urlsafe_encode64(full_request_json),
  'encoded_signature' => Base64.urlsafe_encode64(signature_bytes)
# Send the HTTP request to the API servers:
full_response_json = Net::HTTP.post_form(URI(endpoint), request_parameters).body

# ************* STEP 3: Process the response from the API *********************
# The JSON response is explained at

# Uncomment the line below to see the JSON response:
#puts full_response_json

# Use JSON.parse to turn the JSON response into an object:
full_response = JSON.parse(full_response_json, object_class: OpenStruct)
# full_response.metadata has some rate limit info, but we are mainly interested
# in full_response.response (a LocationDataResponse in this case).
response = full_response.response
if response.type == 'Failure'
  # See for more about failures.
  puts "Request failure: #{response}"
  # The response contains a lot of useful info.  See above to view the JSON, or
  # check the JSON docs at
  puts "Station ID: #{response.stationId}"
  # "myHDD" is the name we gave the HDD DataSpec in our request.
  hdd_data = response.dataSets.myHDD
  if hdd_data.type == 'Failure'
    puts "Failure for HDD DataSet: #{hdd_data}"
    puts 'HDD:'
    hdd_data.values.each { |v|
      puts "#{v.d}: #{v.v}"
  cdd_data = response.dataSets.myCDD
  if cdd_data.type == 'Failure'
    puts "Failure for CDD DataSet: #{cdd_data}"
    puts 'CDD'
    cdd_data.values.each { |v|
      puts "#{v.d}: #{v.v}"

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