country-state-lib

Library for fetching Countries and States

Usage no npm install needed!

<script type="module">
  import countryStateLib from 'https://cdn.skypack.dev/country-state-lib';
</script>

README

country-state-lib

Basic library for Countries and States

This is a fork of harpreetkhalsagtbit/country-state-city that excludes the cities functionality, reducing the overall bundle-size by roughly 4.2MB.

Data sourced from hiiamrohit/Countries-States-Cities-database

Install

npm install country-state-lib

or

yarn add country-state-lib

Usage

  • ES6 Module usage

    import csc from 'country-state-lib'
    
    // Import Interfaces`
    import { ICountry, IState } from 'country-state-lib'
    
  • AMD Module usage

    let csc = require('country-state-lib').default
    

Documentation

getCountryByCode(code)


Accepts a valid CountryCode eg: 'AS' and returns Country Details

type: json | ICountry

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

getCountryById(id)


Accepts a valid CountryId and returns Country Details

type: json | ICountry

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

getStateById(id)


Accepts a valid StateId and returns State Details

type: json | IState

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

getStatesOfCountry(countryId)


Accepts a valid CountryId and returns all States as Array of JSON

type: array of json | IState

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

getAllCountries

Returns all Countries

type: array of json | ICountry

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