Get Countries and States in Node.js app

Anup Kumar Panwar
3 min readAug 22, 2019

Selecting the country and state is often an essential part of the signup process in any web application. Generally, developers use 3rd party APIs for fetching countries and their states. These APIs are rated limited and may charge after some hits.

Another solution is to use any open source library. In this tutorial, I’ll tell you, how you can get a list of countries to inflate your dropdown for selecting countries and states.

Assuming you are building a Node app, we’ll use country-state-picker npm package. This npm package not only gives you the list of countries and their states but also the dial code prefix of all the countries.

To install this package, run the following command at the root of your node app.

npm i country-state-picker --save

Now you can import this package in your code. This package gives you 3 useful functions.

1. getCountries()

This function will return the array containing all the countries. A country object has the following format.

{
"name": "Andorra",
"code": "ad",
"dial_code": "+376"
}

Example

let countries = getCountries();console.log(countries);

Output

[
{
"name": "Afghanistan",
"code": "af",
"dial_code": "+93"
},
{
"name": "Albania",
"code": "al",
"dial_code": "+355"
},
{
"name": "Algeria",
"code": "dz",
"dial_code": "+213"
},
{
"name": "Andorra",
"code": "ad",
"dial_code": "+376"
}
...
]

2. getStates(<country_code>)

This function will return the array containing all the states of a country.

Example

let states = getStates('in');console.log(states);

Output

[
"Assam",
"Goa",
"Madhya Pradesh",
"Manipur",
"Meghalaya",
"Mizoram",
"National Capital Territory of Delhi",
"Sikkim",
"State of Andhra Pradesh",
"State of Arunachal Pradesh",
"State of Bihar",
"State of Chhattisgarh",
"State of Gujarat",
"State of Haryana",
"State of Himachal Pradesh",
"State of Jammu and Kashmir",
"State of Jharkhand",
"State of Karnataka",
"State of Kerala",
"State of Maharashtra",
"State of Nagaland",
"State of Odisha",
"State of Punjab",
"State of Rajasthan",
"State of Tamil Nad",
"State of Uttarakhand",
"Telangana",
"Tripura",
"Union Territory of Andaman and Nicobar Islands",
"Union Territory of Chandigarh",
"Union Territory of Dadra and Nagar Haveli",
"Union Territory of Daman and Di",
"Union Territory of Lakshadweep",
"Union Territory of Puducherry",
"Uttar Pradesh",
"West Bengal"
]

3. getCountry(<country_name | country_code | dial_code>)

This function will return the country object corresponding to the argument passed.

Example

let country = getCountry('+91');console.log(country);

Output

{
"name": "India",
"code": "in",
"dial_code": "+91"
}

4. getFilteredCountries([<country_name | country_code | dial_code>])

This function will receive an array of arguments and return the array of countries corresponding to the argument.

Example

let filteredCountries = getFilteredCountries(['+91', 'us', 'Australia']);

console.log(filteredCountries);

Output

[
{
"name": "India",
"code": "in",
"dial_code": "+91"
},
{
"name": "United States of America",
"code": "us",
"dial_code": "+1"
},
{
"name": "Australia",
"code": "au",
"dial_code": "+61"
}
]

That’s it. These 4 simple functions will provide you with the data required to build a country and state dropdown component in your node application.

Thanks

References

  1. https://www.npmjs.com/package/country-state-picker
  2. https://github.com/AnupKumarPanwar/country-state-picker

--

--

Anup Kumar Panwar

Product Engineer @ Gojek | Founder at The Algorithms | GSoC’18 @ FOSSASIA | And lots of Music & Football