OpenFec
Ruby client for the Federal Election Commission (FEC) API.
Part of the govapi-rb collection of Ruby clients for US government APIs.
Installation
gem 'open_fec'
Configuration
Get a free API key at api.data.gov/signup.
# Via environment variable (recommended)
ENV['OPEN_FEC_API_KEY'] = 'your-api-key'
# Or via configuration block
OpenFec.configure do |c|
c.api_key = 'your-api-key'
end
Usage
Candidates
# Search for candidates
results = OpenFec.candidates.search(name: 'Pelosi', state: 'CA')
results.each { |c| puts "#{c['name']} (#{c['party']})" }
# Find a specific candidate
candidate = OpenFec.candidates.find('H8CA05035')
candidate.results.first['name'] # => "PELOSI, NANCY"
# Get a candidate's committees
committees = OpenFec.candidates.committees('H8CA05035')
Campaign Finance Totals
# Financial totals for a candidate
totals = OpenFec.candidate_totals.list('H8CA05035')
totals.each do |t|
puts "Cycle #{t['cycle']}: raised #{t['receipts']}, spent #{t['disbursements']}"
end
# For a specific cycle
totals = OpenFec.candidate_totals.for_cycle('H8CA05035', 2024)
Contributions by Employer
# Top employer donors to a committee
employers = OpenFec.contribution_aggregates.by_employer(
committee_id: 'C00213512',
cycle: 2024,
sort: '-total',
per_page: 10
)
employers.each { |e| puts "#{e['employer']}: $#{e['total']}" }
PAC Contributions
# PAC contributions to a committee
pacs = OpenFec.contributions.from_pacs(committee_id: 'C00213512', cycle: 2024)
pacs.each { |c| puts "#{c['contributor_name']}: $#{c['contribution_receipt_amount']}" }
Pagination
Most endpoints use offset-based pagination:
OpenFec.candidates.each_page(state: 'VA') do |page|
page.results.each { |c| puts c['name'] }
end
Itemized transaction endpoints (Schedule A, Schedule B) use seek/cursor pagination:
OpenFec.contributions.each_page(committee_id: 'C00213512') do |page|
page.results.each { |c| puts c['contributor_name'] }
end
Rate Limits
DEMO_KEY: 30 requests/hour- Registered key: 1,000 requests/hour
- Contact
[email protected]for higher limits
License
MIT