npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ballotpedia-sdk

v1.0.0

Published

A TypeScript SDK for interacting with the Ballotpedia API v4

Downloads

4

Readme

Ballotpedia SDK

A TypeScript SDK for interacting with the Ballotpedia API v4. This SDK provides a simple interface to access election data, districts, officeholders, and more from Ballotpedia.

This SDK is simply a wrapper around the Ballotpedia API.

Installation

pnpm install ballotpedia-sdk

Usage

import BallotpediaService from 'ballotpedia-sdk';

const ballotpedia = new BallotpediaService({
  apiKey: 'your-api-key-here'
});

Getting Districts

Fetch voting districts for a specific location:

try {
  const districts = await ballotpedia.getDistricts(37.7749, -122.4194);
  console.log(districts.data);
} catch (error) {
  console.error('Error fetching districts:', error);
}

Getting Officeholders

Fetch current officeholders for a location:

try {
  const officeholders = await ballotpedia.getOfficeholders(37.7749, -122.4194, {
    collections: ['Federal', 'State']
  });
  console.log(officeholders.data);
} catch (error) {
  console.error('Error fetching officeholders:', error);
}

Getting Election Dates

Fetch election dates by location:

try {
  const dates = await ballotpedia.getElectionDatesByPoint(37.7749, -122.4194);
  console.log(dates.data);
} catch (error) {
  console.error('Error fetching election dates:', error);
}

Or fetch election dates by parameters:

try {
  const dates = await ballotpedia.getElectionDates({
    state: 'California',
    type: ['General', 'Primary'],
    year: 2024
  });
  console.log(dates.data);
} catch (error) {
  console.error('Error fetching election dates:', error);
}

Getting Elections

Fetch elections by location:

try {
  const elections = await ballotpedia.getElectionsByPoint(
    37.7749,
    -122.4194,
    '2024-11-05',
    {
      collections: ['Federal', 'State']
    }
  );
  console.log(elections.data);
} catch (error) {
  console.error('Error fetching elections:', error);
}

Or fetch elections by state:

try {
  const elections = await ballotpedia.getElectionsByState(
    'California',
    '2024-11-05',
    {
      collections: ['Federal', 'State'],
      officeLevels: ['Federal', 'State'],
      officeBranches: ['Legislative', 'Executive']
    }
  );
  console.log(elections.data);
} catch (error) {
  console.error('Error fetching elections:', error);
}

Error Handling

The SDK provides three types of errors for better error handling:

  • BallotpediaError: Base error class for general errors
  • BallotpediaAPIError: For API-specific errors (includes status code and text)
  • BallotpediaValidationError: For input validation errors
try {
  const districts = await ballotpedia.getDistricts(lat, long);
} catch (error) {
  if (error instanceof BallotpediaAPIError) {
    console.error(`API Error ${error.statusCode}: ${error.message}`);
  } else if (error instanceof BallotpediaValidationError) {
    console.error(`Validation Error: ${error.message}`);
  } else if (error instanceof BallotpediaError) {
    console.error(`General Error: ${error.message}`);
  }
}

API Reference

| Method | Description | |--------|-------------| | getDistricts(lat: number, long: number) | Fetches voting districts for a specific latitude and longitude. | | getOfficeholders(lat: number, long: number, options?: RequestOptions) | Fetches current officeholders for a location with optional filtering. | | getElectionDatesByPoint(lat: number, long: number) | Fetches election dates for a specific latitude and longitude. | | getElectionDates(options?: ElectionDateOptions) | Fetches election dates based on provided parameters. | | getElectionsByPoint(lat: number, long: number, electionDate: string, options?: RequestOptions) | Fetches elections for a specific location and date. | | getElectionsByState(state: string, electionDate: string, options?: RequestOptions) | Fetches elections for a specific state and date. |

Types

RequestOptions

interface RequestOptions {
  collections?: string[];
  officeLevels?: ('Federal' | 'State' | 'Local')[];
  officeBranches?: ('Legislative' | 'Executive' | 'Judicial')[];
  districtTypes?: string[];
  page?: number;
}

ElectionDateOptions

interface ElectionDateOptions {
  state?: string;
  type?: ('General' | 'Primary' | 'Special' | 'Recall')[];
  year?: number;
  page?: number;
}