Skip to content

Instantly share code, notes, and snippets.

@mheadd
Created February 23, 2012 14:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mheadd/1893123 to your computer and use it in GitHub Desktop.
Save mheadd/1893123 to your computer and use it in GitHub Desktop.
Basic Node.js code sample for querying the Regulations.gov API
exports.credentials = {
api_key : 'YOUR_API_KEY_GOES_HERE'
};

~$ node path/to/regs.js

Search found: 15 documents.
EPA-HQ-OPP-2012-0030
EPA-HQ-OPP-2009-0822
EPA-HQ-OPP-2010-0499
EPA-HQ-SFUND-2007-0856
EPA-HQ-OPPT-2011-0182
EPA-HQ-OPP-2011-0641
EPA-HQ-RCRA-2001-0004
EPA-HQ-OPP-2011-0922
EPA-HQ-OPP-2008-0654
EPA-HQ-OPP-2009-0998
EPA-HQ-OPPT-2011-0180
EPA-HQ-OAR-2011-0089
EPA-HQ-OPPT-2011-0180
EPA-HQ-OPP-2009-0999
EPA-HQ-RCRA-2002-0033
// Include required modules and config.
var http = require('http');
var util = require('util');
var config = require('./config');
// Search options.
var dct = process.argv[2] || 'O';
var a = process.argv[3] || 'EPA';
var cp = process.argv[4] || 'O';
// Options for HTTP request.
var options = {
host: 'regulations.gov',
port: 80,
method: 'GET'
};
// Build path to API endpoint.
var path = '/api/documentsearch/v1.json?dct=' + dct
+ '&a=' + a
+ '&cp=' + cp
+ '&api_key=' + config.credentials.api_key;
options.path = path;
// Create HTTP client and make request.
var apiCall = http.request(options, function(response){
// Simple string to hold response from API.
var searchResult = '';
response.on('data', function(chunck){
searchResult += chunck;
});
response.on('end', function(){
var doc = JSON.parse(searchResult);
// Print out number of documens found.
var docs = doc.searchresult.documents.document;
// Write out Docket ID for each.
util.puts('Search found: ' + doc.searchresult.recordCount + ' documents.');
for(var i=0; i<docs.length; i++) {
util.puts(docs[i].docketId);
}
});
});
apiCall.end();
@mheadd
Copy link
Author

mheadd commented Feb 23, 2012

Document structure seems to vary based on search parameters. This may affect how docket IDs are written out.

@stucknat
Copy link

Mike

The resulting structure mostly remains the same. One small point, docket vs. document - dockets contain documents so you may want to get document id and not docket id. Some fields may show up empty when there are no values for them but the broad list of tags remains the same. We will publish the schema in the near future.

@mheadd
Copy link
Author

mheadd commented Feb 24, 2012

Cool - thanks for the info. Will keep modifying and enhancing this basic script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment