Accessing the Public Data API with Ruby / Ruby on Rails


Before Processing:

If you are using Ruby or Ruby On Rails to connect to the BLS Public Data API, you can use the rest-client ruby gem. All Ruby code should start with the following line of code:

require 'rest_client'

Calling the BLS Public Data API

Using the RestClient POST method, you can access the API using the following code. You must specify JSON in the content_type, since the Public Data API only accepts and returns JSON data.

require 'rest_client'
require 'json'
url = ''
response =,
                           {'seriesid => ['seriesid'],
                            'startyear' => '1995',
                            'endyear'   => '1998'
                           :content_type => 'application/json')

Post Processing:

Once the data has been retrieved from the Public Data API, you can use a JSON parser to further consume and inspect the response. We have provided examples of three JSON parsers for your convenience.

JSON Implementation for Ruby

You can install the Ruby gem library with the following command:

gem install json

Once you have installed the Ruby gem library, you can use JSON using the following command:

require 'json'

Once you have installed and required JSON, you can parse it using the following:

response =,....
parsed_json = JSON(response)

ActiveSupport's Built-in JSON Processor

response =,....
parsed_json = ActiveSupport::JSON.decode(response.body)

Ruby Response Built-in JSON Processor

// Get Response
response =,....)
// Call to_json on response client to get JSON response
my_json = response.to_json


Last Modified Date: December 19, 2013