https://github.com/tterb/country-state-lib
Basic library for Countries and States
https://github.com/tterb/country-state-lib
country-codes geolocation states
Last synced: about 1 month ago
JSON representation
Basic library for Countries and States
- Host: GitHub
- URL: https://github.com/tterb/country-state-lib
- Owner: tterb
- License: gpl-3.0
- Created: 2020-11-18T22:50:17.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-07-08T11:35:25.000Z (over 2 years ago)
- Last Synced: 2025-03-14T15:45:51.226Z (8 months ago)
- Topics: country-codes, geolocation, states
- Language: TypeScript
- Homepage:
- Size: 728 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.npmjs.com/package/country-state-lib)
[](https://www.gnu.org/licenses/gpl-3.0)
# country-state-lib
Basic library for Countries and States
This is a fork of [harpreetkhalsagtbit/country-state-city](https://github.com/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](https://github.com/hiiamrohit/Countries-States-Cities-database)
# Install
```
npm install country-state-lib
```
or
```
yarn add country-state-lib
```
# Usage
- ES6 Module usage
```js
import csc from 'country-state-lib'
// Import Interfaces`
import { ICountry, IState } from 'country-state-lib'
```
- AMD Module usage
```js
let csc = require('country-state-lib').default
```
# Documentation
### getCountryByCode(code)
---------------
Accepts a valid `CountryCode` eg: `'AS'` and returns *Country Details*
type: **json | ICountry**
```js
{
"id": "4",
"sortname": "AS",
"name": "American Samoa",
"phonecode": "1684"
}
```
### getCountryById(id)
---------------
Accepts a valid `CountryId` and returns *Country Details*
type: **json | ICountry**
```js
{
"id": "4",
"sortname": "AS",
"name": "American Samoa",
"phonecode": "1684"
}
```
### getStateById(id)
---------------
Accepts a valid `StateId` and returns *State Details*
type: **json | IState**
```js
{
"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**
```js
[
{
"id": 4119,
"name": "Midlands",
"country_id": "246"
}
]
```
### getAllCountries
Returns **all Countries**
type: **array of json | ICountry**
```js
[
{
"id": "4",
"sortname": "AS",
"name": "American Samoa",
"phonecode": "1684"
}
]
```