A lightweight node.js wrapper for the U.S. AP Elections API 2.x.
Here's a minimal example, which fetches results for Presidential primaries held on April 26, 2016.
var ballotTally = require('ballot-tally');
var client = new ballotTally('v2', process.env.AP_API_KEY);
var query = new ballotTally.Query.Elections(client, '2016-04-26');
query.level('fipscode').officeID('P').raceType('D').raceType('R')
.fetch(function(err, data) {
if (!err) console.log(data);
});
Use this lightweight library to quickly create a script to fetch raw results data from the AP.
For heavy lifting, check out the database-ready library Elex, developed by our friends at The New York Times & NPR.
$ npm install ballot-tally
- Get an API key from the AP, then set it as the
AP_API_KEY
environment variable
$ export AP_API_KEY='your_key_here'
- Create a client object
var ballotTally = require('ballot-tally');
var apiKey = process.env.AP_API_KEY;
var client = new ballotTally('v2', apiKey);
- Create a query builder
var query = new ballotTally.Query.Elections(client, '2012-11-06');
- Build query to filter results based on criteria
query.statePostal('FL')
.test(false)
.level('fipscode')
.officeID('P')
.raceType('G');
- Fetch results
query.fetch(function(err, data) {
if (!err) {
console.log("%j", data);
}
else {
console.error(err);
}
});
Instead of using the query builder to fetch results, you can connect to the API with raw queries.
// This makes a query which is identical to the previous example.
var ballotTally = require('ballot-tally');
var apiKey = process.env.AP_API_KEY;
var client = new ballotTally('v2', apiKey);
client.elections('2012-11-06',
{
statepostal: 'FL',
test: false,
level: 'fipscode',
officeID: 'P',
raceTypeId: 'G'
},
function(err, data) {
if (!err) {
console.log("%j", data);
}
else {
console.error(err);
}
}
);
Query options match with the API filtering parameters. Please refer to API documentation for more information.
query.level('state').officeID('P').raceType('D').raceType('R')
.fetch(function(err, data) {
if (!err) console.log(data);
});
query.level('fipscode').officeID('P').raceType('D').raceType('R')
.fetch(function(err, data) {
if (!err) console.log(data);
});
query.level('ru').officeID('P').raceType('D').raceType('R')
.fetch(function(err, data) {
if (!err) console.log(data);
});
Delegate count is reported only at district
level for Presidential Primaries.
query.level('district').officeID('P').raceType('D').raceType('R')
.fetch(function(err, data) {
if (!err) console.log(data);
});
query.level('fipscode').officeID('P').raceType('G').national(true)
.fetch(function(err, data) {
if (!err) console.log(data);
});
query.level('state').officeID('P').raceType('G').national(true).winner('U')
.fetch(function(err, data) {
if (!err) console.log(data);
});
query.level('district').officeID('H').raceType('G').national(true)
.fetch(function(err, data) {
if (!err) console.log(data);
});
See the examples directory for further sample scripts.
This library was developed independently by The Wall Street Journal. It is not endorsed by Associated Press.
v1.0.0 (2016-04-26)
- Initial public release