countrystatecitylist

Library for Country, City and State/Province

Usage no npm install needed!

<script type="module">
  import countrystatecitylist from 'https://cdn.skypack.dev/countrystatecitylist';
</script>

README

countrystatecitylist

Basic library for Country, State and City

Initial database fetched from: https://github.com/hiiamrohit/Countries-States-Cities-database Improved and updated Repo from: https://github.com/harpreetkhalsagtbit/country-state-city

Installation

npm i countrystatecitylist

Integration

  • ES6 Module usage

    import csc from 'countrystatecitylist'
    
    // Import Interfaces`
    import { ICountry, IState, ICity } from 'countrystatecitylist'
    
  • AMD Module usage

    let csc = require('countrystatecitylist').default
    
    // OR
    
    let csc = require('countrystatecitylist')
    

Documentation

getCountryByCode(code)

It accepts a valid CountryCode (sortname) eg: 'AS' and returns Country Details

type: json | ICountry

example: getCountryByCode(AS)

{
    "id": "4",
    "sortname": "AS",
    "name": "American Samoa",
    "phonecode": "1684"
}

getCountryById(id)

It accepts a valid CountryId and returns Country Details

type: json | ICountry

example: getCountryById(4)

{
    "id": "4",
    "sortname": "AS",
    "name": "American Samoa",
    "phonecode": "1684"
}

getStateById(id)

It accepts a valid StateId and returns State Details

type: json | IState

example: getStateById(4119)

{
    "id": 4119,
    "name": "Midlands",
    "country_id": "246"
}

getCityById(id)

It accepts a valid CityId and returns City Details

type: json | ICity

example: getCityById(3)

{
    "id": "3",
    "name": "Port Blair",
    "state_id": "1"
}

getStatesOfCountry(countryId)

It accepts a valid CountryId and returns all States as Array of JSON

type: array of json | IState

[
  {
    "id": 4119,
    "name": "Midlands",
    "country_id": "246"
  }
]

getCitiesOfState(stateId)

It accepts a valid CityId and returns all Cities as Array of JSON

type: array of json | ICity

[
  {
    "id": "3",
    "name": "Port Blair",
    "state_id": "1"
  }
]

getAllCountries

It returns all Countries

type: array of json | ICountry

[
  {
    "id": "4",
    "sortname": "AS",
    "name": "American Samoa",
    "phonecode": "1684"
  }
]