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 🙏

© 2024 – Pkg Stats / Ryan Hefner

countries-states-cities-iso2-phone-codes-api

v1.0.0

Published

This will provide all countries, states, cities, ISO2 codes, phone codes, and country flags.

Downloads

13

Readme

Countries States Cities API

countries-states-cities-iso2-phone-codes-api is an NPM package that provides an easy-to-use interface to access global data related to countries, including their ISO2 codes, phone codes, states or provinces, and cities. The package allows you to fetch lists of countries, retrieve ISO2 codes by country name, get states by country ISO2 code, and fetch cities within a state.

Features

  • Get a complete list of countries.
  • Retrieve ISO2 codes and phone codes by country.
  • Fetch states or provinces for a specific country using the ISO2 code.
  • Retrieve cities for a specific state in a country.
  • Get the ISO2 code by country name.
  • Lightweight and easy to use.

Installation

To install this package, use npm:

npm install countries-states-cities-iso2-phone-codes-api

Usage

Below are some examples of how to use the package:

1. Importing the Package

First, require the countries-states-cities-iso2-phone-codes-api package:

const CountriesAPI = require('countries-states-cities-iso2-phone-codes-api');

2. Get the List of All Countries

Use the getCountryList method to get the list of all countries with their names:

const countryList = CountriesAPI.getCountryList();
console.log(countryList);
/*
Output:
[
  { name: 'United States' },
  { name: 'Canada' },
  { name: 'Australia' },
  // ...other countries
]
*/

3. Get All Countries with ISO2 and Phone Code

You can retrieve a complete list of countries with their ISO2 code and phone code using the getCountries method:

const countries = CountriesAPI.getCountries();
console.log(countries);
/*
Output:
[
  { name: 'United States', iso2: 'US', phone_code: '+1' },
  { name: 'Canada', iso2: 'CA', phone_code: '+1' },
  { name: 'Australia', iso2: 'AU', phone_code: '+61' },
  // ...other countries
]
*/

4. Get ISO2 Code by Country Name

To retrieve the ISO2 code of a country by its name, use the getIso2ByCountryName method:

const iso2 = CountriesAPI.getIso2ByCountryName('Canada');
console.log(iso2); // Output: "CA"

5. Get States by Country ISO2 Code

Retrieve a list of states or provinces in a specific country by providing the ISO2 code using getStatesByCountry:

const states = CountriesAPI.getStatesByCountry('US');
console.log(states);
/*
Output:
[
  'California',
  'Texas',
  'New York',
  // ...other states
]
*/

6. Get Cities by Country ISO2 Code and State Name

To get a list of cities within a specific state of a country, use the getCitiesByState method by passing the country ISO2 code and state name:

const cities = CountriesAPI.getCitiesByState('US', 'California');
console.log(cities);
/*
Output:
[
  'Los Angeles',
  'San Francisco',
  'San Diego',
  // ...other cities
]
*/

7. Get Phone Code by Country ISO2 Code

You can retrieve the phone code for a country using its ISO2 code with getPhoneCodeByCountry:

const phoneCode = CountriesAPI.getPhoneCodeByCountry('CA');
console.log(phoneCode); // Output: "+1"

API Methods

getCountryList()

Returns an array of all countries with their names.

Returns:

  • Array: A list of country objects { name: string }.

getCountries()

Returns an array of all countries with their names, ISO2 codes, and phone codes.

Returns:

  • Array: A list of country objects { name: string, iso2: string, phone_code: string }.

getIso2ByCountryName(countryName)

Returns the ISO2 code for the given country name.

Arguments:

  • countryName (string): The name of the country.

Returns:

  • String or null: The ISO2 code if found, otherwise null.

getStatesByCountry(iso2)

Returns an array of states or provinces for the given country ISO2 code.

Arguments:

  • iso2 (string): The ISO2 code of the country.

Returns:

  • Array or null: A list of state names if the country exists, otherwise null.

getCitiesByState(iso2, stateName)

Returns an array of cities for the given state name in the specified country.

Arguments:

  • iso2 (string): The ISO2 code of the country.
  • stateName (string): The name of the state or province.

Returns:

  • Array or null: A list of city names if the state exists, otherwise null.

getPhoneCodeByCountry(iso2)

Returns the phone code for the given country ISO2 code.

Arguments:

  • iso2 (string): The ISO2 code of the country.

Returns:

  • String or null: The phone code if found, otherwise null.

License

This package is licensed under the MIT License. See the LICENSE file for more details.

Author

Developed by Masum Billah .
For any issues, suggestions, or contributions, feel free to reach out here.


Feel free to contribute to this repository by submitting issues, suggesting features, or creating pull requests.