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

@countrydataapi/sdk

v1.0.0

Published

Official TypeScript/JavaScript SDK for Country Data API - Access countries, states, cities, and postal codes worldwide

Readme

Country Data API - TypeScript/JavaScript SDK

Official SDK for the Country Data API, providing easy access to geographical data including countries, states, cities, and postal codes worldwide.

Installation

npm install @countrydataapi/sdk
# or
yarn add @countrydataapi/sdk
# or
pnpm add @countrydataapi/sdk

Quick Start

import { CountryDataApi } from '@countrydataapi/sdk';

const api = new CountryDataApi({
  apiKey: 'your-api-key'
});

// Get all countries
const countries = await api.countries.getAll({ lang: 'en' });
console.log(countries.data);

// Get country by ISO code
const spain = await api.countries.getByCode({ code: 'ES' });
console.log(spain.data[0].country_name); // "Spain"

// Get states for a country
const states = await api.states.getByCountry({ country: 'Spain' });
console.log(states.data);

// Get cities for a state
const cities = await api.cities.getByState({ state: 'Madrid' });
console.log(cities.data);

// Check remaining tokens
const status = await api.getStatus();
console.log(`Remaining tokens: ${status.remainingTokens}`);

Configuration

const api = new CountryDataApi({
  apiKey: 'your-api-key',           // Required
  baseUrl: 'https://api.countrydataapi.com', // Optional
  timeout: 30000,                    // Optional (ms)
  defaultLanguage: 'en'              // Optional
});

API Reference

Countries

// Get all countries
api.countries.getAll(options?)

// Get country by name
api.countries.getByName({ name: 'Spain', ...options })

// Get country by ISO code (alpha-2, alpha-3, numeric, or CIOC)
api.countries.getByCode({ code: 'ES', ...options })

// Get countries by region
api.countries.getByRegion({ region: 'Europe', ...options })

// Get countries by currency
api.countries.getByCurrency({ currency: 'EUR', ...options })

// Get countries by language
api.countries.getByLanguage({ language: 'spa', ...options })

// Get countries by timezone
api.countries.getByTimezone({ timezone: 'Europe/Madrid', ...options })

States

// Get all states
api.states.getAll(options?)

// Get states by country
api.states.getByCountry({ country: 'Spain', ...options })

// Get states by city
api.states.getByCity({ city: 'Madrid', ...options })

// Get states by zipcode
api.states.getByZipcode({ zipcode: '28001', ...options })

Cities

// Get all cities
api.cities.getAll(options?)

// Get city by name
api.cities.get({ city: 'Madrid', ...options })

// Get cities by country
api.cities.getByCountry({ country: 'Spain', ...options })

// Get cities by state
api.cities.getByState({ state: 'Madrid', ...options })

Zipcodes

// Get all zipcodes
api.zipcodes.getAll(options?)

// Get zipcodes by country
api.zipcodes.getByCountry({ country: 'Spain', ...options })

// Get zipcodes by state
api.zipcodes.getByState({ state: 'Madrid', ...options })

// Get zipcodes by region
api.zipcodes.getByRegion({ region: 'Europe', ...options })

Select (Optimized for Dropdowns)

These endpoints cost only 1 token regardless of the number of results, making them perfect for populating dropdowns.

// Get countries for select
api.select.countries({ lang: 'es' })

// Get states for select
api.select.states({ country: 'ES', lang: 'es' })

// Get cities for select
api.select.cities({ state: 'Madrid', country: 'ES', lang: 'es' })

Request Options

All methods accept these common options:

| Option | Type | Description | |--------|------|-------------| | fields | string | Comma-separated list of fields to return | | lang | 'en' \| 'es' \| 'pt' \| 'fr' \| 'de' \| 'it' | Response language | | limit | number | Maximum number of items to return | | limitToken | number | Maximum tokens to use for this request |

Response Format

All methods return an ApiResponse<T> object:

interface ApiResponse<T> {
  success: boolean;
  data: T[];
  count: number;
  tokens_used: number;
  remaining_tokens?: number;
}

Error Handling

import {
  CountryDataApi,
  AuthenticationError,
  InsufficientTokensError
} from '@countrydataapi/sdk';

try {
  const countries = await api.countries.getAll();
} catch (error) {
  if (error instanceof AuthenticationError) {
    console.error('Invalid API key');
  } else if (error instanceof InsufficientTokensError) {
    console.error('No tokens remaining');
  } else {
    console.error('API error:', error.message);
  }
}

TypeScript Support

This SDK is written in TypeScript and provides full type definitions:

import type {
  Country,
  State,
  City,
  ApiResponse,
  Language
} from '@countrydataapi/sdk';

const response: ApiResponse<Country> = await api.countries.getByCode({
  code: 'ES'
});

const country: Country = response.data[0];
console.log(country.country_name);

License

MIT

Links